views:

1142

answers:

2

I am writing a media player and i would like to have a progress bar showing the progress of the song. I found the ProgressBar class, but all i can get on the screen is a circular spinning icon. what im looking for is an actual bar.

How do i change the style of the ProgressBar to be a bar (not a circle) and how would i use it with MediaPlayer?

thanks

+3  A: 

set style="?android:attr/progressBarStyleHorizontal"

  <ProgressBar android:id="@+id/progreso"
        style="?android:attr/progressBarStyleHorizontal"

and this is an example with MediaPlayer

package com.playerpgbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Player extends Activity implements Runnable, OnClickListener{

   private TextView Status;
   private ProgressBar progressBar;
   private Button StartMedia;
   private Button Stop;
   private MediaPlayer mp;      

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       Status = (TextView) findViewById(R.id.Status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        StartMedia = (Button) findViewById(R.id.StartMedia);
        Stop = (Button) findViewById(R.id.Stop);

        StartMedia.setOnClickListener(this);
        Stop.setOnClickListener(this);                
    }        

@Override
public void onClick(View v) {
        if(v.equals(StartMedia)){
            if(mp != null && mp.isPlaying()) return;
            mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
            mp.start();               
            Status.setText(R.string.PlayingMedia);         
            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(0);
            progressBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if(v.equals(Stop) && mp!=null){
            mp.stop();
            mp = null;            
            Status.setText(R.string.Stopped);
            progressBar.setVisibility(ProgressBar.GONE);
        }

}

    @Override
    public void run() {
        int CurrentPosition= 0;
        int total = mp.getDuration();
        while(mp!=null && CurrentPosition<total){
            try {
                Thread.sleep(1000);
                CurrentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e){
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }


}
Jorgesys
Thanks this works well, it's just missing one line: you need to set the max position (setMax(total)) before you start the while loop or the bar will go straight to 100%.thankyou!
mtmurdock
hello i can't see the bar at 100% at the start, I set progressBar.setMax(mp.getDuration()); and then Execute the thread that contains the while loop to update the progressBar, new Thread(this).start();=)
Jorgesys
A: 

hi trying to put ProgressBar in MediaPlayer but not working i don't know what i m doing wrong

Please Help Me

import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar;

public class ProgressBarPlay extends Activity {

private ProgressBar progressBar; 
private Button Play;
private Button Stop;
MediaPlayer mPlayer;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Play=(Button)findViewById(R.id.Play);
    Stop=(Button)findViewById(R.id.Stop);

    Play.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View view) { if(v.equals(Play)){ if(mPlayer != null && mPlayer.isPlaying()) return;

String url = "http://202.134.228.124:8002/";

                try {
                    mPlayer = new MediaPlayer();
                    mPlayer.setDataSource(url);
                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mPlayer.prepare();
                    mPlayer.start();
                    progressBar.setVisibility(ProgressBar.VISIBLE);
                    progressBar.setProgress(0);
                    progressBar.setMax(mPlayer.getDuration());
                    new Thread(this).start();

                } catch (Exception e) {
                Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
                }

}

    Stop.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) { if(v.equals(Stop) && mPlayer!=null){ }

try {


    mPlayer.stop();
    mPlayer = null; 
    progressBar.setVisibility(ProgressBar.GONE);
} catch (Exception e) {
  Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
  }

}

    @Override
    public void run() {
        int CurrentPosition= 0;
        int total = mPlayer.getDuration();
        while(mPlayer!=null && CurrentPosition<total){
            try {
                Thread.sleep(1000);
                CurrentPosition= mPlayer.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e){
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }
}
nycsdk
if the answers provided on this page dont suit your needs, you might consider posting your own question because it addresses a different issue. this isnt a "board" for posting more questions, just answers to the current question.
mtmurdock
under your onClick method, your brackets are misplaced on that first if statement. Your class needs to implement "Runnable" if you are going to define the run method there.
mtmurdock