我正在使用youtube android Player API进行有关YouTube Live的项目。
public class YoutubeLiveActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
private Context mContext = this;
YouTubePlayerSupportFragment youTubePlayerFragment;
private YouTubePlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_live);
youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(API_KEY, this);
}
private void playVideoAtSelection() {
if (!(player == null)) {
player.loadVideo(LIVE_ID);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer arg1, boolean arg2) {
this.player = arg1;
playVideoAtSelection();
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
}
}
YouTubePlayer无法播放现场演奏,并显示"发生错误"点击重试"。但是,它可以播放普通视频。请帮助!
********************************
我发现有关YouTubePlayer上的实时视频的问题。这是因为Mobile的YouTube版本不是最新版本。一旦在Play商店更新了YouTube后,实时视频可以正常播放。
// Use the Youtube player.
public class YouTubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public String videoId = "";
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.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 YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
finish();
}
@Override
public void onVideoStarted() {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_player);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
videoId = bundle.getString(CommonUtils.INTENT_YOUTUBE_VIDEO_ID);
}
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(CommonUtils.You_Tube_Api_Key, this);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Error: Fail to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
if (!wasRestored) {
player.cueVideo(videoId);
}
}
}
activity_you_tube_player.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">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:padding="0dp" />
</LinearLayout>
Download Youtube jar File From this link, and Copy it into libs folder
[https://developers.google.com/youtube/android/player/downloads/][1]