package com.kerry.demo;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Demo1Activity extends Activity implements OnClickListener{
Button buttons [] = new Button[6] ;
SoundPool soundPool;
int streamId;
float volume ;
// Map hashMap = new HashMap<integer, integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSoundPool();
buttons[0] = (Button) this.findViewById(R.id.button1);
buttons[1] = (Button) this.findViewById(R.id.button2);
buttons[2] = (Button) this.findViewById(R.id.button3);
buttons[3] = (Button) this.findViewById(R.id.button4);
buttons[4] = (Button) this.findViewById(R.id.button5);
buttons[5] = (Button) this.findViewById(R.id.button6);
for(Button bt :buttons){
bt.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
playSound(1);
Toast.makeText(this,"button1",0).show();
;break;
case R.id.button2:
playSound(2);
Toast.makeText(this,"button2",0).show();
;break;
case R.id.button3:
Toast.makeText(this,"button3",0).show();
pauseSound();
;break;
case R.id.button4:
stopSound();
;break;
case R.id.button5:
playSound(5);
;break;
case R.id.button6:;break;
}
}
public void initSoundPool(){
soundPool = new SoundPool(1, //同事播放音效的数量
AudioManager.STREAM_MUSIC, //设置音频类型
0); //为以后留得参数 展示设置为0 即可
streamId = soundPool.load(this,R.raw.noitem,1);
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
int streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volume = streamVolume/streamMaxVolume;
}
public void playSound(int mumber){
soundPool.play(
streamId, //声音资源文件
volume, //左声道
volume, //右声道
1, // 优先级 0 为最低
mumber, //播放次数
1); //0.5~2.0 播放速度
}
public void stopSound(){
soundPool.stop(streamId);
}
public void pauseSound(){
soundPool.pause(streamId);
}
}
分享到:
相关推荐
收集到的一个非常好的安卓组件播放声音的小实例,区分了MediaPlayer和SoundPool两种方法的不同用法,会给初学者带来很大的帮助
使用 SoundPool 同时播放多个音频 具体详情请参见 http://blog.csdn.net/aduovip/article/details/14695779
Soundpool音乐播放控制练习
使用 SoundPool 播放音效,SoundPool 还支持自行设置声音的品质、音量、 播放比率等参数
Android下使用SoundPool的示例,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SoundPool.html
soundPool在Android5.0后Api有了改变,这个Demo就是为解决这种变化的,它可以在Android4.+----7.+系统正常播放音频文件!
Android中的SoundPool一般用于游戏中的多个音频文件中,以后有意向做游戏开发,或对多个音频播放有要求的可以研究下SoundPool。
Android设备的一些较短的声音可以用SoundPool来做,如消息提醒等,此例子介绍了SoundPool的用法。
简单的音频播放实例,如果音频较大建议实用全体,这个SoundPool非常轻量级,比较适用于较小的音频播放
android soundpool播放音乐,使用soundpool播放音乐。
4-15-2(SoundPool音效).7z
Android的SoundPool类使用与利弊
资源为一个专门处理短音效soundPool的工具类,使用起来很方便,封装的很好!使用方法: 1、你只需要在需要的地方调用这个方法进行类初始化 private void initSound() { soundPlay = new SoundPlay(); soundPlay....
即时音效, 通过android.media.SoundPool实现 具体请参见 http://blog.csdn.net/aduovip/article/details/9026567
android安卓app音频播放方式 MediaPlayer与SoundPool的区别.zip
SoundPool使用,主要实现了SoundPool加载、播放、释放声音资源的完整过程
一个关于soundpool使用方法的小例子,帮助需要的朋友、
一个简单的sample , 用来理解及测试SoundPool的相关功能 https://blog.csdn.net/qq_28261343
soundPool在Android5.0后Api有了改变,这个Demo就是为解决这种变化的,它可以在Android4.+----7.+系统正常播放音频文件!
一个简单的sample , 用来理解及测试SoundPool的相关功能 相关博客 https://blog.csdn.net/qq_28261343/article/details/82818868