MediaPlayer 可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用 MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。 SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。
片] 程序截图
![](http://static.oschina.net/uploads/code/201011/08212145_Jg0M.jpg)
[代码] main.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout android:id = "@+id/LinearLayout01" |
03 |
android:layout_width = "fill_parent" android:layout_height = "fill_parent" |
04 |
xmlns:android = "http://schemas.android.com/apk/res/android" |
05 |
android:orientation = "vertical" > |
06 |
< SeekBar android:id = "@+id/SeekBar01" android:layout_height = "wrap_content" |
07 |
android:layout_width = "fill_parent" ></ SeekBar > |
08 |
< LinearLayout android:id = "@+id/LinearLayout02" |
09 |
android:layout_width = "wrap_content" android:layout_height = "wrap_content" > |
10 |
< Button android:id = "@+id/Button01" android:layout_width = "wrap_content" |
11 |
android:layout_height = "wrap_content" android:text = "播放音频" ></ Button > |
12 |
< Button android:id = "@+id/Button02" android:layout_width = "wrap_content" |
13 |
android:layout_height = "wrap_content" android:text = "停止播放" ></ Button > |
15 |
< SeekBar android:id = "@+id/SeekBar02" android:layout_height = "wrap_content" |
16 |
android:layout_width = "fill_parent" ></ SeekBar > |
18 |
< SurfaceView android:id = "@+id/SurfaceView01" |
19 |
android:layout_width = "fill_parent" android:layout_height = "250px" ></ SurfaceView > |
20 |
< LinearLayout android:id = "@+id/LinearLayout02" |
21 |
android:layout_width = "wrap_content" android:layout_height = "wrap_content" > |
22 |
< Button android:layout_width = "wrap_content" |
23 |
android:layout_height = "wrap_content" android:id = "@+id/Button03" |
24 |
android:text = "播放视频" ></ Button > |
25 |
< Button android:layout_width = "wrap_content" |
26 |
android:layout_height = "wrap_content" android:text = "停止播放" android:id = "@+id/Button04" ></ Button > |
[代码] TestMedia.java
001 |
package com.testMedia; |
003 |
import java.io.IOException; |
004 |
import java.util.Timer; |
005 |
import java.util.TimerTask; |
006 |
import android.app.Activity; |
007 |
import android.media.AudioManager; |
008 |
import android.media.MediaPlayer; |
009 |
import android.os.Bundle; |
010 |
import android.view.SurfaceHolder; |
011 |
import android.view.SurfaceView; |
012 |
import android.view.View; |
013 |
import android.widget.Button; |
014 |
import android.widget.SeekBar; |
015 |
import android.widget.Toast; |
018 |
public class TestMedia extends Activity { |
019 |
/** Called when the activity is first created. */ |
021 |
private SeekBar skb_audio= null ; |
022 |
private Button btn_start_audio = null ; |
023 |
private Button btn_stop_audio = null ; |
025 |
private SeekBar skb_video= null ; |
026 |
private Button btn_start_video = null ; |
027 |
private Button btn_stop_video = null ; |
028 |
private SurfaceView surfaceView; |
029 |
private SurfaceHolder surfaceHolder; |
031 |
private MediaPlayer m = null ; |
032 |
private Timer mTimer; |
033 |
private TimerTask mTimerTask; |
035 |
private boolean isChanging= false ; |
037 |
public void onCreate(Bundle savedInstanceState) { |
038 |
super .onCreate(savedInstanceState); |
039 |
setContentView(R.layout.main); |
045 |
m.setOnCompletionListener( new MediaPlayer.OnCompletionListener(){ |
047 |
public void onCompletion(MediaPlayer arg0) { |
048 |
Toast.makeText(testMedia. this , "结束" , 1000 ).show(); |
054 |
mTimer = new Timer(); |
055 |
mTimerTask = new TimerTask() { |
061 |
if (m.getVideoHeight()== 0 ) |
062 |
skb_audio.setProgress(m.getCurrentPosition()); |
064 |
skb_video.setProgress(m.getCurrentPosition()); |
068 |
mTimer.schedule(mTimerTask, 0 , 10 ); |
070 |
btn_start_audio = (Button) this .findViewById(R.id.Button01); |
071 |
btn_stop_audio = (Button) this .findViewById(R.id.Button02); |
072 |
btn_start_audio.setOnClickListener( new ClickEvent()); |
073 |
btn_stop_audio.setOnClickListener( new ClickEvent()); |
074 |
skb_audio=(SeekBar) this .findViewById(R.id.SeekBar01); |
075 |
skb_audio.setOnSeekBarChangeListener( new SeekBarChangeEvent()); |
077 |
btn_start_video = (Button) this .findViewById(R.id.Button03); |
078 |
btn_stop_video = (Button) this .findViewById(R.id.Button04); |
079 |
btn_start_video.setOnClickListener( new ClickEvent()); |
080 |
btn_stop_video.setOnClickListener( new ClickEvent()); |
081 |
skb_video=(SeekBar) this .findViewById(R.id.SeekBar02); |
082 |
skb_video.setOnSeekBarChangeListener( new SeekBarChangeEvent()); |
083 |
surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01); |
084 |
surfaceHolder = surfaceView.getHolder(); |
085 |
surfaceHolder.setFixedSize( 100 , 100 ); |
086 |
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
092 |
class ClickEvent implements View.OnClickListener{ |
094 |
public void onClick(View v) { |
095 |
if (v==btn_start_audio) |
098 |
m=MediaPlayer.create(testMedia. this , R.raw.big); |
099 |
skb_audio.setMax(m.getDuration()); |
102 |
} catch (IllegalStateException e) { |
105 |
} catch (IOException e) { |
111 |
else if (v==btn_stop_audio || v==btn_stop_video) |
115 |
else if (v==btn_start_video) |
118 |
m=MediaPlayer.create(testMedia. this , R.raw.test); |
119 |
skb_video.setMax(m.getDuration()); |
120 |
m.setAudioStreamType(AudioManager.STREAM_MUSIC); |
121 |
m.setDisplay(surfaceHolder); |
126 |
} catch (IllegalArgumentException e) { |
129 |
} catch (IllegalStateException e) { |
132 |
} catch (IOException e) { |
144 |
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{ |
147 |
public void onProgressChanged(SeekBar seekBar, int progress, |
154 |
public void onStartTrackingTouch(SeekBar seekBar) { |
159 |
public void onStopTrackingTouch(SeekBar seekBar) { |
160 |
m.seekTo(seekBar.getProgress()); |