- MediaPlayer播放通常的音频文件
- 程序示例:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filenameText = (EditText)this.findViewById(R.id.filename);
//对mediaPlayer进行实例化
mediaPlayer = new MediaPlayer();
ButtonClickListener listener = new ButtonClickListener();
//获取4个按钮
Button playButton = (Button)this.findViewById(R.id.play);
Button pauseButton = (Button)this.findViewById(R.id.pause);
Button resetButton = (Button)this.findViewById(R.id.reset);
Button stopButton = (Button) this.findViewById(R.id.stop);
//设置4个按钮的监听
playButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
resetButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
Log.i(TAG, "onCreate()");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
this.filename = savedInstanceState.getString("filename");
this.position = savedInstanceState.getInt("position");
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState()");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
outState.putString("filename", filename);
outState.putInt("position", position);
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState()");
}
private void play() throws IOException
{
//获取文件路径
File audioFile = new File(Environment.getExternalStorageDirectory(),filename);
mediaPlayer.reset();
mediaPlayer.setDataSource(audioFile.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
}
//如果突然电话到来,停止播放音乐
@Override
protected void onPause()
{
if(mediaPlayer.isPlaying())
{
//保存当前播放点
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}
//
@Override
protected void onResume()
{
//如果电话结束,继续播放音乐
if(position>0 && filename!=null)
{
try
{
play();
mediaPlayer.seekTo(position);
position = 0;
}
catch (IOException e)
{
Log.e(TAG, e.toString());
}
}
super.onResume();
}
//对mediaPlayer进行摧毁
@Override
protected void onDestroy()
{
mediaPlayer.release();
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
private final class ButtonClickListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
//先得到文本框中的内容
filename = filenameText.getText().toString();
//得到button
Button button = (Button) v;
try
{
//通过传过来的Buttonid可以判断Button的类型
switch (v.getId())
{
case R.id.play:
play();
break;
case R.id.pause:
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
button.setText(R.string.continue1);//让这个按钮上的文字显示为继续
}
else
{
mediaPlayer.start();//继续播放
button.setText(R.string.pause);
}
break;
case R.id.reset:
if(mediaPlayer.isPlaying())
{
mediaPlayer.seekTo(0);//让它从0开始播放
}
else
{
play();//如果它没有播放,就让它开始播放
}
break;
case R.id.stop:
//如果它正在播放的话,就让他停止
if(mediaPlayer.isPlaying())
mediaPlayer.stop();
break;
}
}
catch (Exception e)
{
Log.e(TAG, e.toString());
}
}
}
}
【2】使用SoundPool实现短促的音效
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的游戏开发中。
在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。
SoundPool(android.media.SoundPool),顾名思义是声音池的意思,主要用于播放一些较短的声音片段,支持从程序的资源或文件系统加载。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、播放比率等参数,支持通过ID对多个音频流进行管理。
- SoundPool存在的缺陷
1.SoundPool最大只能申请1M的内存空间,这就意味着我们只能用一些很短的声音片段,而不是用它来播放歌曲或者做游戏背景音乐。
2.SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用,因为有些时候它们可能会使你的程序莫名其妙的终止。建议使用这两个方法的时候尽可能多做测试工作,还有些朋友反映它们不会立即中止播放声音,而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。
3.SoundPool的效率问题。其实SoundPool的效率在这些播放类中算是很好的了,但是有的朋友在G1中测试它还是有100ms左右的延迟,这可能会影响用户体验。也许这不能管SoundPool本身,因为到了性能比较好的Droid中这个延迟就可以让人接受了。
在现阶段SoundPool有这些缺陷,但也有着它不可替代的优点,基于这些我们建议大在如下情况中多使用SoundPool:1.应用程序中的声效(按键提示音,消息等)2.游戏中密集而短暂的声音(如多个飞船同时爆炸)
- SoundPool使用方法
开发步骤:
1>往项目的res/raw目录中放入音效文件。
2>新建SoundPool对象,然后调用SoundPool.load()加载音效,调用SoundPool.play()方法播放指定音效文件。