Java (Android): MP4 動画の再生

MoviePlayerActivity

XML: movie_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Java: MoviePlayerActivity.java

package jp.feb19.snippets;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

public class MoviePlayerActivity extends Activity {

	VideoView video;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.movie_layout);

		RelativeLayout videoLayout = (RelativeLayout) findViewById(R.id.videoLayout);
		videoLayout.setBackgroundColor(Color.BLACK);

		video = (VideoView) findViewById(R.id.videoView);

		// 設定 ムービーの設定は android.resource://{リソースパッケージ名}/{ムービーID} で指定する
		String path = "android.resource://jp.feb19.snippets/" + R.raw.movie;
		Uri pathToVideo = Uri.parse(path);
		video.setVideoURI(pathToVideo);

		// コントローラの設定
		video.setMediaController(new MediaController(this));

		video.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				// 準備完了したときの処理
				mp.start();
			}
		});
		video.setOnCompletionListener(new OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				// 完了したときの処理
				mp.seekTo(0);
				mp.start();
			}
		});
	}
}