2016-10-10 144 views
1

我試圖在我的活動中使用SoundPool播放聲音,但我無法聽到聲音。SoundPool不播放聲音

private void beginGame(){ 
     setContentView(R.layout.activity_splash1); 

     if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){ 
      AudioAttributes aa = new AudioAttributes.Builder() 
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 
        .setUsage(AudioAttributes.USAGE_GAME) 
        .build(); 


      sp = new SoundPool.Builder() 
        .setMaxStreams(10) 
        .setAudioAttributes(aa) 
        .build(); 
     }else{ 
      sp = new SoundPool(10, AudioManager.STREAM_MUSIC,1); 
     } 

     sid = sp.load(Splash1.this,R.raw.bubble,1); 
     sp.play(sid,1,1,1,0,0.99f); 

     Thread loading = new Thread() { 
      public void run() { 
       try { 
        sleep(2000); 
        // TODO NEXT ACTIVITY 
       } 

       catch (Exception e) { 
        e.printStackTrace(); 
       } 

       finally { 
        finish(); 
       } 
      } 
     }; 

     loading.start(); 
    } 
+0

是沒有錯誤執行或它會崩潰的應用程序? –

+0

不存在錯誤 –

+1

您使用哪個API級別進行測試? – 0X0nosugar

回答

2

首先,在項目我的工作,我有一個像[AnswerSound]下面的一個一類,所以我總是得到它的實例,並調用任何方法爲我所需要的。我希望你能從中看到和踢出。

public class AnswerSound { 
private final float volume; 
private Context context; 

private SoundPool correctAnswerSoundPool; 
private int correctAnswerSoundID; 
boolean correctLoaded = false; 


private SoundPool wrongAnswerSoundPool; 
private int wrongAnswerSoundID; 
boolean wrongLoaded = false; 
public AnswerSound(Context context) 
{ 
    this.context = context; 
    correctAnswerSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 10); 
    correctAnswerSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){ 
    @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
     correctLoaded = true; 
         } 
      } 
    ); 

    wrongAnswerSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 10); 

    wrongAnswerSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
    { 
    @Override 
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
     wrongLoaded = true; 
     } 
     } 

    ); 
    //assign the sound IDS 
    correctAnswerSoundID = correctAnswerSoundPool.load(context, R.raw.correct_answer, 1); 

    wrongAnswerSoundID = wrongAnswerSoundPool.load(context, R.raw.wrong_answer_sound_effect, 2); 

    //getting the user sound settings 
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 

    float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    volume = actualVolume/maxVolume; 

    //then you can play the sound by checking if the sound is loaded 
} 



public void playCorrectAnswerSound() 
{ 
    if (correctLoaded) { 
     correctAnswerSoundPool.play(correctAnswerSoundID, volume, volume, 1, 0, 1f); 
    } 

} 
public void playWrongAnswerSound() { 
    if (wrongLoaded) { 
     wrongAnswerSoundPool.play(wrongAnswerSoundID, volume, volume, 1, 0, 1f); 
    } 
} 

}

+0

多數民衆贊成在良好的但現在只是想一個簡單的解決方案:) –