[英]How to set repeating notification sound for certain time in android?
I want to create a custom notification using NotificationComapat.Builder
. 我想使用NotificationComapat.Builder
创建一个自定义通知。 I want this notification having repeating sound (sound is from project's raw folder) and the duration should be 60 seconds. 我希望此通知具有重复的声音(声音来自项目的原始文件夹),持续时间应为60秒。
I managed to get the sound but I don't know how to set it to repeat for that time duration. 我设法获得了声音,但我不知道如何在该时间内将其设置为重复。 Please help! 请帮忙!
PS: is there any way without timer task? PS:有没有计时器任务的方法吗?
You can do it by the following 您可以执行以下操作
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.tone);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.start();
}
});
and your Thread Should stop the Media Player by 并且您的线程应通过以下方式停止Media Player
mp.stop();
after the sleeping of 60000 milliseconds. 休眠60000毫秒后。 hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.