我对android youtube api有一些问题。我的代码似乎在我第一次调用我的youtube意图时工作得很好,但每当我调用它时 第二次我只有一秒的播放时间。我通过我的一个文件中的一个简单按钮调用intent。所有帮助都非常感谢
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(),
// ShimmerGraphandLogService.class);
// startActivityForResult(intent, 0);
Intent intent = new Intent(v.getContext(), youtube_player.class);
intent.putExtra("exercise",exercise_count );
startActivityForResult(intent, 0);
}
});
这是youtube布局文件
<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>
这是youtube java文件
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; //hollla
@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) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.youtube_player, menu);
return true;
}
/**
* YouTubePlayerView init listeners
*/
@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]);
//playa.play();
}
private class MHPlaybackEventListener implements PlaybackEventListener {
@Override
public void onBuffering(boolean arg0) {
//Toast.makeText(youtube_player.this, "onBuffering video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onPaused() {
//Toast.makeText(youtube_player.this, "onPaused video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onPlaying() {
//Toast.makeText(youtube_player.this, "onPlaying video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onSeekTo(int arg0) {
//Toast.makeText(youtube_player.this, "onSeekTo video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopped() {
//Toast.makeText(youtube_player.this, "onStopped video...", Toast.LENGTH_SHORT).show();
}
}
private class MHPlayerStateChangeListener implements PlayerStateChangeListener {
@Override
public void onAdStarted() {
//Toast.makeText(youtube_player.this, "onAdStarted video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(ErrorReason arg0) {
//Toast.makeText(youtube_player.this, "onError video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoaded(String arg0) {
playa.play();
//pauseButton.setVisibility(View.VISIBLE);
}
@Override
public void onLoading() {
//Toast.makeText(youtube_player.this, "onLoading video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onVideoEnded() {
//Toast.makeText(youtube_player.this, "onVideoEnded video...", Toast.LENGTH_SHORT).show();
}
@Override
public void onVideoStarted() {
//Toast.makeText(youtube_player.this, "onVideoStarted video...", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
View view = this.getWindow().getDecorView().findViewById(android.R.id.content);;
view.setVisibility(View.GONE);
}
}
这是我收到的警告/错误。
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(这些都应该是正数)。