Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

material design - How do I use Android ProgressBar in determinate mode?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use the style ?android:attr/progressBarStyleHorizontal

for example:

  <ProgressBar android:id="@+id/progressBar"
        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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...