2016-11-11 90 views
0

我使用SoundPool在我创建的街机游戏中播放声音。然而,playPassSound()从开始到结束需要大约40毫秒。我只构造1x SoundPlayer,它只加载一次所有声音。我在我的游戏线程中调用playPassSound(),该线程也处理渲染调用。SoundPool play()块渲染

这是我的声音播放类是什么样子(声音有关的一切只是一个封装):

public class SoundPlayer { 

private AudioAttributes audioAttributes; 
private final int SOUND_POOL_MAX = 2; 
private AudioManager audioManager; 
private SoundPool soundPool; 
private int soundIdPass; 
private float volume; 
private boolean passLoaded; 

public SoundPlayer(Context context){ 

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ 

     audioAttributes = new AudioAttributes.Builder() 
       .setUsage(AudioAttributes.USAGE_GAME) 
       .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 
       .build(); 
     soundPool = new SoundPool.Builder() 
       .setAudioAttributes(audioAttributes) 
       .setMaxStreams(SOUND_POOL_MAX) 
       .build(); 

    }else { 
     //SoundPool is deprecated as of API Level 21 (Lollipop) 
     soundPool = new SoundPool(SOUND_POOL_MAX, AudioManager.STREAM_MUSIC, 0); 
    } 

    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 

    soundIdPass = soundPool.load(context, R.raw.pass, 1); 

    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 

      if(sampleId == soundIdPass){ 
       passLoaded = true; 
     } 
    }); 
} 

public void playPassSound(){ 
    controlVolume(); 
    soundPool.play(soundIdPass, volume, volume, 1, 0, 1f); 
} 
public void controlVolume(){ 
    float actualVolume = (float) audioManager 
      .getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager 
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    volume = actualVolume/maxVolume; 
} 

}

为什么playPassSound()花了这么长时间?我怎样才能将它从我的游戏/渲染线程中分离出来,以便游戏不受它影响?

回答

0

只需使用Mediaplayer来代替...不要打扰调试这个。这里似乎没有任何真正的解决方案。破解只有this