Android:Button Click上的Random Sound FX

时间:2011-10-02 17:37:30

标签: android button random soundpool

我正在尝试使用soundpool在按钮单击时播放随机声音FX。这适用于模拟器,但不适用于我的设备。有线索吗?

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;
                    }
    });
}
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。我使用MP3文件,显然,Android 2.0+仅适用于OGG文件。将所有这些从MP3转换为OGG修复了它。一切都很好!