Soundpool sample not ready

后端 未结 8 1054
孤独总比滥情好
孤独总比滥情好 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:54

    The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream which we simply can call preparation of Stream, that takes some times depending on size of sound file. And if we try to play the sound before that process is over, we get "sample x not ready" error.

    There are two solutions for this

    1. implement setOnLoadCompleteListener or
    2. wait arbitrary amount of time so that preparation is over

    and then play it Note that there is no exception for this condition or application is not crashing without any try-catch

    0 讨论(0)
  • 2020-12-09 03:55

    This would work for you definitely !

    public void playWavFile() {
    
        Thread streamThread = new Thread(new Runnable() {           
    
            @Override
            public void run() {                 
                SoundPool soundPool;
                final int wav;
                String path = "/mnt/sdcard/AudioRecorder/record.wav";
                soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC, 0);
                wav = soundPool.load(path, 1);
                soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                    @Override
                    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                        // TODO Auto-generated method stub
                        soundPool.play(wav,100, 100, 0, 0, 1f);
                    }
                });
    
            }        
        });
    
        streamThread.start();
    }   
    
    0 讨论(0)
提交回复
热议问题