在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的游戏开发中。
在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。
SoundPool的使用步骤是 :
1.在res中新建raw文件夹,然后将需要播放的音频放入其中;
2.初始化SoundPool实例;
3.调用SoundPool的play函数进行播放。
几个重要的函数:
soundPool的构造函数
public SoundPool (int maxStreams, int streamType, int srcQuality)
参数:
maxStreams 同时最大播放音频个数
streamType 音频流的类型,通常使用 STREAM_MUSIC.
srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.
音效播放函数:
public void playSounds(int sound, int number){
//实例化AudioManager对象,控制声音
AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
//最大音量
float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//当前音量
float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
//播放
sp.play(spMap.get(sound), //声音资源
volumnRatio, //左声道
volumnRatio, //右声道
1, //优先级,0最低
number, //循环次数,0是不循环,-1是永远循环
1); //回放速度,0.5-2.0之间。1为正常速度
}
代码清单
主Activity:
package com.example.soundpool;
import java.util.HashMap;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SPActivity extends Activity {
private Button playBtn;
private Button pauseBtn;
private SoundPool sp;
private HashMap<Integer,Integer> spMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);
playBtn=(Button)findViewById(R.id.button1);
pauseBtn=(Button)findViewById(R.id.button2);
sp=new SoundPool(2,AudioManager.STREAM_MUSIC,0);
spMap = new HashMap<Integer,Integer>();
spMap.put(1, sp.load(this, R.raw.sound1, 1));
playBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
playSounds(1,1);
}
});
pauseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp.pause(spMap.get(1));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sp, menu);
return true;
}
public void playSounds(int sound, int number){
AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1);
}
}
布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="Play" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Pouse" />
</RelativeLayout>
遇到的一个小问题:
pause按钮在第一次播放的时候好用,第一次之后的播放就不管用了,网上找到的解释:
原来这个流对应的ID是需要play方法返回的,后来我用mPresentPlayId存储play返回的流ID,在stop时将流ID使用mPresentPlayId来替换就没问题了,后来输出了下mPresentPlayId的值,发现这个值第一次是2.第二次是4,以后使用这个方法一定要注意这个问题。
分享到:
相关推荐
Android下使用SoundPool的示例,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SoundPool.html
Android的SoundPool类使用与利弊
使用 SoundPool 播放音效,SoundPool 还支持自行设置声音的品质、音量、 播放比率等参数
Android中的SoundPool一般用于游戏中的多个音频文件中,以后有意向做游戏开发,或对多个音频播放有要求的可以研究下SoundPool。
Android SoundPool使用案例及源码分析
Android设备的一些较短的声音可以用SoundPool来做,如消息提醒等,此例子介绍了SoundPool的用法。
android soundpool播放音乐,使用soundpool播放音乐。
Android SoundPool音效使用Demo源码.rar
大家知道MediaPlayer占用的资源比较多,且不可以同时支持播放多个音频,所以我们有一种叫做SoundPool,比如我们常见的按键音或者是手机提示音,还比如我们在游戏的开发中会有大量的音效效果等,下边介绍一下她的用法...
主要为大家详细介绍了Android使用SoundPool实现播放音频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
摘要:Java源码,Android,Soundpool Android中的soundpool实例源码下载,播放音乐的例子,还是比较实用的,很基础的例子,几乎在Android多媒体中经常用到的。
本文实例为大家分享了Android使用SoundPool播放音效的具体代码,供大家参考,具体内容如下 SoundPool(int maxStreams, int streamType, int srcQuality) 参数依次是: ①指定支持多少个声音,SoundPool对象中允许...
主要为大家详细介绍了Android使用SoundPool实现播放音效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一个简单的sample , 用来理解及测试SoundPool的相关功能 相关博客 https://blog.csdn.net/qq_28261343/article/details/82818868
一个简单的sample , 用来理解及测试SoundPool的相关功能 https://blog.csdn.net/qq_28261343
资源为一个专门处理短音效soundPool的工具类,使用起来很方便,封装的很好!使用方法: 1、你只需要在需要的地方调用这个方法进行类初始化 private void initSound() { soundPlay = new SoundPlay(); soundPlay....
一个关于soundpool使用方法的小例子,帮助需要的朋友、