开发者

Android: Random Sound FX on Button Click

I'm trying to play a random sound FX on开发者_StackOverflow社区 a button click using the soundpool. This works in the emulator but not on my device. Any clues?

public class enc extends Activity {   
private static final String TAG = "MyActivity";

private SoundPool soundPool;
private HashMap<Integer, Integer> soundsMap;
int SOUND1=1;
int SOUND2=2; 
int SOUND3=3;
int SOUND4=4;
int SOUND5=5;
int SOUND6=6;
int SOUND7=7;
int SOUND8=8;
int fSpeed = 1;

Random random = new Random();
int hit = random.nextInt(6)+1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //SoundPool
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    soundsMap = new HashMap<Integer, Integer>();
    soundsMap.put(SOUND1, soundPool.load(this, R.raw.hit1, 1));
    soundsMap.put(SOUND2, soundPool.load(this, R.raw.hit2, 1));
    soundsMap.put(SOUND3, soundPool.load(this, R.raw.hit3, 1));
    soundsMap.put(SOUND4, soundPool.load(this, R.raw.hit4, 1));
    soundsMap.put(SOUND5, soundPool.load(this, R.raw.hit5, 1));
    soundsMap.put(SOUND6, soundPool.load(this, R.raw.hit6, 1));
    soundsMap.put(SOUND7, soundPool.load(this, R.raw.spell, 1));
    soundsMap.put(SOUND8, soundPool.load(this, R.raw.kill, 1));


    setContentView(R.layout.enc);

}


public void setButtonClickListener() {
    Button wepb = (Button)findViewById(R.id.uwep);
    wepb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i(TAG, "----------------------------SFX: " + hit);
            playSound(hit, fSpeed);
            hit = random.nextInt(6)+1;
                    }
    });
}
}


I found the problem. I was using MP3 files and apparently, Android 2.0+ only works with OGG files. Converting all of them from MP3 to OGG fixed it. All is well!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜