现在我正在创建一个使用YouTube API播放视频的Android应用程序。 但是当我改变活动时,玩家停止了。 即使活动会改变,我也希望像音乐播放器一样继续播放。 我尝试通过在YouTubePlayer上添加静态来解决这个问题。(但无法解决。)
我的意思是当活动或应用程序本身转移到后台时,我想保持它正在播放。
所以,如果您有任何想法,请与我分享:)
-----------------代码-----------------
public class PlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,YouTubePlayer.PlayerStateChangeListener{
public static final String DEVELOPER_KEY = "My_Dev_Key";
private LinearLayout yPos;
private static YouTubePlayer yt;
private YouTubePlayerView ytp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
yPos = (LinearLayout)findViewById(R.id.youtube_pos);
try{
ytp = new YouTubePlayerView(this);
ytp.initialize(DEVELOPER_KEY, this);
}catch(Exception e){
finish();
}
yPos.addView(ytp);
}
@Override
public void onAdStarted() {
// TODO Auto-generated method stub
}
@Override
public void onError(ErrorReason arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLoaded(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLoading() {
// TODO Auto-generated method stub
}
@Override
public void onVideoEnded() {
// TODO Auto-generated method stub
}
@Override
public void onVideoStarted() {
// TODO Auto-generated method stub
}
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
if(!arg2){
if(yt == null){
yt = arg1;
yt.setPlayerStateChangeListener(this);
yt.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yt.loadVideo("u0v5A6cQOhs");
}
}
}
}