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



我使用youtube player api成功播放了youtube视频。但我需要在后台按后退按钮运行它。我在谷歌上搜索了很多,但一无所获。请帮我实现这一点。提前感谢

这是我的代码-

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

我搜索了这个主题,发现没有官方的方式,也不会按照谷歌代码上的这个页面。

他们建议你创造一个玩家。

隐藏播放器。

将播放器静音。

使用您的视频ID和可选偏移量调用seekTo()。

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

按下暂停。

调用seekTo(originalOffset,false)。

取消播放器的静音。

展示玩家。(或其他)。

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

当点击后退按钮时,检查播放器的状态,我认为它将处于暂停和/或缓冲状态。您可以尝试在侦听器上进行修补,打开缓冲侦听器,调用playvideo/将状态更改为播放视频。我试过并让它在iOS上运行,不知道在这种情况下平台的局限性。

相关内容

  • 没有找到相关文章

最新更新