问题描述
播放使用的MediaPlayer
类 .WAV
文件。当我需要循环音频我设置 .setLooping(真);
。所以很明显,怀疑是我怎么每次添加一个延迟的音频播放,再说我也想的延迟5000
。
类似的问题的答案在这里没有我的情况下工作。任何帮助将是AP preciated。这里是我的code:
按钮样品=(按钮)findViewById(R.id.samplex);
Sample.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ 字符串文件路径= Environment.getExternalStorageDirectory()+/ myAppCache / wakeUp.wav 尝试{
mp.setDataSource(文件路径);
MP prepare()。
mp.setLooping(真); }
赶上(抛出:IllegalArgumentException五){
e.printStackTrace();
}赶上(SecurityException异常五){
e.printStackTrace();
}赶上(IllegalStateException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
mp.start(); }
});
您需要注册2监听器(在完成和错误),然后你就需要推迟接下来的发挥在完成回调。原因错误监听是返回真正
来避免调用完成事件只要有一个错误 - 说明这里
私人最终的Runnable loopingRunnable =新的Runnable(){
@覆盖
公共无效的run(){
如果(熔点!= NULL){
如果(mp.isPlaying(){
mp.stop();
}
mp.start();
}
}
}mp.setDataSource(文件路径);
mp.setOnCompletionListener(新MediaPlayer.OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的MP){
button.postDelayed(loopingRunnable,5000);
}
});
mp.setOnErrorListener(新MediaPlayer.OnErrorListener(){
...
返回true;
});MP prepare()。
//无需环路它,因为在完成事件发生的这种关心
// mp.setLooping(真);
每当你的销毁方法是( Activity.onDestroyed(),Fragment.onDestroy(),View.onDetachedFromWindow()
),请确保您删除的可运行的回调,如:
@覆盖
保护无效的onDestroy(){
super.onDestroy();
...
button.removeCallbacks(loopingRunnable); 如果(熔点!= NULL){
如果(mp.isPlaying()){
mp.stop();
} mp.release();
熔点=无效;
}
}
Playing .wav
file using MediaPlayer
class. As I need to loop the Audio I've set .setLooping(true);
. So obviously, the doubt is how do I add a delay each time the audio plays, say I want a delay of 5000
.
The answers to similar questions here doesn't work in my case. Any help would be appreciated. Here is my code:
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();
}
});
You need to register 2 listeners (on completion and on error) and then you would need to delay next play in on completion callback. Reason for the error listener is to return true
to avoid calling on completion event whenever there is an error - explanation 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);
Whenever your destruction method is (Activity.onDestroyed(), Fragment.onDestroy(), View.onDetachedFromWindow()
), ensure you are removing the runnable callbacks, e.g.
@Override
protected void onDestroy() {
super.onDestroy();
...
button.removeCallbacks(loopingRunnable);
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
}
这篇关于添加延迟,而媒体播放器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!