Hello,
I have created a MediaPlayer and will play a sound file. There is also
a ProgressBar which I've configured to be a Horizontal Progress Bar.
The problem I'm having is getting the progress bar to update as the
song plays, i.e. when the song is half way played I want the progress
bar to be half way filled.
I can see that the trick is to use some sort of sleeping thread that
wakes up and then performs the ProgressBar.setProgress
(MediaPlayer.getCurrentPosition()) code but I cannot factor in the
threading code.
Would someone please take a look at a simple version of my code that
uses a button to manually update the progress bar and please help to
set me on the right course. Threads have never been my strong suit :(
Thanks in advance guys.

public class musicPlayer extends Activity {

        private Button buttonPlay;
        private MediaPlayer mp = new MediaPlayer();
        private ProgressBar progressMusic;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initialiseSong();

        //Create Play Button
        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                        playSong();
                }
        });

        progressMusic = (ProgressBar) findViewById
(R.id.progressMusic);
        progressMusic.setProgress(0);
        progressMusic.setMax(mp.getDuration());

        final Button buttonIndex = (Button) findViewById
(R.id.buttonIndex);
        buttonIndex.setOnClickListener(new OnClickListener(){
                public void onClick(View v){
                        progressMusic.setProgress(mp.getCurrentPosition());
                }
        });
    }

    //Creates the player with a default song selected.
    private void initialiseSong(){
        mp = MediaPlayer.create(this, R.raw.cake);
    }
    private void playSong(){
                if(mp.isPlaying()){
                        mp.pause();
                        buttonPlay.setText("Resume");
                }else{
                        mp.start();
                        buttonPlay.setText("Pause");
                }
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to