0

我想以纵向模式全屏运行视频。我可以全屏播放视频,但我的视频正在拉伸。我正在使用视频视图。并尝试了 TextureView/SurfaceView 但没有运气。

我无法保持视频的纵横比。

下面是我的代码:

getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceHolder = binding.video.getHolder();

      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mediaPlayer = new MediaPlayer();
      if(mediaPlayer.isPlaying()){
         mediaPlayer.reset();
      }

      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


      try {
         mediaPlayer.setDataSource(getApplicationContext(), mUri);
         mediaPlayer.prepare();
      } catch (IllegalArgumentException e) {
         e.printStackTrace();
      } catch (IllegalStateException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

//      surfaceHolder.setFixedSize(surfaceView_Width,surfaceView_Height);
      mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
         @Override
         public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.setLooping(true);



     /*
           *  Handle aspect ratio
           */

                int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
                int screenHeight = getWindowManager().getDefaultDisplay().getHeight();

                float screenProportion = (float) screenWidth / (float) screenHeight;

                int surfaceView_Width = binding.video.getWidth();
                int surfaceView_Height = binding.video.getHeight();

                float video_Width = mediaPlayer.getVideoWidth();
                float video_Height = mediaPlayer.getVideoHeight();

                float videoProportion = (float) video_Width / (float) video_Height;
                LayoutParams layoutParams = binding.video.getLayoutParams();

                if (videoProportion > screenProportion) {
                   layoutParams.width = screenWidth;
                   layoutParams.height = (int) ((float) screenWidth / videoProportion);
                } else {
                   layoutParams.width = (int) (videoProportion * (float) screenHeight);
                   layoutParams.height = screenHeight;
                }

                binding.video.setLayoutParams(layoutParams);

 @Override
   public void surfaceCreated(SurfaceHolder surfaceHolder) {
      mediaPlayer.setDisplay(surfaceHolder);
   }

请帮我解决一下这个。提前致谢。

4

0 回答 0