簡體   English   中英

Android SoundPool 不播放聲音

[英]Android SoundPool plays no sound

我想在使用 SoundPool 單擊按鈕時播放短促的聲音。 但是當點擊按鈕時沒有聲音出現,盡管日志告訴我它已加載並“正在播放”。 如果相關,聲音文件是 .ogg 。 這是我的代碼, buttonSound()onCreate()被調用。

void buttonSound(){
    AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();

    soundPool = new SoundPool.Builder().setAudioAttributes(attributes).build();

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
            int status) {
            Log.d("SOUNDPOOL", "COMPLETE "+button);
        }
    });
    button = soundPool.load(this, R.raw.button, 1);

}


public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button_back:
            // Sound loaded
            if(button != 0) {
                soundPool.play(button, 1f, 1f, 1, 0, 1f);
                Log.d("PLAYING", "PLAYING");
            }
            break;

所以顯然AudioAttributes.USAGE_ASSISTANCE_SONIFICATION是問題所在,它適用於AudioAttributes.USAGE_GAMEAudioAttributes.USAGE_MEDIA 不過,我不知道為什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM