本文介紹多媒體的音頻和視頻播放。
播放音頻和視頻的源文件可以是一下三種:
源文件(即res/raw下或assets下)
文件系統(即sdcard)
流媒體(即網絡)
1. 播放音頻文件
播放音頻文件可以用MediaPlayer類或SoundPool類。下面我們來介紹這兩個類。
1.1 MediaPlayer從源文件(即res/raw下或assets下)播放音頻
[java]
mMediaPlayer = MediaPlayer.create(this, R.raw.sleep_away);
mMediaPlayer.start();
mMediaPlayer = MediaPlayer.create(this, R.raw.sleep_away);
mMediaPlayer.start();
1.2 MediaPlayer從文件系統(即sdcard)播放音頻
[java]
String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "sleep_away.mp3";// /mnt/sdcard/sleep_away.mp3
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mMediaPlayer.start();
String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "sleep_away.mp3";// /mnt/sdcard/sleep_away.mp3
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mMediaPlayer.start();方法二:
[java]
String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "sleep_away.mp3";// /mnt/sdcard/sleep_away.mp3
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
mMediaPlayer.start();
String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "sleep_away.mp3";// /mnt/sdcard/sleep_away.mp3
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
mMediaPlayer.start();
1.3 MediaPlayer從流媒體(即網絡)播放音頻
添加權限<uses-permission android:name="android.permission.INTERNET" />
[java]
filePath = "http://www.xyz.com/Audio/sample.mp3";
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mMediaPlayer.start();
filePath = "http://www.xyz.com/Audio/sample.mp3";
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mMediaPlayer.start();方法二:
[java]
filePath = "http://www.xyz.com/Audio/sample.mp3";
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
mMediaPlayer.start();
filePath = "http://www.xyz.com/Audio/sample.mp3";
mMediaPlayer = MediaPlayer.create(this, Uri.parse(filePath));
mMediaPlayer.start();
另外,MediaPlayer的幾個方法介紹一下:
start():開始播放
pause():暫停播放
reset() + prepare() + start():重新播放(其實MediaPlayer的create方法會首次調用prepare()方法)
另外,在onDestroy()方法中必須釋放MediaPlayer的資源:
[java]
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
1.4 SoundPool播放音頻文件
開發Android軟件中我們可能經常需播放多媒體聲音文件,一般使用MediaPlayer類但該類占用資源較多,對於游戲等應用可能不是很適合,SoundPool類在SDK的android.media.SoundPool,顧名思義是聲音池的意思。主要播放一些較短的聲音片段,可以從程序的資源或文件系統加載,相對於MediaPlayer類可以做到使用較少的CPU資源和較短的反應延遲。
SoundPool和其他聲音播放類相比,其特點是可以自行設置聲音的品質、音量、播放比率等參等。並且它可以同時管理多個音頻流,每個流都有獨自的ID,對某個音頻流的管理都是通過ID進行的。
SoundPool基本使用方法:
創建一個SoundPool對象:new SoundPool(int maxStreams, int streamType, int srcQuality);
從資源或者文件載入音頻流:load(Context context, int resId, int priority);
播放聲音:play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
關於SoundPool的用法參見Demohttp://download.csdn.net/detail/tianjf0514/4895976
2. 播放視頻文件
播放視頻有兩種方法,一種是用VideoView播放,另一種是用SurfaceView播放。
2.1 VideoView播放方式可以用VideoView.setVideoURI()和VideoView.setVideoPath兩種方式播放視頻
從文件系統(即sdcard)
[java]
videoView.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + File.separator + "wild_life.mp4"));
videoView.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + File.separator + "wild_life.mp4"));
[java]
videoView.setVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "wild_life.mp4");
videoView.setVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "wild_life.mp4");
從流媒體(即網絡)
[java]
videoView.setVideoURI(Uri.parse("http://v.pps.tv/play_3445G7.html"));
videoView.setVideoURI(Uri.parse("http://v.pps.tv/play_3445G7.html"));
[java]
videoView.setVideoPath("http://v.pps.tv/play_3445G7.html");
videoView.setVideoPath("http://v.pps.tv/play_3445G7.html");
用VideoView的方式播放視頻很簡單,但是視頻的位置和大小不受控制,所以要下面來介紹另一種方式
2.2 SurfaceView播放方式播放視頻
SurfaceView的控制接口SurfaceHolder的setFixedSize方法可以設置視頻的大小。
具體的SurfaceView怎麼播放視頻參考Demohttp://download.csdn.net/detail/tianjf0514/4895976