如何在Android中发挥了良好的效果 [英] How to play a Sound Effect in Android
问题描述
我希望做一个很简单的一件code的,发挥了良好的效果。到目前为止,我有这个code:
I'm looking to do a very simple piece of code that plays a sound effect. So far I have this code:
SoundManager snd;
int combo;
private void soundSetup() {
// Create an instance of the sound manger
snd = new SoundManager(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the samples from res/raw
combo = snd.load(R.raw.combo);
}
private void playSound() {
soundSetup();
snd.play(combo);
}
然而,出于某种原因,当我使用 playSound()
方法,没有任何反应。音频文件是在正确的位置。
However, for some reason when I use the playSound()
method, nothing happens. The audio file is in the correct location.
推荐答案
有没有你正在使用一个特定的原因 SoundManager类
?我会用的MediaPlayer
相反,这里是Android的文档
Is there a specific reason you are using SoundManager
? I would use MediaPlayer
instead, here is a link to the Android Docs
<一个href=\"http://developer.android.com/reference/android/media/MediaPlayer.html\">http://developer.android.com/reference/android/media/MediaPlayer.html
那么它的那样简单。
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.combo);
mp.start();
请了RES /目录下一个名为原始/目录中。拖动WAV或MP3文件到原始/目录。从如上的任何地方播放它们。
Make a directory called "raw/" under the "res/" directory. Drag wav or mp3 files into the raw/ directory. Play them from anywhere as above.
这篇关于如何在Android中发挥了良好的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!