我正在开发一个手机取景器应用程序,我想实现远程振铃功能...我已经为MediaPlayer编写了一个代码,但是当我测试它时,闹钟响了不停(也许是时间)警报歌太长了,但我想让它在特定时期内响起)...我希望设置一个响铃闹钟的定时器,就像让我说响10秒,但不知道如何实现它...需要你们的帮助......谢谢...
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(RingerActivity.this, R.raw.alarm);
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
Toast.makeText(this, e.getMessage() , Toast.LENGTH_SHORT).show(); }
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
答案 0 :(得分:2)
您可以使用Runnable和Handler在10秒后停止MediaPlayer。
Handler h = new Handler();
Runnable stopPlaybackRun = new Runnable() {
public void run(){
mp.stop();
mp.release();
}
};
h.postDelayed(stopPlaybackRun, 10 * 1000);
答案 1 :(得分:0)
<强>的Android 强>
计时器中的媒体播放器
MediaPlayer buzzer; //Outside the method
public void BuzzerSound(){
buzzer=MediaPlayer.create(MainActivity.this, R.raw.buzzer_sound);
Thread timer= new Thread(){
public void run(){
try{
buzzer.start();
buzzer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
};
timer.start(); }