本文介绍了Android的MediaPlayer的实时快进和快退按钮实现使用可运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,这却没有真正找到明确的指示。如何创建一个按钮,将快进和暂停快退当前歌曲。

请参阅答案


解决方案

 专用处理器repeatUpdateHandler =新的处理程序();
公众诠释mValue; //增量
私人布尔mAutoIncrement = FALSE; //用于实时快速向前的
私人布尔mAutoDecrement = FALSE; //用于实时倒带私有类RptUpdater实现Runnable {
        公共无效的run(){
            如果(mAutoIncrement){
                mValue + = 30; //改变这个值来控制多少转发
                mediaPlayer.seekTo(getCurrentPosition()+ mValue);
                repeatUpdateHandler.postDelayed(新RptUpdater(),50);
            }否则如果(mAutoDecrement){
                mValue - = 30; //改变这个值来控制多少倒带
                seekTo(getCurrentPosition() - mValue);
                repeatUpdateHandler.postDelayed(新RptUpdater(),50);
            }
        }
    }BTN prev.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    v.set pressed(真);
                    mAutoDecrement = TRUE;
                    repeatUpdateHandler.post(新RptUpdater());
                    返回false;
                }
                否则如果(event.getAction()== MotionEvent.ACTION_UP){
                    v.set pressed(假);
                    如果((event.getAction()== MotionEvent.ACTION_UP || event.getAction()== MotionEvent.ACTION_CANCEL)
                            &功放;&安培; mAutoDecrement){
                        mAutoDecrement = FALSE;
                    }
                    返回false;
                }
                返回false;
            }
        });btnNext.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    v.set pressed(真);
                    mAutoIncrement = TRUE;
                    repeatUpdateHandler.post(新RptUpdater());
                    返回false;
                }
                否则如果(event.getAction()== MotionEvent.ACTION_UP){
                    v.set pressed(假);
                    如果((event.getAction()== MotionEvent.ACTION_UP || event.getAction()== MotionEvent.ACTION_CANCEL)
                            &功放;&安培; mAutoIncrement){
                        mAutoIncrement = FALSE;
                    }
                    返回false;
                }                返回false;
            }
        });

I needed help with this but didn't really find clear instructions. How to create a button that will fast forward and rewind current song on hold.

Please see answer

解决方案
private Handler repeatUpdateHandler = new Handler();
public int mValue;           //increment
private boolean mAutoIncrement = false;          //for fast foward in real time
private boolean mAutoDecrement = false;         // for rewind in real time



private class RptUpdater implements Runnable {
        public void run() {
            if( mAutoIncrement ){
                mValue += 30; //change this value to control how much to forward
                mediaPlayer.seekTo(getCurrentPosition()+ mValue);
                repeatUpdateHandler.postDelayed( new RptUpdater(), 50 );
            } else if( mAutoDecrement ){
                mValue -= 30; //change this value to control how much to rewind
                seekTo(getCurrentPosition()- mValue);
                repeatUpdateHandler.postDelayed( new RptUpdater(), 50 );
            }
        }
    }



btnPrev.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setPressed(true);
                    mAutoDecrement = true;
                    repeatUpdateHandler.post( new RptUpdater() );
                    return false;
                }
                else if(event.getAction() == MotionEvent.ACTION_UP) {
                    v.setPressed(false);
                    if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)
                            && mAutoDecrement ){
                        mAutoDecrement = false;
                    }
                    return false;
                }
                return false;
            }
        });

btnNext.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setPressed(true);
                    mAutoIncrement = true;
                    repeatUpdateHandler.post( new RptUpdater() );
                    return false;
                }
                else if(event.getAction() == MotionEvent.ACTION_UP) {
                    v.setPressed(false);
                    if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)
                            && mAutoIncrement ){
                        mAutoIncrement = false;
                    }
                    return false;
                }

                return false;
            }
        });

这篇关于Android的MediaPlayer的实时快进和快退按钮实现使用可运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:25