MediaPlayer主要用于播放音频,因此它没有提供图像输出界面,所以要借助于SurfaceView来显示MediaPalyer播放的图像输出。
SurfaceHolder是一个接口,其作用就像一个关于Surface的监听器。提供访问和控制SurfaceView背后的Surface 相关的方法 (providingaccess and control over this SurfaceView's underlying surface),它通过三个回调方法,让我们可以感知到Surface的创建、销毁或者改变。在SurfaceView中有一个方法getHolder,可以很方便地获得SurfaceView所对应的Surface所对应的SurfaceHolder
1 package com.example.day19_01videoplayer; 2 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.view.SurfaceHolder; 10 import android.view.SurfaceView; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 15 private SurfaceHolder holder; 16 private MediaPlayer mediaPlayer; 17 private final int STATE_IDLE = 0; 18 private final int STATE_PLAYING = 1; 19 private final int STATE_PAUSED = 2; 20 private final int STATE_STOP =3; 21 22 private int current_state = STATE_IDLE; 23 private String path; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.sf_video); 31 holder = surfaceView.getHolder(); 32 path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/sirendingzhi.mp4"; 33 34 } 35 36 public void play(View v){ 37 38 if (mediaPlayer==null) { 39 mediaPlayer = new MediaPlayer(); 40 } 41 42 if (current_state==STATE_PAUSED) { 43 mediaPlayer.start(); 44 } 45 46 if (current_state==STATE_IDLE||current_state==STATE_STOP) { 47 mediaPlayer.reset(); 48 try { 49 mediaplay(); 50 } catch (Exception e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 } 55 56 current_state=STATE_PLAYING; 57 58 59 } 60 61 private void mediaplay() throws IOException { 62 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 63 mediaPlayer.setDisplay(holder); 64 mediaPlayer.setDataSource(path); 65 mediaPlayer.prepare(); 66 mediaPlayer.start(); 67 68 } 69 70 public void pause(View v){ 71 if (current_state==STATE_PLAYING) { 72 mediaPlayer.pause(); 73 current_state=STATE_PAUSED; 74 } 75 } 76 77 public void stop(View v){ 78 if (mediaPlayer!=null) { 79 if (current_state==STATE_PLAYING||current_state==STATE_PAUSED) { 80 mediaPlayer.stop(); 81 mediaPlayer.reset(); 82 current_state=STATE_STOP; 83 } 84 } 85 86 } 87 88 public void replay(View v){ 89 90 if (current_state!=STATE_IDLE) { 91 if (current_state==STATE_STOP) { 92 93 } 94 95 if (current_state==STATE_PLAYING||current_state==STATE_PAUSED) { 96 mediaPlayer.stop(); 97 mediaPlayer.reset(); 98 } 99 try { 100 mediaplay(); 101 } catch ( Exception e) { 102 // TODO Auto-generated catch block 103 e.printStackTrace(); 104 } 105 current_state=STATE_PLAYING; 106 } 107 } 108 109 @Override 110 protected void onDestroy() { 111 // TODO Auto-generated method stub 112 super.onDestroy(); 113 114 if (mediaPlayer!=null) { 115 mediaPlayer.stop(); 116 mediaPlayer.reset(); 117 mediaPlayer.release(); 118 } 119 } 120 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.day19_01videoplayer.MainActivity" 10 android:orientation="vertical"> 11 12 <LinearLayout 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal" > 16 <Button 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="播放" 20 android:onClick="play" /> 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="暂停" 25 android:onClick="pause" /> 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="停止" 30 android:onClick="stop" /> 31 <Button 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="重播" 35 android:onClick="replay" /> 36 </LinearLayout> 37 38 <SurfaceView 39 android:id="@+id/sf_video" 40 android:layout_width="fill_parent" 41 android:layout_height="fill_parent" 42 /> 43 44 </LinearLayout >