最近涉及到一個需要Android播放視頻的界面,內容不多,直接上代碼。
先看布局文件act_video_play,裡面有一個VideoView組件:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ff000000"
- android:gravity="center" >
- <VideoView
- android:id="@+id/videoView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center" />
- </LinearLayout>
再來看activity代碼:
- package com.example.playmp4;
-
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnKeyListener;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.media.MediaPlayer.OnPreparedListener;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.DisplayMetrics;
- import android.view.KeyEvent;
- import android.widget.LinearLayout;
- import android.widget.LinearLayout.LayoutParams;
- import android.widget.MediaController;
- import android.widget.VideoView;
-
- public class PlayMP4 extends Activity {
-
- private VideoView videoView;
- private int play_progress;
- private String video_url;
- private String progress_Title;
- private Dialog progress;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.act_video_play);
-
- Intent intent = getIntent();
- video_url = intent.getStringExtra("video_url");
- progress_Title = intent.getStringExtra("title");
-
- // for test
- video_url = Environment.getExternalStorageDirectory().toString()
- + "/dcim/100MEDIA/VIDEO0002.mp4";
-
- if (this.progress_Title == null)
- this.progress_Title = "Loading";
-
- play_progress = intent.getIntExtra("play_progress", 0);
- videoView = (VideoView) findViewById(R.id.videoView);
- progress = ProgressDialog.show(this, "loading", progress_Title);
- progress.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- dialog.dismiss();
- PlayMP4.this.finish();
- }
- return false;
- }
- });
-
- videoView.setVideoURI(Uri.parse(video_url));
- MediaController controller = new MediaController(this);
- videoView.setMediaController(controller);
- videoView.requestFocus();
-
- videoView.setLayoutParams(new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
-
- videoView.setOnPreparedListener(new OnPreparedListener() {
- @Override
- public void onPrepared(MediaPlayer mp) {
- if (progress != null)
- progress.dismiss();
- }
- });
-
- videoView.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- if (progress != null)
- progress.dismiss();
- }
- });
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- Intent intent = new Intent();
- intent.putExtra("play_progress", videoView.getCurrentPosition());
- setResult(RESULT_OK, intent);
- }
- return super.onKeyDown(keyCode, event);
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- videoView.seekTo(play_progress);
- videoView.start();
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- if (videoView != null) {
- videoView.pause();
- play_progress = videoView.getCurrentPosition();
- }
- if (progress != null) {
- progress.dismiss();
- }
- }
- }
video_url這個就是播放的地址,可以是本地文件地址,也可以是網絡上的鏈接。
play_progress為播放進度。當這個播放界面關閉後,會返回該值給上一級界面。下次再播放同一個視頻時,就可以直接從上一次停止的位置播放。如何處理記錄播放位置,這個就根據應用情況,請自行設計,這裡就不多啰嗦了。
這個就不附工程了,代碼如上。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11