How do I use Android ProgressBar in determinate mode?

后端 未结 2 571
半阙折子戏
半阙折子戏 2020-11-28 07:37

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 cir

2条回答
  •  时光说笑
    2020-11-28 08:11

    use the style ?android:attr/progressBarStyleHorizontal

    for example:

      

    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

提交回复
热议问题