Skip to content

Instantly share code, notes, and snippets.

@EleLawliet
Created July 20, 2016 19:46
Show Gist options
  • Save EleLawliet/7adc66b03065bd3a218c4b204b70e010 to your computer and use it in GitHub Desktop.
Save EleLawliet/7adc66b03065bd3a218c4b204b70e010 to your computer and use it in GitHub Desktop.
YoutubePlayerView Example.
dependencies {
...
compile 'com.jaedongchicken:ytplayer:1.2.0'
}
package com.misnutricionistas;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.google.gson.Gson;
import com.jaedongchicken.ytplayer.JLog;
import com.jaedongchicken.ytplayer.YoutubePlayerView;
import com.jaedongchicken.ytplayer.model.YTParams;
import com.objects.MrPublicacionDetalle;
import com.tools.Constantes;
import com.web.VolleySingleton;
import org.json.JSONArray;
/**
* Created by Junior on 16/06/2016.
*/
public class DetailEjercicio extends AppCompatActivity {
private Bundle bundle ;
private Gson gson = new Gson();
private static String PUBLICACION_ID = "publicacion_id=";
private YoutubePlayerView youtubePlayerView;
private static Intent intent = null;
private ProgressDialog barraprogreso = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_ejercicio);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
if(barraprogreso==null) barraprogreso = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT);
barraprogreso.show();
barraprogreso.setContentView(R.layout.progressdialogeneral);
barraprogreso.setCancelable(false);
youtubePlayerView = (YoutubePlayerView) findViewById(R.id.ypv_detail_ejercicio);
//method inicializa el video
inizializarVideo(intent.getStringExtra("Enlace"));
barraprogreso.dismiss();
intent = getIntent();
Bundle bundle = intent.getExtras();
youtubePlayerView.setAutoPlayerHeight(this);
YTParams params = new YTParams();
params.setControls(0);
// initialize YoutubePlayerCallBackListener with Params and VideoID
// youtubePlayerView.initialize("WCchr07kLPE", params, new YoutubePlayerView.YouTubeListener())
// initialize YoutubePlayerCallBackListener and VideoID
// pause video
// youtubePlayerView.pause();
// play video when it's ready
// youtubePlayerView.play();
// cue video
// youtubePlayerView.onCueVideo("WCchr07kLPE");
// start with 20 secs
// youtubePlayerView.onLoadVideo("WCchr07kLPE", 20);
// seek to 20 secs
// youtubePlayerView.seekToMillis(20);
//TODO enlace video ejercicio
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// you can cast with float that I recommend.
double sec = (double) msg.obj;
currentSec.setText(String.valueOf(sec));
}
};
@Override
protected void onDestroy() {
super.onDestroy();
// this is optional but you need.
youtubePlayerView.destroy();
}
private void inizializarVideo(String enlace) {
youtubePlayerView.initialize(enlace, new YoutubePlayerView.YouTubeListener() {
@Override
public void onReady() {
// when player is ready.
JLog.i("onReady()");
}
@Override
public void onStateChange(YoutubePlayerView.STATE state) {
/**
* YoutubePlayerView.STATE
* UNSTARTED, ENDED, PLAYING, PAUSED, BUFFERING, CUED, NONE
*/
JLog.i("onStateChange(" + state + ")");
}
@Override
public void onPlaybackQualityChange(String arg) {
JLog.i("onPlaybackQualityChange(" + arg + ")");
}
@Override
public void onPlaybackRateChange(String arg) {
JLog.i("onPlaybackRateChange(" + arg + ")");
}
@Override
public void onError(String arg) {
JLog.e("onError(" + arg + ")");
}
@Override
public void onApiChange(String arg) {
JLog.i("onApiChange(" + arg + ")");
}
@Override
public void onCurrentSecond(double second) {
// currentTime callback
Message msg = new Message();
msg.obj = second;
handler.sendMessage(msg);
}
@Override
public void onDuration(double duration) {
// total duration
JLog.i("onDuration(" + duration + ")");
}
@Override
public void logs(String log) {
// javascript debug log. you don't need to use it.
JLog.d(log);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DetailPublicacionRecetaNutricional">
<com.jaedongchicken.ytplayer.YoutubePlayerView
android:id="@+id/ypv_detail_ejercicio"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment