如何在声池中实现释放功能



hy,,我是安卓新手,我有错误 运行我的应用程序时AudioFlinger could not create track, status: -12。我阅读了使用release()修复它的方法,但我不知道如何在我的代码上实现,请帮助我?这是我的代码,请修复:)

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();
        }
});

你的声音大于 1MB 吗?SoudPool不会播放比这更大的声音。

如果不是,您可以在播放声音后使用释放。

// 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

最新更新