我终于在我的应用上播放了YouTube直播,唯一的不便是当我进入全屏模式时,播放器停止播放并且不再播放,有时当我停止播放器并尝试再次播放时播放2秒然后停止,我知道YouTube直播处于测试阶段,但这并不意味着我们找不到解决方案,有没有人对如何解决这个问题有任何建议?
LiveYouTube.java
public class LiveYouTube extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyBMwV1heA9q75LkybRCK5SXXTJWADRtmDh4";
//http://youtu.be/<VIDEO_ID>
public static final String VIDEO_ID = "URS94beDtJA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** attaching layout xml **/
setContentView(R.layout.youtubelive);
/** Initializing YouTube player view **/
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new 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 PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
};
youtubelive.xml
<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" xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/rel"
android:layout_width="fill_parent"
android:layout_height="@dimen/play_list_row_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#c00311" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:gravity="center"
android:text="LIVE VIDEO"
android:textColor="@color/video_descrption_top_bar_text"
android:textSize="@dimen/video_view_top_bar_font_size"
android:textStyle="bold" />
</RelativeLayout>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="0dp" />
<ImageView
android:id="@+id/logo"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_marginTop="-13dp"
android:src="@drawable/shadow_line" />