1
我有2 SoundPool
實例,如果我運行我的應用程序,soundPool2
運行與soundPool1
相同的聲音,我不知道爲什麼這會發揮相同的聲音。任何人都可以幫我找到問題嗎?SoundPool沒有播放?
public class MainActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
private SoundPool soundPool2;
private int soundID;
private int soundID2;
boolean loaded = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button1);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);
View view2 = findViewById(R.id.button2);
view2.setOnTouchListener(this);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool2, int sampleId,
int status) {
loaded = true;
}
});
soundID2 = soundPool.load(this, R.raw.bird, 1);
}
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume/maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played ");
}
}
return false;
}
public boolean onTouch2(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume/maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}}
非常感謝老師,它的作品,我測試過它,完美! :),並運行找到..再次感謝,即時新手我必須學習更多。他@Tigger –
高興得到了幫助。 – Tigger
@Tigger嘿觸發我需要你的聲音池的幫助。這是一個簡單的代碼,請邀請我去聊天室。謝謝! –