Activity编程之视频播放界面
最近涉及到一个需要播放视频的界面,内容不多,直接上代码。
先看布局文件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" ><VideoViewandroid: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;@Overrideprotected 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 testvideo_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() {@Overridepublic 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() {@Overridepublic void onPrepared(MediaPlayer mp) {if (progress != null)progress.dismiss();}});videoView.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {if (progress != null)progress.dismiss();}});}@Overridepublic 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);}@Overrideprotected void onResume() {super.onResume();videoView.seekTo(play_progress);videoView.start();}@Overrideprotected void onStop() {super.onStop();if (videoView != null) {videoView.pause();play_progress = videoView.getCurrentPosition();}if (progress != null) {progress.dismiss();}}}
video_url这个就是播放的地址,可以是本地文件地址,也可以是网络上的链接。
play_progress为播放进度。当这个播放界面关闭后,会返回该值给上一级界面。下次再播放同一个视频时,就可以直接从上一次停止的位置播放。如何处理记录播放位置,这个就根据应用情况,请自行设计,这里就不多嗦了。
这个就不附工程了,代码如上。