How to implement release function in soundpool?

試著忘記壹切 提交于 2019-12-11 10:35:20

问题


hy,,im newbie for android,i have error AudioFlinger could not create track, status: -12 when running my application. i read for use release() method for fix it, but i don't know how to implement on my code, please help me ?? this is my code,please fix it :)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.talempong);

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundID = spool.load(this, R.raw.doo, 1);

   ImageButton Button = (ImageButton)findViewById(R.id.imagebutton1);
    Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Sound();



        }

    });
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    spool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundID2 = spool2.load(this, R.raw.re, 1);

    ImageButton Button2 = (ImageButton)findViewById(R.id.imagebutton2);
    Button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Sound2();
        }
});

回答1:


Is your sound greater that 1MB? SoudPool doesn't plays sounds that are greater that that.

If it isn't you can use the release after playing the sound.

// your code:

this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.doo, 1);


//after this you have to: 

spool.setOnLoadCompleteListener(new OnLoadCompleteListener(){

@Override 
    public void onLoadComplete(SoundPool sound,int sampleId,int status){
        spool.play(teste, 20,20, 1, 0, 1);
        new Thread(new Runnable(){
@Override                       
     public void run(){
         try {
        Thread.sleep(2000);
                spool.release();
         } catch (InterruptedException e) { e.printStackTrace(); }
         }
        }).start();
    }
});

// in the Thread.sleep put the value in millisecs of the time of your music this is a sample os 2 seconds


来源:https://stackoverflow.com/questions/21027422/how-to-implement-release-function-in-soundpool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!