[英]ringtone can not be stopped
I'm developing a speedometer app and now working on some kind of speed limit function.我正在开发一个速度计应用程序,现在正在研究某种速度限制功能。 The idea is to make noise when speed gets bigger than the target value.这个想法是在速度大于目标值时发出噪音。 The code I use:我使用的代码:
Ringtone ringtone;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.updateSpeed(null);
}
private void updateSpeed(Location location){
...
float speed = location.getSpeed();
int i = (int) speed;
this.doLimit(i);
}
private void doLimit(int i) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
if (i >= 50) {
this.lets_dance();
} else {
if (ringtone.isPlaying()) {
ringtone.stop();
}
}
}
private void lets_dance() {
ringtone.play();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
if(!ringtone.isPlaying()){
ringtone.play();
}
}
}, 1000, 1000);
}
The problem is that the ringtone is playing the first time the speed exceeds 50 and doesn't stop.问题是铃声第一次播放速度超过50并且不停。
Before I tried a lot of methods of using Mediaplayer with and without setLooping and situation was even worse (it caused app to lag like I'm using it on the calculator).在我尝试了很多使用和不使用 setLooping 的 Mediaplayer 方法之前,情况更糟(它导致应用程序滞后,就像我在计算器上使用它一样)。
It could be because each time doLimit() is called, a new instance of ringtone is created.这可能是因为每次调用 doLimit() 时,都会创建一个新的铃声实例。 Try with the following lines in the onCreate() function:尝试在 onCreate() 函数中使用以下几行:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.