搜索栏在运行音轨时不会移动。无法调试

时间:2018-07-18 09:04:42

标签: android android-mediaplayer android-seekbar

关注了YouTube教程。但是我的搜索栏不会随音频一起移动。根据教程,我添加了处理程序,并且还添加了一个runnable。我想念什么吗?

private MediaPlayer music;
Handler handler;
Runnable runnable;
SeekBar seek;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handler = new Handler();
    Button play = (Button) findViewById(R.id.play);
    Button pause = (Button) findViewById(R.id.pause);
    music = MediaPlayer.create(this, R.raw.gnr_sweet);
    seek = (SeekBar)  findViewById(R.id.seekBar);
    seek.setMax(music.getDuration());
    playCycle();
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean userinput) {
            if(userinput){
                music.seekTo(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}
public void playCycle(){
    seek.setProgress(music.getCurrentPosition());
    if(music.isPlaying()){
        runnable= new Runnable() {
            @Override
            public void run() {
                playCycle();
            }
        };
        handler.postDelayed(runnable, 1000);
    }
}

我是android开发人员的新手,因此任何类型的输入都可能有用。 我在playCycle()方法中缺少什么吗? 还有一个用于播放暂停按钮的clicklistener,我刚刚将其从代码段中删除,以获取干净的Stackoverflow代码

3 个答案:

答案 0 :(得分:0)

删除此条件

datetime

进度条更改

 let params = [
        "key":value,
        "key1": key2] as [String : Any]



    let manager = Alamofire.SessionManager.default
    manager.session.configuration.timeoutIntervalForRequest = 30000

    manager.request(url, method: .post, parameters: params)
        .responseJSON {


            response in

            switch response.result {
            case .success:
               stopActivityIndicator()
                print(response)
                if let result = response.result.value {
                    let JSON = result as! NSDictionary
                    print(JSON)
                    let ResponseSuccess = JSON.object(forKey: "response")!
                    self.displayAlert(Message: ResponseSuccess as! String, myView:self)


                }


            case .failure( _):

                if let result = response.result.value {
                    stopActivityIndicator()
                    let JSON = result as! NSDictionary
                    print(JSON)
                    let ResponseFail = JSON.object(forKey: "response")!
                    self.displayAlert(Message: ResponseFail as! String, myView:self)
                }
            }


    }

答案 1 :(得分:0)

尝试

  mediaPlayer.seekTo((int) length);
        mediaPlayer.start();

        startTime = mediaPlayer.getCurrentPosition();
        finalTime = mediaPlayer.getDuration();

        seekbar.setMax((int) finalTime);

        handler.postDelayed(runnable, 100);

 @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (fromUser) {
        if (mediaPlayer == null) {
            seekBar.setProgress(progress);
        } else {
            mediaPlayer.seekTo(progress);
            seekbar.setProgress(progress);
        }
        length = progress;
    }

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

  Runnable runnable = new Runnable() {
    @Override
    public void run() {
        startTime = mediaPlayer.getCurrentPosition();
        seekbar.setProgress((int) startTime);
        handler.postDelayed(this, 100);
    }
};

对我有用

答案 2 :(得分:0)

就我而言,我创建了一个Runnable。 然后,我认为您需要在调用handler.postDelayed之前先设置seekBar.setProgress。这在我这方面起作用。

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        displayCurrentTime();
        seekBar.setProgress((int) player.getCurrentTimeMillis());
        mHandler.postDelayed(this, 100);
    }