3

我已經在我的Android應用程序中使用以下代碼實現了ExoPlayer。根據演示器演示應用程序中視頻的寬高比更改表面視圖的寬高比

String versionName; 
     try { 
      String packageName = getPackageName(); 
      PackageInfo info = getPackageManager().getPackageInfo(packageName, 0); 
      versionName = info.versionName; 
     } catch (PackageManager.NameNotFoundException e) { 
      versionName = "?"; 
     } 

     player = ExoPlayer.Factory.newInstance(2); 


     Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); 
     DataSource dataSource = new DefaultUriDataSource(this, null, versionName); 

     MediaPresentationDescriptionParser parser = new MediaPresentationDescriptionParser(); 
     UriDataSource manifestDataSource = new DefaultUriDataSource(this, versionName); 
     ManifestFetcher<MediaPresentationDescription> manifestFetcher = new ManifestFetcher<>(passedWorkoutObject.workoutMediaURL, manifestDataSource, parser); 

     ExtractorSampleSource sampleSource = new ExtractorSampleSource(
       Uri.parse(VIDEO_URL), dataSource, allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE); 

     MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(
       this, sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); 

     MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource); 
     player.prepare(videoRenderer, audioRenderer); 

     player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface); 

     player.setPlayWhenReady(true); 
     control = new PlayerControl(player); 

但問題是我需要播放所有方面比率的視頻。如何調整surfaceView以適應視頻的大小而不是其他方式?

+1

你解決了這個問題嗎? –

回答

3

請記住,我在AndroidManifest中使用android:screenOrientation="sensorLandscape"作爲包含我的項目中的ExoPlayer的Activity,下面是我用於根據視頻寬高比更改SurfaceView寬高比的方式。其中

1.將SurfaceView到容器(的FrameLayout)的大小將被改變:根據視頻的縱橫比

<FrameLayout 
     android:id="@+id/rootView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorBlack" 
     android:focusable="true" 
     android:keepScreenOn="true"> 

     <FrameLayout 
      android:id="@+id/video_frame" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"> 

      <SurfaceView 
       android:id="@+id/surfaceView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" /> 

     </FrameLayout> 

</FrameLayout> 

2.容器的變化大小:

private void applyAspectRatio(FrameLayout container, SimpleExoPlayer exoPlayer) { 
     float videoRatio = (float) exoPlayer.getVideoFormat().width/exoPlayer.getVideoFormat().height; 

     Display display = getActivity().getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     float displayRatio = (float) size.x/size.y; 

     if (videoRatio > displayRatio) { 
      Log.d(TAG, "applying " + videoRatio + "aspectRatio"); 
      container.getLayoutParams().height = Math.round(container.getMeasuredWidth()/videoRatio); 
      container.requestLayout(); 
     } else { 
      Log.d(TAG, "applying usual aspectRatio"); 
      FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
      params.gravity = CENTER; 
      container.setLayoutParams(params); 
     } 
    } 

3.將此applyAspectRatio()方法放入onPlayerStateChanged Activity或Fragmen的回調方法t(必須實現ExoPlayer.EventListener接口):

@Override 
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { 
     if (playbackState == ExoPlayer.STATE_READY) applyAspectRatio(); 
    }