重复音频文件,并在任务改变时停止
Repeat audio file, and stop when the task changed
我想每 1 分钟重复播放一次 "Searching" 的音频文件。当 CASE_STATUS_SEARCHING
将更改为其他时,我如何实施它以停止此文件?我用了 TIMER
和 SOUNDPOOL
:
public class Player {
private static SoundPool soundPool;
private static int searching;
final Timer timer = new Timer();
public void searching () {
timer.schedule(new TimerTask() {
@Override
public void run() {
soundPool.play(searching, 1.0f, 1.0f, 1, 0, 1.0f);
}
}, 60000);
}
//我在这里开始线程:
public class Locator BroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
final String action = intent.getAction();
switch (newStatus) {
case STATUS_SEARCHING:
stateStr = getString(R.string.Scanning);
tvStatus.setText(R.string.Scanning);
Player.searching();
break;
}
在 Whosebug 的帮助下,我自己解决了这个问题。
public void playStillSearching() {
if (handler == null) {
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
soundPool.play(stillSearching, 1.0f, 1.0f, 1, 0, 1.0f);
handler.postDelayed(this, 3000);
}
}, 3000);
}
}
public void stopSpeak() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
比我在我的活动中实施这些。
我想每 1 分钟重复播放一次 "Searching" 的音频文件。当 CASE_STATUS_SEARCHING
将更改为其他时,我如何实施它以停止此文件?我用了 TIMER
和 SOUNDPOOL
:
public class Player {
private static SoundPool soundPool;
private static int searching;
final Timer timer = new Timer();
public void searching () {
timer.schedule(new TimerTask() {
@Override
public void run() {
soundPool.play(searching, 1.0f, 1.0f, 1, 0, 1.0f);
}
}, 60000);
}
//我在这里开始线程:
public class Locator BroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
final String action = intent.getAction();
switch (newStatus) {
case STATUS_SEARCHING:
stateStr = getString(R.string.Scanning);
tvStatus.setText(R.string.Scanning);
Player.searching();
break;
}
在 Whosebug 的帮助下,我自己解决了这个问题。
public void playStillSearching() {
if (handler == null) {
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
soundPool.play(stillSearching, 1.0f, 1.0f, 1, 0, 1.0f);
handler.postDelayed(this, 3000);
}
}, 3000);
}
}
public void stopSpeak() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
比我在我的活动中实施这些。