※前提条件:本情報はAndroid Studio 1.5.1を基づいて説明してる VideoView †レイアウトの準備 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/view" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </RelativeLayout> ファイルPathから再生 VideoView view = (VideoView) findViewById(R.id.videoView); view.setVideoPath(Environment.getExternalStorageDirectory().toString() + "/test.mp4"); view.start(); URLから再生 VideoView view = (VideoView) findViewById(R.id.videoView); view.setVideoURI(Uri.parse("http://test.com/test.mp4")); view.start(); 注意点 上記サンプルソースでは、setVideoPath()または、setVideoURI()直後にstart()を実行していますが、 setVideoXXメソッドは、非同期であるため準備ができるまで再生を待つ必要があります。 そのため、リスナーを設定し、準備が完了してから再生を行うのが正しい実装です。 VideoView view = (VideoView) findViewById(R.id.videoView); videoView.setVideoPath(Environment.getExternalStorageDirectory().toString() + "/test.mp4"); videoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { view.start(); } }); 中古車買取実績No.1ガリバー コメント: |