Android应用开发之简易视频播放器
Android应用开发之简易视频播放器
在Android中播放视频用到的也是MediaPlayer,展示视频通常使用SurfaceView控件。
在main.xml布局文件添加用于视频画面绘制的SurfaceView 控件:
<SurfaceView android:layout_width="fill_parent"android:layout_height="240dip"android:id="@+id/surfaceView"/>
MeidaPlayer播放视频相关API使用方法:
- SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
- surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
- /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
- surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- MediaPlayer mediaPlayer = new MediaPlayer();
- mediaPlayer.reset();//重置为初始状态
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- /* 设置Video影片以SurfaceHolder播放 */
- mediaPlayer.setDisplay(surfaceView.getHolder());
- mediaPlayer.setDataSource("/mnt/sdcard/oppo.mp4");
- mediaPlayer.prepare();//缓冲
- mediaPlayer.start();//播放
- mediaPlayer.pause();//暂停播放
- mediaPlayer.start();//恢复播放
- mediaPlayer.stop();//停止播放
- mediaPlayer.release();//释放资源
详细代码:
布局文件layout/main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#ffffff"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/filename"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="oppo.mp4"
- android:id="@+id/filename"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/play"
- android:id="@+id/play"
- />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/pause"
- android:id="@+id/pause"
- />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/reset"
- android:id="@+id/reset"
- />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/stop"
- android:id="@+id/stop"
- />
- </LinearLayout>
- <SurfaceView
- android:layout_width="fill_parent"
- android:layout_height="240dip"
- android:id="@+id/surfaceView"
- />
- </LinearLayout>
|
评论暂时关闭