0

现在我正在创建一个使用 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");
        }
    }
}

}

4

0 回答 0