我正在尝试为我反复播放的音频文件创建延迟。 但是,代码似乎没有任何效果。这是我的代码:
try {
mp.setDataSource(text);
mp.prepare();
mp.setLooping(true);
mp.setVolume(0.5f,0.5f);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
@Override
public void run()
{
time += 1000;
Log.d("TimerExample", "Going for... " + time);
h.postDelayed(this, 5000);
}
}, 5000);
}
});
任何修复/解决方案都将受到高度赞赏。
答案 0 :(得分:0)
请参阅此https://stackoverflow.com/questions/31247607/adding-delay-while-mediaplayer-loops
主要是你必须使用这样的监听器
private final Runnable loopingRunnable = new Runnable() {
@Override
public void run() {
if (mp != null) {
if (mp.isPlaying() {
mp.stop();
}
mp.start();
}
}
}
mp.setDataSource(filePath);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
button.postDelayed(loopingRunnable, 5000);
}
});