在 Mediaplayer 循环时添加延迟
Adding delay while Mediaplayer loops
正在使用 MediaPlayer
class 播放 .wav
文件。因为我需要循环播放我设置的音频 .setLooping(true);
。很明显,问题是每次播放音频时如何添加延迟,比如我想要 5000
的延迟。
此处类似问题的答案不适用于我的情况。任何帮助,将不胜感激。这是我的代码:
Button Sample = (Button)findViewById(R.id.samplex);
Sample.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = Environment.getExternalStorageDirectory()+"/myAppCache/wakeUp.wav";
try {
mp.setDataSource(filePath);
mp.prepare();
mp.setLooping(true);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
您需要注册 2 个侦听器(完成时和出错时),然后您需要在完成回调时延迟下一次播放。错误侦听器的原因是 return true
以避免在出现错误时调用完成事件 - 解释 here
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);
}
});
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
...
return true;
});
mp.prepare();
// no need to loop it since on completion event takes care of this
// mp.setLooping(true);
只要您的销毁方法是 (Activity.onDestroyed(), Fragment.onDestroy(), View.onDetachedFromWindow()
),请确保您正在删除可运行的回调,例如
@Override
protected void onDestroy() {
super.onDestroy();
...
button.removeCallbacks(loopingRunnable);
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
}
正在使用 MediaPlayer
class 播放 .wav
文件。因为我需要循环播放我设置的音频 .setLooping(true);
。很明显,问题是每次播放音频时如何添加延迟,比如我想要 5000
的延迟。
此处类似问题的答案不适用于我的情况。任何帮助,将不胜感激。这是我的代码:
Button Sample = (Button)findViewById(R.id.samplex);
Sample.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = Environment.getExternalStorageDirectory()+"/myAppCache/wakeUp.wav";
try {
mp.setDataSource(filePath);
mp.prepare();
mp.setLooping(true);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
您需要注册 2 个侦听器(完成时和出错时),然后您需要在完成回调时延迟下一次播放。错误侦听器的原因是 return true
以避免在出现错误时调用完成事件 - 解释 here
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);
}
});
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
...
return true;
});
mp.prepare();
// no need to loop it since on completion event takes care of this
// mp.setLooping(true);
只要您的销毁方法是 (Activity.onDestroyed(), Fragment.onDestroy(), View.onDetachedFromWindow()
),请确保您正在删除可运行的回调,例如
@Override
protected void onDestroy() {
super.onDestroy();
...
button.removeCallbacks(loopingRunnable);
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
}