2011-10-02 107 views
0

我正在嘗試使用soundpool播放按鈕上的隨機聲音效果。這適用於模擬器,但不適用於我的設備。任何線索?Android:按鈕上的隨機聲音效果點擊

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

當你說它在設備上不起作用時,你是什麼意思? – slayton

+0

當我將其安裝在手機上時,聲音無法播放。如果我從池中調用一個聲音而不使用隨機整數,它就可以正常工作。當我嘗試使用mediaplayer時,我遇到了同樣的問題 – cerealspiller

+0

隨機數是否正確生成?你可以發佈日誌消息嗎? – slayton

回答

0

我發現了這個問題。我使用的是MP3文件,顯然,Android 2.0+僅適用於OGG文件。將它們全部從MP3轉換爲OGG將其固定。一切都很好!