Android音效SoundPool問題:soundpool 1 not retry


Android音效SoundPool問題:soundpool 1 not retry


今天開發中要用到SoundPool,遇到soundpool 1 not retry無法播放聲音,MediaPlay可以


后來經過一番研究,發現:

出現soundpool 1 not retry問題的代碼,無法播放聲音

mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
        //初始化soundPool 對象,第一個參數是允許有多少個聲音流同時播放,第2個參數是聲音類型,第三個參數是聲音的品質
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
        soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
        volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);

//loop:循環中的循環模式(0 =沒有循環,-1 =無限循環)
                soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

問題解決:這里的問題是soundPool.load(MainActivity.this, R.raw.or, 1),即 load() 聲音文件后,立馬 play() 播放,系統還沒有准備好聲音文件,所以才出了問題

這里需要你:先在其他地方load()好了,比如在構造函數里先load()好了,在需要播放的地方再調用play(),也就是要過一段時間再調用play()

這樣寫就沒問題

mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
        //初始化soundPool 對象,第一個參數是允許有多少個聲音流同時播放,第2個參數是聲音類型,第三個參數是聲音的品質
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
        soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
        volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM