音频seekBar线程问题?

发布于 2024-11-25 17:55:17 字数 1383 浏览 3 评论 0原文

最初,我有一个 MediaPlayer mPlayers 来播放/暂停/停止我的音频文件。现在,我正在尝试为我的媒体添加一个搜索栏。我尝试使用线程,这就是我想到的:

在 onCreate() 中:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            mPlayer.seekTo(progress);
        }

////........Omitted Code..........\\\\

//At the very end of the method:
seekThread  = new Thread(this); //My Activity implements:Runnable
seekThread.start();

然后,我实现了一个方法 run() 来每 1 秒更新我的搜索栏:

public void run() {
    try {
        while(mPlayer != null){
            int currentPosition = mPlayer.getCurrentPosition();
            Message msg = new Message();
            msg.what = currentPosition;
            threadHandler.sendMessage(msg);
            Thread.sleep(1000);
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
private Handler threadHandler = new Handler(){
    public void handleMessage(Message msg){
        seekBar.setProgress(msg.what);
    }
};

但是,在我的 HTC Magic,音频运行效率低下。有些话重复了。似乎搜索栏每隔一段时间就会向后退一点。

为了让你更好地理解这个问题,让我们这样说:如果音频说: abcdefghijklmn ,那么它将是这样的: abccdeffghijjklmnn 。

您对我的帖子有什么改进建议吗?我做得对吗?

Originally, I had a MediaPlayer mPlayers to play/pause/stop my Audio file. Now, I'm trying to add a seek bar for my media. I tried using Threads and this is what I came up with:

In onCreate() :

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            mPlayer.seekTo(progress);
        }

////........Omitted Code..........\\\\

//At the very end of the method:
seekThread  = new Thread(this); //My Activity implements:Runnable
seekThread.start();

Then, I implemented a method run() to update my seek bar every 1 sec:

public void run() {
    try {
        while(mPlayer != null){
            int currentPosition = mPlayer.getCurrentPosition();
            Message msg = new Message();
            msg.what = currentPosition;
            threadHandler.sendMessage(msg);
            Thread.sleep(1000);
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
private Handler threadHandler = new Handler(){
    public void handleMessage(Message msg){
        seekBar.setProgress(msg.what);
    }
};

However, In my HTC Magic, the audio run inefficiently. Some words are repeated. It seems like the seek bar is going back a little bit every some time.

To make you understand the problem well, Let's say this: if the audio says that: abcdefghijklmn , then it will be like this: abccdeffghijjklmnn.

Any improvement you suggest to my thread ? Am I doing it right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-12-02 17:55:18

不确定这是否有帮助,但请尝试:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if (fromUser) {
            mPlayer.seekTo(progress);
        }
    }

没有此条件,mPlayer.seekTo(progress);每次调用seekBar.setProgress(msg.what)时都会被调用;

Not sure this will help, but try:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if (fromUser) {
            mPlayer.seekTo(progress);
        }
    }

Without this condition, mPlayer.seekTo(progress); is called every time you call seekBar.setProgress(msg.what);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文