首页 > 代码库 > Android 的 MediaPlayer 多媒体播放器

Android 的 MediaPlayer 多媒体播放器

MediaPlayer 可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用 MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。 SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。

标签:MediaPlayerSurfaceView Android SDK

[1].[图片] 程序截图 跳至[1][2][3]

技术分享

[2].[代码] main.xml 跳至[1][2][3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutandroid:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <SeekBarandroid:id="@+id/SeekBar01"android:layout_height="wrap_content"
        android:layout_width="fill_parent"></SeekBar>
    <LinearLayoutandroid:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"android:layout_height="wrap_content">
        <Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"
            android:layout_height="wrap_content"android:text="播放音频"></Button>
        <Buttonandroid:id="@+id/Button02"android:layout_width="wrap_content"
            android:layout_height="wrap_content"android:text="停止播放"></Button>
    </LinearLayout>
    <SeekBarandroid:id="@+id/SeekBar02"android:layout_height="wrap_content"
        android:layout_width="fill_parent"></SeekBar>
    <SurfaceViewandroid:id="@+id/SurfaceView01"
        android:layout_width="fill_parent"android:layout_height="250px"></SurfaceView>
    <LinearLayoutandroid:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"android:layout_height="wrap_content">
        <Buttonandroid:layout_width="wrap_content"
            android:layout_height="wrap_content"android:id="@+id/Button03"
            android:text="播放视频"></Button>
        <Buttonandroid:layout_width="wrap_content"
            android:layout_height="wrap_content"android:text="停止播放"android:id="@+id/Button04"></Button>
    </LinearLayout>
</LinearLayout>

[3].[代码] TestMedia.java 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.testMedia;
importjava.io.IOException;   importjava.util.Timer; importjava.util.TimerTask; importandroid.app.Activity;   importandroid.media.AudioManager; importandroid.media.MediaPlayer; importandroid.os.Bundle;   importandroid.view.SurfaceHolder; importandroid.view.SurfaceView; importandroid.view.View;   importandroid.widget.Button;   importandroid.widget.SeekBar; importandroid.widget.Toast;  
publicclass TestMedia extends Activity {
    /** Called when the activity is first created. */
    privateSeekBar skb_audio=null;
    privateButton btn_start_audio = null;  
    privateButton btn_stop_audio = null;
    privateSeekBar skb_video=null;
    privateButton btn_start_video = null;  
    privateButton btn_stop_video = null;
    privateSurfaceView surfaceView; 
    privateSurfaceHolder surfaceHolder; 
    
    privateMediaPlayer m = null;  
    privateTimer mTimer;
    privateTimerTask mTimerTask;
    
    privateboolean isChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突     @Override 
    publicvoid onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        
        //----------Media控件设置---------//         m=new MediaPlayer();
        
        //播放结束之后弹出提示         m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
            @Override
            public void onCompletion(MediaPlayer arg0) {
               Toast.makeText(testMedia.this,"结束", 1000).show();
               m.release();
            }
        });
        
      //----------定时器记录播放进度---------//        mTimer = newTimer();
        mTimerTask = newTimerTask() {
            @Override
            public void run() { 
               if(isChanging==true)
                   return;
                
               if(m.getVideoHeight()==0)
                   skb_audio.setProgress(m.getCurrentPosition());
               else
                   skb_video.setProgress(m.getCurrentPosition());
            }
        };
        mTimer.schedule(mTimerTask, 0,10);
        
        btn_start_audio = (Button) this.findViewById(R.id.Button01);  
        btn_stop_audio = (Button) this.findViewById(R.id.Button02);  
        btn_start_audio.setOnClickListener(newClickEvent());
        btn_stop_audio.setOnClickListener(newClickEvent());
        skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);
        skb_audio.setOnSeekBarChangeListener(newSeekBarChangeEvent());
        
        btn_start_video = (Button) this.findViewById(R.id.Button03);  
        btn_stop_video = (Button) this.findViewById(R.id.Button04);  
        btn_start_video.setOnClickListener(newClickEvent());
        btn_stop_video.setOnClickListener(newClickEvent());
        skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);
        skb_video.setOnSeekBarChangeListener(newSeekBarChangeEvent());
        surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setFixedSize(100,100);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }  
     
  /**
   * 按键事件处理
   */
  classClickEvent implementsView.OnClickListener{
    @Override
    publicvoid onClick(View v) {
        if(v==btn_start_audio)
        {
            m.reset();//恢复到未初始化的状态            m=MediaPlayer.create(testMedia.this, R.raw.big);//读取音频            skb_audio.setMax(m.getDuration());//设置SeekBar的长度            try {                   
               m.prepare();    //准备            } catch(IllegalStateException e) {         
               // TODO Auto-generated catch block                              e.printStackTrace();                
            } catch (IOException e) {           
               // TODO Auto-generated catch block                              e.printStackTrace();                
            }       
            m.start();  //播放         }
        else if(v==btn_stop_audio || v==btn_stop_video)
        {
            m.stop();
        }
        else if(v==btn_start_video)
        {
            m.reset();//恢复到未初始化的状态            m=MediaPlayer.create(testMedia.this, R.raw.test);//读取视频            skb_video.setMax(m.getDuration());//设置SeekBar的长度            m.setAudioStreamType(AudioManager.STREAM_MUSIC);
            m.setDisplay(surfaceHolder);//设置屏幕           
            try {
               m.prepare();
                
            } catch (IllegalArgumentException e) {
               // TODO Auto-generated catch block                e.printStackTrace();
            } catch (IllegalStateException e) {
               // TODO Auto-generated catch block                e.printStackTrace();
            } catch (IOException e) {
               // TODO Auto-generated catch block                e.printStackTrace();
            }
            m.start();
        }
    }
  }
  
  /**
   * SeekBar进度改变事件
   */
  classSeekBarChangeEvent implementsSeekBar.OnSeekBarChangeListener{
    @Override
    publicvoid onProgressChanged(SeekBar seekBar,int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub        
    }
    @Override
    publicvoid onStartTrackingTouch(SeekBar seekBar) {
        isChanging=true;
    }
    @Override
    publicvoid onStopTrackingTouch(SeekBar seekBar) {
        m.seekTo(seekBar.getProgress());
        isChanging=false;   
    }
      
  }
}

Android 的 MediaPlayer 多媒体播放器