简体   繁体   English

将SeekBar连接到MediaPlayer无法正常工作

[英]Connecting SeekBar to MediaPlayer isn't working

I've been following a tutorial to implement a SeekBar to my app but it isn't working. 我一直在遵循一个教程,以实现对我的应用程序的SeekBar ,但它不起作用。 Here's how I'm trying to do it. 这就是我要尝试的方法。

public void run(){
    int currentPosition = 0;
    int total = player.getDuration();
    sBar.setMax(total);

    while (player.isPlaying()) {
        try {
            Thread.sleep(1000);
            currentPosition = player.getCurrentPosition();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sBar.setProgress(currentPosition);
    }
}

sBar is the SeekBar which I declared as a global variable. sBar是我声明为全局变量的SeekBar

Now the problem is that there was nowhere mentioned in the tutorial where to then call this run method? 现在的问题是,教程中没有提到可以在何处调用该运行方法? I tried calling it right after the MediaPlayer is started but it isn't working. 我尝试在MediaPlayer启动后立即调用它,但是它不起作用。

This is obviously meant to be put in a Thread object- its an infinite loop that sleeps for a second at a time. 这显然是要放在Thread对象中的-一个无限循环,一次休眠一秒钟。 It can't go on the UI thread or it will freeze your app. 它无法进入UI线程,否则将冻结您的应用程序。

I made a completely different approach to this and it worked. 我对此采取了完全不同的方法,并且有效。 Here's how I've done it now: 这是我现在做的事情:

Runnable run = new Runnable(){

    @Override
    public void run() {
        seekBarUpdate();
    }

};

public void seekBarUpdate(){
    sBar.setMax(player.getDuration());
    sBar.setProgress(player.getCurrentPosition());
    seekHandler.postDelayed(run, 25);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM