I have some problems with android youtube api. My code seems to work just fine the first time I call my intention YouTube, when I call it a second time I get only one second of playback. I call intention a simple button in one of my files. All help is truly appreciated.
ImageView btnplay_video = (ImageView) findViewById(R.id.button_video);
btnplay_video.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), youtube_player.class);
intent.putExtra("exercise",exercise_count );
startActivityForResult(intent, 0);
}
});
This is youtube layout file
<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" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And this is youtube jout file
public class youtube_player extends YouTubeBaseActivity implements OnInitializedListener {
public String[] Exercises = { "Walking on the spot", "Side steps",
"V-step", "Running on the spot + arm movement", "Side jumps" };
public String[] Youtube_urls = {"FzZcCix_-3k","EIKh0AZGSEQ","pRgnFVAJZe8","R6hCksYs73M","Wx778vK_Lio"};
public String youtube_android_api_key = "AIzaSyCDfKgYduc4sGK9C1weGAIHzsM2LjtvM_M";
private YouTubePlayerView playerView;
private YouTubePlayer playa;
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube_player);
playerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);
playerView.initialize(youtube_android_api_key, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error)
{
Toast.makeText(youtube_player.this,
"Oh dear, something has gone terribly wrong please try ejecting and re-inserting your diskette", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
playa = player;
playa.setPlaybackEventListener(new MHPlaybackEventListener());
playa.setPlayerStateChangeListener(new MHPlayerStateChangeListener());
playa.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
int exercise = (int) this.getIntent().getExtras().get("exercise");
playa.cueVideo(Youtube_urls[exercise]);
}
private class MHPlaybackEventListener implements PlaybackEventListener {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
}
private class MHPlayerStateChangeListener implements PlayerStateChangeListener {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
playa.play();
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
View view = this.getWindow().getDecorView().findViewById(android.R.id.content);;
view.setVisibility(View.GONE);
}
}
This is the warning / error I get.
YouTube - . YouTubePlayerView com.google.android.youtube.player.YouTubePlayerView {212125c5 V.E........... ID 0,0-0,0 # 7f0a0060 app: id/youtube_player_view}. YouTubePlayerView: left: 0, top: 0, right: 0, bottom: 0 ( ).