播发音乐时的状态条使用
播放音乐时的状态条使用
<ProgressBar android:id="@+id/progreso" style="?android:attr/progressBarStyleHorizontal"
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); } } }