Develop with pleasure!

福岡でCloudとかBlockchainとか。

Androidで動画再生

Androidで動画再生アプリ作ってみる。

AndroidでAudioとVideo - Develop with pleasure!みたいにMediaPlayer使ってゴリゴリ書けば良いのかと思ったら、VideoView | Android Developersを使えば、簡単にできる。

  • レイアウトファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <VideoView android:id="@+id/videoView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>
  • Activity
package jp.co.haw;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        VideoView video = (VideoView) findViewById(R.id.videoView);
        video.requestFocus();
        video.setMediaController(new MediaController(this));
        video.setVideoURI(Uri.parse("動画ファイルのパス"));
    }
}

こんだけ。動作させると↓な感じになる。

仕組み的にはVideoViewの内部でMediaPlayerを生成してよろしくやってくれてる。
再生可能な動画フォーマットは今のところ、H.263、H.264 AVC、MPEG-4 SPみたい。