CountdownTimer上的媒体播放器错误(-19,0)

时间:2013-05-02 03:57:53

标签: android android-mediaplayer countdowntimer

我创建了一个程序,它在CountdownTimer上的每个onTick方法上重复调用媒体播放器

媒体播放器类

public class Buzzer extends Activity{
private MediaPlayer mp;
private AssetFileDescriptor descriptor;
private Context context;

public Buzzer(Context context){
    super();
    this.context=context;   

}
public void playBeep(){
    play("beep.mp3");
} 


public void stop(){
    if (mp.isPlaying()) {
        mp.stop();
        mp.release();
    }
}

private void play(String file){

    try {
         mp = new MediaPlayer();
        if (mp.isPlaying()) {
            mp.stop();
            mp.release();
            mp.reset();
            mp = new MediaPlayer();
        }

        descriptor= context.getAssets().openFd("audios/"+file);
        mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        mp.prepare();
        mp.start();

    } catch (IllegalArgumentException e) {
                e.printStackTrace();
    } catch (IllegalStateException e) {
                e.printStackTrace();
    } catch (IOException e) {
                e.printStackTrace();
    }

}
public void onResume(){
    super.onResume();
}

public void onDestroy(){
     mp.stop();
     mp.release();
     super.onDestroy();
 }
}

//主要活动

      public class Main extends Activity {
      Timer timer;
      Buzzer buzzer;


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


               buzzer=new Buzzer(Main.this);
               timer=new Timer(24000,1000);
               timer.start();

          }

  public class Timer extends CountDownTimer{

        public ShotClock_CountdownTimer(long millisInFuture, long countDownInterval) {
          super(millisInFuture, countDownInterval);
        }
        public void onTick (long millisUntilFinished) {

        buzzer.playBeep();  
        }
        public void onFinish() {
        }   

      } 
   }

3 个答案:

答案 0 :(得分:1)

你应该在每次调用playBeep之前释放媒体播放器。你需要释放()媒体播放器,否则资源不会被释放,你很快就会内存不足。

答案 1 :(得分:0)

public class Buzzer extends Activity{
private MediaPlayer mp;
private AssetFileDescriptor descriptor;
private Context context;

public Buzzer(Context context){
    super();
    this.context=context;   
    mp = MediaPlayer.create(this,R.raw.beep);
}
public void playBeep(){
    if(mp!=null ){
         mp.pause();
         mp.seekTo(0);
         mp.start();
    }
} 


public void stop(){
    if (mp.isPlaying()) {
        mp.pause()
    }
}


public void onResume(){
    super.onResume();
    playBeep();
}

public void onDestroy(){
     super.onDestroy();
     stop();
 }
}

答案 2 :(得分:0)

变化:

mp = new MediaPlayer();
if (mp.isPlaying()) {
    mp.stop();
    mp.release();
    mp.reset();
    mp = new MediaPlayer();
 }

为:

if(mp != null){
    if(mp.isPlaying()){
        mp.stop();
    }
    mp.release();
}else{
    mp = new MediaPlayer();
}

发生此错误是因为您的MediaPlayer处于错误状态。根据您当前的代码,您将在检查它是否正在播放之前创建一个新的MediaPlayer。此外,如果正在播放,您只会发布mp。你需要每次都释放它。