<ProgressBar android:id="@+id/progreso"
style="?android:attr/progressBarStyleHorizontal"
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);
}
}
}
标签:播放,音乐,ProgressBar,进度条
From: https://blog.51cto.com/u_16166892/6523967