66900


android4.4で動画再生すると、90度回転して再生されます。  
android8は正常に再生されます。

exoplayerのバージョンは2.11.4です。  
com.google.android.exoplayer:exoplayer-core:2.11.4

android4.4で正常に再生するにはどうすればよろしいでしょうか？  
アドバイスをよろしくお願いいたします。

[code]

    public class VideoView extends FrameLayout {
    
      private HandlerWrapper mHandler;
      private Uri mUri;
      private SurfaceView mSurfaceView;
      private SimpleExoPlayer mMediaPlayer;
    
      public VideoView(Context context) {
        super(context);
        init(null, 0);
      }
    
      public VideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
      }
    
      public VideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
      }
    
      @Override
      protected void onDetachedFromWindow() {
        releaseMediaPlayer();
        super.onDetachedFromWindow();
      }
    
      public void setHandler(HandlerWrapper handler) {
        mHandler = handler;
      }
    
      public void setUri(Uri uri) {
        mUri = uri;
      }
    
      public void play() {
        if (mMediaPlayer == null)
          return;
        mMediaPlayer.setPlayWhenReady(true);
      }
    
      public void pause() {
        if (mMediaPlayer == null)
          return;
        mMediaPlayer.setPlayWhenReady(false);
      }
    
      public void seekTo(long position) {
        if (mMediaPlayer == null)
          return;
        mMediaPlayer.seekTo(position);
      }
    
      public boolean isPlaying() {
        return mMediaPlayer != null &&
               mMediaPlayer.getPlaybackState() != Player.STATE_ENDED &&
               mMediaPlayer.getPlaybackState() != Player.STATE_IDLE  &&
               mMediaPlayer.getPlayWhenReady();
      }
    
      private void init(AttributeSet attrs, int defStyle) {
        mSurfaceView = new SurfaceView(getContext());
        mSurfaceView.setZOrderOnTop(true);
        addView(mSurfaceView, 1, 1);
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
            DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
                                               .setPrioritizeTimeOverSizeThresholds(false)
                                               .createDefaultLoadControl();
            mMediaPlayer = new SimpleExoPlayer.Builder(getContext())
                             .setLoadControl(loadControl)
                             .build();
            mMediaPlayer.setVideoSurfaceHolder(holder);
            Context context = getContext();
            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                                                     Util.getUserAgent(context, context.getPackageName()));
            MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
                                        .createMediaSource(mUri);
            mMediaPlayer.addVideoListener(new VideoListener() {
              @Override
              public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
                layoutSurfaceView(width, height);
              }
            });
            mMediaPlayer.addListener(new EventListener() {
              private boolean mReady;
              @Override
              public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
              }
              @Override
              public void onIsPlayingChanged(boolean isPlaying) {
              }
              @Override
              public void onPlayerError(ExoPlaybackException error) {
              }
            });
            mMediaPlayer.prepare(videoSource);
          }
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
            releaseMediaPlayer();
          }
        });
      }
    
      private void releaseMediaPlayer() {
        if (mMediaPlayer == null)
          return;
        mMediaPlayer.release();
        mMediaPlayer = null;
      }
    
      private void layoutSurfaceView(int width, int height) {
        if (mMediaPlayer == null)
          return;
        float radio = Math.min((float)getWidth() / (float)width, (float)getHeight() / (float)height);
        LayoutParams params = (LayoutParams)mSurfaceView.getLayoutParams();
        params.width   = (int)Math.floor(width  * radio);
        params.height  = (int)Math.floor(height * radio);
        params.gravity = Gravity.CENTER;
        mSurfaceView.requestLayout();
      }
    
    }
    
[/code]


CC BY-SA 4.0
2020-05-18T03:30:16.120
66737
2020-05-22T23:45:40.147
7212
question
android
androidでExoPlayerで動画再生すると90度回転して再生されます。


自己レスです。  
SurfaceViewをTextureViewに変更して、onVideoSizeChangedイベントのunappliedRotationDegreesをみて、必要に応じて回転することで解決しました。

[code]

    mMediaPlayer.addVideoListener(new VideoListener() {
      @Override
      public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
        int viewWidth  = getWidth();
        int viewHeight = getHeight();
        Matrix transform = new Matrix();
        float pivotX = (float)viewWidth  / 2.0f;
        float pivotY = (float)viewHeight / 2.0f;
        if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) {
          transform.postRotate(unappliedRotationDegrees, pivotX, pivotY);
          float radio = Math.min((float)viewWidth / (float)height, (float)viewHeight / (float)width);
          transform.postScale((float)height * radio / (float)viewHeight,
                              (float)width  * radio / (float)viewWidth, pivotX, pivotY);
        } else {
          float radio = Math.min((float)viewWidth / (float)width, (float)viewHeight / (float)height);
          transform.postScale((float)width  * radio / (float)viewWidth,
                              (float)height * radio / (float)viewHeight, pivotX, pivotY);
        }
        mTextureView.setTransform(transform);
      }
    });
    
[/code]


CC BY-SA 4.0
2020-05-22T23:45:40.147
66900
2020-05-22T23:45:40.147
7212
66737
answer