私はメディア プレーヤーを書いています。曲の進行状況を示すプログレス バーが必要です。ProgressBar クラスを見つけましたが、画面に表示されるのは回転する円形のアイコンだけです。私が探しているのは実際のバーです。
ProgressBar のスタイルをバー (円ではなく) に変更するにはどうすればよいですか? また、MediaPlayer でどのように使用しますか?
ありがとう
私はメディア プレーヤーを書いています。曲の進行状況を示すプログレス バーが必要です。ProgressBar クラスを見つけましたが、画面に表示されるのは回転する円形のアイコンだけです。私が探しているのは実際のバーです。
ProgressBar のスタイルをバー (円ではなく) に変更するにはどうすればよいですか? また、MediaPlayer でどのように使用しますか?
ありがとう
スタイルを使用する?android:attr/progressBarStyleHorizontal
例えば:
<ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
これは 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);
}
}
}