2016-08-22 46 views
0

我試圖從片段播放聲音片段,但聲音未播放。我沒有在控制檯中發現任何錯誤,也沒有崩潰,只是沒有播放聲音。是的,我的音量已經上升,沒有靜音。所有相關代碼如下SoundPool在不能播放Android Studio的片段中

 int soundId; 
     int currentSound; 
     SoundPool sp; 

     boolean pressed; 
     boolean loaded; 

     sp = new SoundPool(5, AudioManager.STREAM_MUSIC,0); 
     currentSound = R.raw.whatarethose; 
     sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
      @Override 
      public void onLoadComplete(SoundPool soundPool, int i, int i1) { 
       loaded = true; 
      } 
     }); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(loaded) { 
        soundId = sp.load(getActivity(), currentSound, 1); 

        sp.play(soundId, 1, 1, 0, 0, 1); 
       } 
      } 
     }); 
+1

你是指在模擬器中?你可以在真實的設備上試用它嗎?我最近也遇到了模擬器不能播放聲音的問題,但是應用在真實設備上運行良好。 – ShadowGod

回答

0

您正在按鈕單擊中加載聲音。嘗試在設置OnCompleteListener之後加載聲音,然後單擊。

int soundId; 
    int currentSound; 
    SoundPool sp; 

    boolean pressed; 
    boolean loaded; 

    sp = new SoundPool(5, AudioManager.STREAM_MUSIC,0); 
    currentSound = R.raw.whatarethose; 
    sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int i, int i1) { 
      loaded = true; 
     } 
    }); 
    soundId = sp.load(getActivity(), currentSound, 1); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(loaded) { 
       sp.play(soundId, 1, 1, 0, 0, 1); 
      } 
     } 
    });