1

I've been working on this for a while, trying to get this tutorial to work (http://united-coders.com/nico-heid/an-android-seekbar-for-your-mediaplayer/), but I haven't had any luck. The audio playback works perfect, but the SeekBar doesn't move.

package com.example.playingaudio;

import java.io.FileInputStream;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends Activity implements Runnable {

    private MediaPlayer mediaPlayer;
    private SeekBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        progress = (SeekBar) findViewById(R.id.seekBar1);

    }

    public void playButton(View view) {
        try {
            playRecording();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    protected void playRecording() throws Exception {
        final MediaPlayer mediaPlayer = new MediaPlayer();
        FileInputStream fileStream = new FileInputStream(
                "/sdcard/Download/mySong.mp3");
        mediaPlayer.setDataSource(fileStream.getFD());
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
        mediaPlayer.start();
        run();
    }

    private void ditchMediaPlayer() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void run() {
        // mp is your MediaPlayer
        // progress is your ProgressBar

        int currentPosition = 0;
        int total = mediaPlayer.getDuration();
        progress.setMax(total);
        while (mediaPlayer != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = mediaPlayer.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            progress.setProgress(currentPosition);
        }
    }

}
EGHDK
  • 17,818
  • 45
  • 129
  • 204

2 Answers2

1

Try changing this line

final MediaPlayer mediaPlayer = new MediaPlayer();

To, this line

mediaPlayer = new MediaPlayer();

The reason is you already have a class variable mediaPlayer declared and why are you declaring the local variable again with the same name.

Peshal
  • 1,508
  • 1
  • 12
  • 22
0

The reason your bar is not updating is because you aren't giving it a chance to. You have a constant loop on your UI thread that consists mostly of sleep(). You can't do that and expect the UI to update.

If you look at that tutorial more closely, you'll see that they don't call runOnUiThread(). In fact, at the bottom there is a link back to SO, which shows a bit more of the code involved. There's just a new Thread created, and start() is run. Nothing too tricky.


Example:

(call this method after mediaPlayer.start()):

private void createProgressThread() {   
    progressUpdater = new Runnable() {
        @Override
        public void run() {
            //...
            //...
        }
    };
    Thread thread = new Thread(progressUpdater);
    thread.start();
}
Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • I switched up my code, I'm not streaming, just playing back a file now. So you can disregard any of the streaming code. The file is local, and it plays fine. – EGHDK Aug 06 '13 at 00:46
  • If you "switched up your code", it would be good to edit the question to reflect the *exact* code you're using. You can't debug by looking at fake code. – Geobits Aug 06 '13 at 00:48
  • Yeah, I saw that link, but I still don't understand where that "run" code is supposed to be placed. I've updated my question to show how I originally tried to do it. – EGHDK Aug 06 '13 at 01:04
  • That still calls it on the UI thread. You need a new `Thread` to run it on. See code example in update. – Geobits Aug 06 '13 at 01:09
  • What is `progressUpdater`? – EGHDK Aug 06 '13 at 14:51
  • It's a [Runnable](http://developer.android.com/reference/java/lang/Runnable.html) object. The second line in the example is where it's assigned. If you're not familiar with runnables and threads, you might want to do some reading. – Geobits Aug 06 '13 at 15:14