Soundpool sample not ready

后端 未结 8 1108
孤独总比滥情好
孤独总比滥情好 2020-12-09 03:15

I have a .wav file that I\'d like to use across my game, currently I am loading the sound in onCreate() of each activity in the game.

 soundCoun         


        
8条回答
  •  没有蜡笔的小新
    2020-12-09 03:44

    private SoundPool soundPool;
    private int my_sound;
    boolean loaded = false;
    

    // In the constructor

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    my_sound = soundPool.load(this, R.raw.blast_sound, 1);
    
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
           loaded = true;
        }
    });
    

    // then where ever you want to play the sound, type

    if (loaded) {
    soundPool.play(my_sound,  0.9f, 0.9f, 1, 0, 1f);
    }
    

提交回复
热议问题