在旋转时更改MediaController位置

时间:2015-04-07 17:25:01

标签: android android-layout android-mediaplayer android-video-player

我正试图在方向更改时动态更改MediaController的锚点。经过我的所有尝试后,这根本不能正常工作。

也许你可以指出我的错误。

用几句话说:

肖像模式:

  1. weightSum = 1。
  2. SurfaceView体重:0.4
  3. MediaControllerHorizontalScrollView重量:0.6
  4. MediaController始终可见但不隐藏
  5. 横向模式:

    1. SurfaceView重量:0.0(全屏)

    2. MediaControlls = View.Gone(我需要将其锚点更改为SurfaceView。并弹出onTouch。但是如何?)


    3. 代码:

      player.xml:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/root"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:keepScreenOn="true"
          tools:context="tenkol.design.com.imbrecords.ActivityExoPlayer"
          android:weightSum="1"
          android:orientation="vertical">
      
          <FrameLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/surface_wrapper"
              android:layout_weight="0.4">
      
              <com.google.android.exoplayer.VideoSurfaceView
                  android:id="@+id/surface_view"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_gravity="center" />
      
              <View
                  android:id="@+id/shutter"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@android:color/black" />
          </FrameLayout>
      
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="0.6"
              android:orientation="vertical">
      
              <FrameLayout
                  android:id="@+id/controls_wrapper"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="2dp">
              </FrameLayout>
      
              <HorizontalScrollView
                  android:id="@+id/myGallery"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="0dp">
      
                  <LinearLayout
                      android:id="@+id/channelsScrollView"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:gravity="center"
                      android:orientation="horizontal"
                      android:paddingBottom="5dp"
                      android:paddingTop="5dp"
                      />
              </HorizontalScrollView>
      
          </LinearLayout>
      
      </LinearLayout>
      

      带转化的代码:

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
      
          if (Global.getScreenOrientation(this) == Configuration.ORIENTATION_LANDSCAPE) {
              //hide action bar
              getSupportActionBar().hide();
              if (myGallery != null) {
                  //hide HorizontalScrollView
                  myGallery.setVisibility(View.GONE);
              }
              //make surface fullscreen
              surfaceWrapper.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, 0f));
              controlsWrapper.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, 0f));
              //hide mediaController
              mc.hide();
              controlsWrapper.setVisibility(View.GONE);
          }
          if (Global.getScreenOrientation(this) == Configuration.ORIENTATION_PORTRAIT) {
              //and here restore everything
              getSupportActionBar().show();
              if (myGallery != null) {
                  myGallery.setVisibility(View.VISIBLE);
              }
      
              surfaceWrapper.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, 0.4f));
              controlsWrapper.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, 0.6f));
              controlsWrapper.setVisibility(View.VISIBLE);
          }
      }
      

      我尝试了什么:

      1)当Landscape:创建另一个mediaController并绑定到根视图(不起作用)时:

        mediaController.setAnchorView(root);
        mediaController.setMediaPlayer(player.getPlayerControl());
        mediaController.setEnabled(true);
        mediaController.show(3);
      

      2)横向时:更改先前MediaController(Nah)的锚点。

        mc.setAnchorView(surfaceWrapper);
        mc.show(3);
      

      3)interface surfaceChanged(仍然没有):

      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
          if (Global.getScreenOrientation(this) == Configuration.ORIENTATION_LANDSCAPE) {
              mc.setAnchorView(surfaceWrapper);
              mc.setMediaPlayer(player.getPlayerControl());
              mc.setVisibility(View.VISIBLE);
              mc.show(3);
          }
      }
      

      那么如何在MediaController模式下动态将SurfaceView的锚点更改为LANDSCAPE

      P.S。告诉是否需要更多代码。

1 个答案:

答案 0 :(得分:1)

由于你要覆盖onConfigurationChanged,我认为这意味着该活动不允许Android管理其方向更改,而是选择自行完成所有操作。这通常不是一个好主意,你现在正面临着这个决定的痛苦。 : - (

如果我可以提出建议:让Android管理您活动的生命周期,并使用不同的布局(例如layout-land / views.xml和layout-port / views.xml)。这将允许您自然地在XML中表达您的布局,而无需进行疯狂的代码更改。

您可能会说:但我希望我的ExoPlayer不必停下来,准备并重新开始每次方向更改。好消息是,有办法解决这个问题。

只需创建一个ExoPlayer,在第一次加载时准备它,并继续使用每个新活动实例的那个实例(存储在静态或其他单例中)。您可以从中分离旧活动,并在准备好后重新连接新活动。

如果ExoPlayer与MediaPlayer的工作方式类似,那么这一切都可以正常工作。