main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="播放影片" />
<VideoView
android:id="@+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
PlayVideo2Activitypublic class PlayVideo2Activity extends Activity implements OnPreparedListener { private static final String TAG = "PlayVideo2Activity"; private static final int REQUEST_CODE = 1; private VideoView vv; private MediaController mc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.vv = (VideoView) this.findViewById(R.id.vv); // 設定影片控制面板 this.mc = new MediaController(this); this.vv.setMediaController(this.mc); // 呼叫 VideoView.setVideoURI() 之後,會觸發 this.vv.setOnPreparedListener(this); } public void onClick(View v) { // 挑選影片 Intent it = new Intent(Intent.ACTION_GET_CONTENT); it.setType("video/*"); this.startActivityForResult(it, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent it) { super.onActivityResult(requestCode, resultCode, it); if (resultCode != RESULT_OK) { Log.e(TAG, "選檔失敗"); return; } if (requestCode != REQUEST_CODE) { Log.d(TAG, "不是選檔"); return; } Uri uri = it.getData(); Log.d(TAG, "取得影片:" + uri.getPath()); Log.d(TAG, "將影片路徑傳給 VideoView"); this.vv.setVideoURI(uri); } @Override public void onPrepared(MediaPlayer mp) { Log.d(TAG, "取得影片路徑,立即播放"); // 若用傳進來的 MediaPlayer 播放,沒有控制面板 // mp.start(); vv.start(); } }
相關文章
沒有留言:
張貼留言