Android应用开发之简易视频播放器_Linux编程_Linux公社-Linux系统门户网站

在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(176144);  //设置分辨率

 

 

  • /*下面设置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是,视频播放界面会是黑屏。

 

 

Android应用开发之简易视频播放器
详细代码:

布局文件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

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <resources>
  3.     <string name=“hello”>Hello World, PlayActivity!</string>
  4.     <string name=“app_name”>视频播放器</string>
  5.     <string name=“filename”>视频文件www.linuxidc.com</string>
  6.     <string name=“sdcarderror”>SDCard不存在</string>
  7. </resources>

Activity

  1. package com.Android.media;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.media.AudioManager;
  6. import android.media.MediaPlayer;
  7. import android.os.Bundle;
  8. import android.os.Environment;
  9. import android.util.Log;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceView;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.ImageButton;
  15. import android.widget.Toast;
  16. public class PlayerActivity extends Activity {
  17.     private static final String TAG = “PlayerActivity”;
  18.     private EditText filenameText;
  19.     private MediaPlayer mediaPlayer;
  20.     private SurfaceView surfaceView;
  21.     private String filename;
  22.     private int position;
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.main);
  27.         filenameText = (EditText)this.findViewById(R.id.filename);
  28.         surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
  29.         surfaceView.getHolder().setFixedSize(176144);//设置分辨率
  30.         surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  31.         surfaceView.getHolder().addCallback(new SurfaceCallback());
  32.         mediaPlayer = new MediaPlayer();
  33.         ButtonClickListener listener = new ButtonClickListener();
  34.         ImageButton playButton = (ImageButton)this.findViewById(R.id.play);
  35.         ImageButton pauseButton = (ImageButton)this.findViewById(R.id.pause);
  36.         ImageButton resetButton = (ImageButton)this.findViewById(R.id.reset);
  37.         ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);
  38.         playButton.setOnClickListener(listener);
  39.         pauseButton.setOnClickListener(listener);
  40.         resetButton.setOnClickListener(listener);
  41.         stopButton.setOnClickListener(listener);
  42.     }
  43.     private final class SurfaceCallback implements SurfaceHolder.Callback{
  44.         @Override
  45.         public void surfaceCreated(SurfaceHolder holder) {
  46.             if(position>0 && filename!=null){
  47.                 try {
  48.                     play();
  49.                     mediaPlayer.seekTo(position);
  50.                     position = 0;
  51.                 } catch (IOException e) {
  52.                     Log.e(TAG, e.toString());
  53.                 }
  54.             }
  55.         }
  56.         @Override
  57.         public void surfaceChanged(SurfaceHolder holder, int format, int width,
  58.                 int height) {
  59.         }
  60.         @Override
  61.         public void surfaceDestroyed(SurfaceHolder holder) {
  62.             if(mediaPlayer.isPlaying()){
  63.                 position = mediaPlayer.getCurrentPosition();
  64.                 mediaPlayer.stop();
  65.             }
  66.         }
  67.     }
  68.     private final class ButtonClickListener implements View.OnClickListener{
  69.         @Override
  70.         public void onClick(View v) {
  71.             if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  72.                 Toast.makeText(PlayerActivity.this, R.string.sdcarderror, 1).show();
  73.                 return;
  74.             }
  75.             filename = filenameText.getText().toString();
  76.             try {
  77.                 switch (v.getId()) {
  78.                 case R.id.play:
  79.                     play();
  80.                     break;
  81.                 case R.id.pause:
  82.                     if(mediaPlayer.isPlaying()){
  83.                         mediaPlayer.pause();
  84.                     }else{
  85.                         mediaPlayer.start();
  86.                     }
  87.                     break;
  88.                 case R.id.reset:
  89.                     if(mediaPlayer.isPlaying()){
  90.                         mediaPlayer.seekTo(0);
  91.                     }else{
  92.                         play();
  93.                     }
  94.                     break;
  95.                 case R.id.stop:
  96.                     if(mediaPlayer.isPlaying()) mediaPlayer.stop();
  97.                     break;
  98.                 }
  99.             } catch (Exception e) {
  100.                 Log.e(TAG, e.toString());
  101.             }
  102.         }
  103.     }
  104.     private void play() throws IOException {
  105.         File videoFile = new File(Environment.getExternalStorageDirectory(), filename);
  106.         mediaPlayer.reset();//重置为初始状态
  107.         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  108.         /* 设置Video影片以SurfaceHolder播放 */
  109.         mediaPlayer.setDisplay(surfaceView.getHolder());
  110.         mediaPlayer.setDataSource(videoFile.getAbsolutePath());
  111.         mediaPlayer.prepare();//缓冲              
  112.         mediaPlayer.start();//播放
  113.     }
  114. }

 

来源URL:http://www.linuxidc.com/Linux/2012-03/55565.htm