使用 youtube player api 在后台播放 Youtube 视频 [英] Play Youtube video in background using youtube player api

查看:30
本文介绍了使用 youtube player api 在后台播放 Youtube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 youtube player api 成功播放了 youtube 视频.但我需要在按下后退按钮时在后台运行它.我搜索了很多但一无所获请帮助我实现这一点.提前致谢

I have successfully played youtube video using youtube player api. But I need to run it in background on back button press.I have googled a lot but found nothing Please help me to achieve this.Thanks in advance

这是我的代码-

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()");
          }

         }

}

推荐答案

我搜索了那个话题,发现没有官方的方式,不会按照此页面在 Google 代码上.

I searched for that topic and I found that there is not an official way and won't be according to this page on Google code.

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

In it they propose that you create a player.

隐藏播放器.

将播放器静音.

使用您的 videoId 和可选偏移量调用 seekTo().

Call seekTo() with your videoId and optional offset.

等待状态更改为正在播放".

Wait for state change to "playing".

按下暂停键.

调用 seekTo(originalOffset, false).

Call seekTo(originalOffset, false).

取消静音播放器.

显示播放器.(或其他).

Show the player. (or whatever).

它可能无法满足您的需要,但也许您可以使用此方法更改实现目标所需的内容.

It maybe doesn't do what you need but maybe you can change what you need to achieve your goal using this method.

这篇关于使用 youtube player api 在后台播放 Youtube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆