0

我创建了一个程序,它在 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() {
        }   

      } 
   }
4

3 回答 3

1

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

于 2013-05-02T04:48:13.020 回答
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();
 }
}
于 2013-05-02T04:48:25.757 回答
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在播放时才释放。你需要每次都释放它。

于 2013-05-02T05:17:28.713 回答