使用Youtube播放器API在后台播放Youtube视频

6
我成功使用 Youtube 播放器 API 播放了视频。但是我需要在按下后退按钮时将其在后台运行。我已经搜索了很多,但没有找到答案。请帮助我实现这一点。提前致谢。
以下是我的代码-
public class FullscreenDemoActivity extends YouTubeFailureRecoveryActivity
        implements View.OnClickListener,

        YouTubePlayer.OnFullscreenListener {
//  private MyPlaybackEventListener myPlaybackEventListener;
    private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

    private LinearLayout baseLayout;
    private YouTubePlayerView playerView;
    public YouTubePlayer player;
    private Button fullscreenButton;
    private CompoundButton checkbox;
    private View otherViews;

    private boolean fullscreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fullscreen_demo);
        baseLayout = (LinearLayout) findViewById(R.id.layout);
        playerView = (YouTubePlayerView) findViewById(R.id.player);
        fullscreenButton = (Button) findViewById(R.id.fullscreen_button);
        checkbox = (CompoundButton) findViewById(R.id.landscape_fullscreen_checkbox);
        otherViews = findViewById(R.id.other_views);
        playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
            YouTubePlayer player, boolean wasRestored) {
        this.player = player;
        setControlsEnabled();
        // Specify that we want to handle fullscreen behavior ourselves.
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
        player.setOnFullscreenListener(this);
        if (!wasRestored) {
            player.cueVideo("avP5d16wEp0");
        }
        int controlFlags = player.getFullscreenControlFlags();
        setRequestedOrientation(PORTRAIT_ORIENTATION);
        controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
        player.setFullscreenControlFlags(controlFlags);
        player.play();


    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return playerView;
    }

    @Override
    public void onClick(View v) {
        player.setFullscreen(!fullscreen);
    }



    private void doLayout() {
        LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) playerView
                .getLayoutParams();
        if (fullscreen) {
            // When in fullscreen, the visibility of all other views than the
            // player should be set to
            // GONE and the player should be laid out across the whole screen.
            playerParams.width = LayoutParams.MATCH_PARENT;
            playerParams.height = LayoutParams.MATCH_PARENT;

            otherViews.setVisibility(View.GONE);
        } else {
            otherViews.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams otherViewsParams = otherViews
                    .getLayoutParams();
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                playerParams.width = otherViewsParams.width = 0;
                playerParams.height = WRAP_CONTENT;
                otherViewsParams.height = MATCH_PARENT;
                playerParams.weight = 1;
                baseLayout.setOrientation(LinearLayout.HORIZONTAL);
            } else {
                playerParams.width = otherViewsParams.width = MATCH_PARENT;
                playerParams.height = WRAP_CONTENT;
                playerParams.weight = 0;
                otherViewsParams.height = 0;
                baseLayout.setOrientation(LinearLayout.VERTICAL);
            }
            setControlsEnabled();
        }
    }

    private void setControlsEnabled() {
        checkbox.setEnabled(player != null
                && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
        fullscreenButton.setEnabled(player != null);
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        fullscreen = isFullscreen;
        doLayout();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // System.out.println(getScreenOrientation());
        doLayout();
    }

    private final class MyPlaybackEventListener implements PlaybackEventListener {

          private void updateLog(String prompt){
        System.out.println(prompt);
          };

          @Override
          public void onBuffering(boolean arg0) {
           updateLog("onBuffering(): " + String.valueOf(arg0));
          }

          @Override
          public void onPaused() {

           updateLog("onPaused()");
          }

          @Override
          public void onPlaying() {
           updateLog("onPlaying()");

          }

          @Override
          public void onSeekTo(int arg0) {
           updateLog("onSeekTo(): " + String.valueOf(arg0));
          }

          @Override
          public void onStopped() {
              player.loadVideo("avP5d16wEp0");

              player.cueVideo("avP5d16wEp0");
              player.play();
           updateLog("onStopped()");
          }

         }

}
2个回答

1

我搜索了那个主题,发现没有官方的方法,根据Google code上的这个页面

他们建议你创建一个播放器。

隐藏播放器。

静音播放器。

使用你的videoId和可选偏移调用seekTo()。

等待状态变为“playing”。

按暂停键。

使用seekTo(originalOffset, false)。

取消静音播放器。

显示播放器。(或者其他什么)

也许它不能满足你的需求,但也许你可以使用这种方法改变你需要的东西来实现你的目标。


该解决方案的问题在于,当 Activity/Fragment 在后台运行时,YouTube 播放器将停止播放,我需要它继续播放。 - BoazGarty
我对我要说的事情不是很了解。不妨使用一个处理缓冲然后将其传递给活动的服务。我会和你一起研究它,如果有人发现什么,他会在这里发布。 - Abkarino
也许这可以帮助你。 https://groups.google.com/forum/#!topic/android-developers/3sEonjD0Nqk - Abkarino
1
即使可以传递视频流,我们仍需要在调用onPause后让播放器继续播放,Activity/Fragment必须始终保持活动状态。 - BoazGarty
onPause()对服务没有影响。这意味着保持视频流在服务中,并在保持服务开启的同时随时从中传递所需内容到活动中。在视频结束时,您可以选择清除服务流并关闭服务。 - Abkarino

0
点击返回按钮时,检查播放器的状态,我认为它会处于暂停和/或缓冲状态。你可以尝试在监听器中进行修补,在缓冲监听器中调用playvideo /更改状态为播放视频。我已经在iOS上尝试过,并且有效果,不清楚这种情况在其他平台上是否有限制。

然而,这可能会导致播放器停止并重新开始。我需要它像屏幕在前台一样继续播放。 - BoazGarty
视频并没有停止,而是暂停了,所以它会从暂停的地方继续播放,尝试一下,我相信它会起作用的 @david - Rakii
我知道它已经暂停了,我已经实现了这个功能,但我需要视频继续播放而不是暂停约一秒钟再继续播放。 - BoazGarty
是的,那个小暂停,我注意到了。这是不可避免的。 - Rakii

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接