添加延迟,而媒体播放器循环 [英] Adding delay while Mediaplayer loops

查看:248
本文介绍了添加延迟,而媒体播放器循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

播放使用的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监听器(在完成和错误),然后你就需要推迟接下来的发挥在完成回调。原因错误监听是返回真正来避免调用完成事件只要有一个错误 - 说明<一个href=\"http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html#onError%28android.media.MediaPlayer,int,int\"相对=nofollow>这里

 私人最终的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;
    }
}

这篇关于添加延迟,而媒体播放器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆