Android で動画再生するのに良い方法は?

画面の向き portrate で動画再生の方法を2とおり試してみる。

「VideoView 使用」
  VideoViewsetMediaController メソッドで以下のように紐つける。

   final MediaController controller = new MediaController(this);
   videoView = (VideoView)findViewById(R.id.videoView1);
   videoView.setMediaController(controller);
   videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
      @Override
      public void onPrepared(MediaPlayer mp){
          controller.setAnchorView(videoView);
         controller.show(videoView.getDuration());
         //videoView.start();
      }
   });
  
 画面レイアウトは、portrate に、、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="タイトル"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="209dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Information"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

画面は以下のようになる。

f:id:posturan:20160313195220p:plain



「MediaPlayer 使用+SurfaceView 」
  SurfaceView に、MediaPlayer を表示させる方法で、SurfaceView 生成時に、SurfaceHolder
  を表示先に指定する。

  以下のように、SurfaceHolder.Callback 実装の中で MediaPlayer を生成する方法

  MediaPlayer mMediaPlayer;
  private SurfaceView mSurfaceView;
   :
  mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
   :
  @Override
  public void surfaceCreated(
SurfaceHolder holder){

     String path = System.getenv("EXTERNAL_STORAGE")   + "/test_640x360.mp4";
     try{
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);
        mController = new MediaController(this);
        mController.setMediaPlayer(this);
        mController.setAnchorView(mSurfaceView);
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
           @Override
           public void onPrepared(MediaPlayer mp){
              new Handler().post(new Runnable(){
                 @Override
                 public void run(){
                    mController.setEnabled(true);
                    mController.show(0);
                    //mMediaPlayer.start();
                 }
              });
           }
        });
        mMediaPlayer.prepare();
     }catch(Exception e){
        // TODO
     }
  }
  この他、SurfaceHolder.Callback の実装メソッドsurfaceChanged、surfaceDestroyed があり
  細かい制御ができるけど、VideoView のようにタップしてコントローラの表示/非表示の切り替えは
  明示的に、SurfaceView に、setOnClickListener で MediaController.show MediaController.hide を
  書く必要がある。

  MediaController が、放っておいて消えてしまうことがあるので表示復活させるために、
  SurfaceView に、setOnClickListener で、MediaController.show は書きたくなくても
  書くことになる。

  画面レイアウトは、SurfaceView にするだけ。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="タイトル"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Information"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>


画面向き縦横切り替え、回転時に、Viewサイズ変更で、surfaceChanged 実装を書ける分、
MediaPlayer 使用+SurfaceViewは、安定して動かすことができた。


”MediaPlayer 使用+SurfaceView”が良いのは、ループ再生をする場合、
MediaPlayer インスタンス生成後、setLooping(true) を実行するだけで、ループ再生になる。

VideoView だと、VideoView#setOnCompletionListenerメソッドをオーバーライドし
再生完了通知を受けて、、、なんて書かなければならず面倒である。


まあ1つの判断材料にはなるであろう。