退后退出全屏视频模式

时间:2016-03-11 05:59:43

标签: java android android-video-player onbackpressed

我刚开始使用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();
    }
}

1 个答案:

答案 0 :(得分:1)

假设您已围绕Demo构建应用程序,在演示应用程序中,您在包ImaPlayer中拥有类com.google.googlemediaframeworkdemo.demo.adplayer,其中包含两个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.

由于两个SimpleVideoPlayers都被声明为私有,因此无法访问它们。以下是解决此问题的两种解决方案:

解决方案1:

ImaPlayer课程中为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();
    }
}

解决方案2:

ImaPlayer课程中添加以下代码:

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)