gpt4 book ai didi

android - Android-YouTubePlayer Play Live

转载 作者:行者123 更新时间:2023-12-03 06:14:23 24 4
gpt4 key购买 nike

我正在使用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上播放实时视频的问题。这是因为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]

关于android - Android-YouTubePlayer Play Live,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46087415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com