按后退退出全屏视频模式

Exit Full Screen Video Mode on Back Press

我刚开始使用 Google 提供的这个 GMF 示例。

我想知道如何通过点击后退按钮退出全屏视频模式,我尝试使用下面的代码,但没有成功,

here可以看到MainActivity.java

的实际代码
boolean isFullScreen = false; // globally declared

@Override
public void onGoToFullscreen() {
    isFullScreen = true;
    videoListView.setVisibility(View.INVISIBLE);
}

@Override
public void onReturnFromFullscreen() {
    videoListView.setVisibility(View.VISIBLE);
}

@Override
public void onBackPressed() {
    if(isFullScreen) {
        onReturnFromFullscreen();
    }
    else {
        super.onBackPressed();
    }
}

假设您已经围绕 Demo 构建了您的应用程序,在演示应用程序中您有包 com.google.googlemediaframeworkdemo.demo.adplayer 中的 class ImaPlayer,其中包含两个 SimpleVideoPlayer引用,顾名思义,一个是显示添加,一个是显示内容。

  /**
   * Plays the ad.
   */
  private SimpleVideoPlayer adPlayer;

 /**
   * Plays the content (i.e. the actual video).
   */
  private SimpleVideoPlayer contentPlayer;

要退出全屏,您需要在 SimpleVideoPlayer

上调用 setFullscreen(false)
public void setFullscreen(boolean shouldBeFullscreen)

Make the player enter or leave fullscreen mode.

Parameters:
    shouldBeFullscreen - If true, the player is put into fullscreen mode. If false, the player leaves fullscreen mode.

由于两个 SimpleVideoPlayer 都声明为私有,因此您无法访问它们。这里有 2 个解决方案来解决这个问题:

解决方案一:

ImaPlayer class 中为 adPlayercontentPlayer

创建吸气剂
public SimpleVideoPlayer getAdPlayer(){
    return this.adPlayer;
}

public SimpleVideoPlayer getContentPlayer(){
    return this.ContentPlayer;
}

在你处理后退键的地方MainActivity修改为这个

@Override
public void onBackPressed() {
    if(isFullScreen) {
        imaPlayer.getAdPlayer().setFullscreen(false);
        imaPlayer.getContentPlayer().setFullscreen(false);
        // after this calls you will see that your callback method onReturnFromFullscreen() will be called
    }
    else {
        super.onBackPressed();
    }
}

方案二:

ImaPlayerclass中添加这段代码:

public void exitFullscreen(){

 if (adPlayer != null) {
          adPlayer.setFullscreen(false);
        }
        contentPlayer.setFullscreen(false);
        //again after this calls you will see that your callback method onReturnFromFullscreen() will be called  
     }
 }

如果您没有围绕演示应用程序构建它,您需要调用您的视频播放器(很可能是 SimpleVideoPlayersetFullscreen(false)