我为youtube播放器创建了一个自定义布局..
第一次加载时会显示视频 - 这里没问题
如果我关闭此页&再次打开同一页面 - 显示黑屏
我的代码:
public class CustomYouTubeView extends LinearLayout implements YouTubePlayer.OnInitializedListener {
private String mYouTubeVideoId;
private String TAG=CustomYouTubeView.class.getSimpleName();
private YouTubePlayerView mYouTubePlayerView;
private YouTubeIntializerListener youTubeIntializerListener;
public CustomYouTubeView(Context context){
super(context);
}
public CustomYouTubeView(Activity context, String youTubeVideoId , YouTubeIntializerListener youTubeIntializerListener) {
super(context);
Log.d(TAG,"CustomYouTubeView :: ");
this.youTubeIntializerListener=youTubeIntializerListener;
mYouTubeVideoId = youTubeVideoId;
//String apiKey = "AIzaSyCLLeT5eV5jCHZIjNME4MG6q_GEftd8crQ";
String apiKey=context.getString(R.string.google_api_key);
mYouTubePlayerView = new YouTubePlayerView(context);
mYouTubePlayerView.setTag(mYouTubeVideoId);
mYouTubePlayerView.initialize(apiKey, this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//int margin = (int) ScreenUtils.dip2pixel(10, context);
params.leftMargin = 10;
params.rightMargin = 10;
params.topMargin = 10;
params.bottomMargin = 10;
addView(mYouTubePlayerView, params);
}
private void releasePlayer(){
mYouTubePlayerView=null;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
Log.d(TAG,"onInitializationFailure()");
System.out.println("CustomYouTubeView onInitializationFailure");
youTubeIntializerListener.youTubeFalied();
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, final boolean wasRestored) {
Log.d(TAG,"onInitializationSuccess()");
System.out.println("CustomYouTubeView onInitializationSuccess");
if (!wasRestored) {
player.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onVideoStarted() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
@Override
public void onLoading() {
}
@Override
public void onLoaded(String videoId) {
Log.d(TAG, "onInitializationSuccess()");
player.loadVideo(mYouTubeVideoId);
youTubeIntializerListener.youTubeInitialized();
}
@Override
public void onAdStarted() {
}
});
}
}
任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
播放Youtube视频的代码是
YouTubePlayerView video = new YouTubePlayerView(this);
video.initialize(SERVER_API_KEY, this);
addView(video, params);
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if(!wasRestored) {
try {
youTubePlayer.cueVideo(data.getString("videoId", "").toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
可能是因为你的
private void releasePlayer(){ mYouTubePlayerView=null; }
你没有第二次播放视频,删除此功能并尝试。