EGHDK
EGHDK

Reputation: 18130

MediaPlayer works in Android, but progress does not

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);
        }
    }

}

Upvotes: 1

Views: 364

Answers (2)

Peshal
Peshal

Reputation: 1518

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.

Upvotes: 1

Geobits
Geobits

Reputation: 22342

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();
}

Upvotes: 0

Related Questions