在Android中播放视频用到的也是MediaPlayer,展示视频通常使用SurfaceView控件。比较全面,布局文件,java文件,字符串文件都有了。
在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();//释放资源
下面介绍一个播放视频的简易例子,在模拟器上运行时最好用2.1, 2.1以后的模拟器播放视频时会有问题,视频播放同样需要处理一些来电类的情况,需要覆写Activity生命周期里的几个方法,需要注意的是,当Surface控件被其他Activity遮挡住是,会被销毁,所以需要为他写一个回调函数,否则当当前Activity被其他Activity打断后,又回到当前Activity是,视频播放界面会是黑屏。
布局文件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>
数据文件values/strings.xml
- <?xml version=“1.0” encoding=“utf-8”?>
- <resources>
- <string name=“hello”>Hello World, PlayActivity!</string>
- <string name=“app_name”>视频播放器</string>
- <string name=“filename”>视频文件www.linuxidc.com</string>
- <string name=“sdcarderror”>SDCard不存在</string>
- </resources>
Activity
- package com.Android.media;
- import java.io.File;
- import java.io.IOException;
- import android.app.Activity;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.Toast;
- public class PlayerActivity extends Activity {
- private static final String TAG = “PlayerActivity”;
- private EditText filenameText;
- private MediaPlayer mediaPlayer;
- private SurfaceView surfaceView;
- private String filename;
- private int position;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- filenameText = (EditText)this.findViewById(R.id.filename);
- surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
- surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
- surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- surfaceView.getHolder().addCallback(new SurfaceCallback());
- mediaPlayer = new MediaPlayer();
- ButtonClickListener listener = new ButtonClickListener();
- ImageButton playButton = (ImageButton)this.findViewById(R.id.play);
- ImageButton pauseButton = (ImageButton)this.findViewById(R.id.pause);
- ImageButton resetButton = (ImageButton)this.findViewById(R.id.reset);
- ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);
- playButton.setOnClickListener(listener);
- pauseButton.setOnClickListener(listener);
- resetButton.setOnClickListener(listener);
- stopButton.setOnClickListener(listener);
- }
- private final class SurfaceCallback implements SurfaceHolder.Callback{
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if(position>0 && filename!=null){
- try {
- play();
- mediaPlayer.seekTo(position);
- position = 0;
- } catch (IOException e) {
- Log.e(TAG, e.toString());
- }
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- if(mediaPlayer.isPlaying()){
- position = mediaPlayer.getCurrentPosition();
- mediaPlayer.stop();
- }
- }
- }
- private final class ButtonClickListener implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- Toast.makeText(PlayerActivity.this, R.string.sdcarderror, 1).show();
- return;
- }
- filename = filenameText.getText().toString();
- try {
- switch (v.getId()) {
- case R.id.play:
- play();
- break;
- case R.id.pause:
- if(mediaPlayer.isPlaying()){
- mediaPlayer.pause();
- }else{
- mediaPlayer.start();
- }
- break;
- case R.id.reset:
- if(mediaPlayer.isPlaying()){
- mediaPlayer.seekTo(0);
- }else{
- play();
- }
- break;
- case R.id.stop:
- if(mediaPlayer.isPlaying()) mediaPlayer.stop();
- break;
- }
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- }
- }
- }
- private void play() throws IOException {
- File videoFile = new File(Environment.getExternalStorageDirectory(), filename);
- mediaPlayer.reset();//重置为初始状态
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- /* 设置Video影片以SurfaceHolder播放 */
- mediaPlayer.setDisplay(surfaceView.getHolder());
- mediaPlayer.setDataSource(videoFile.getAbsolutePath());
- mediaPlayer.prepare();//缓冲
- mediaPlayer.start();//播放
- }
- }