android studio使用SeekBar显示播放进度条
时间: 2023-06-30 08:05:34 浏览: 184
要在 Android Studio 中使用 SeekBar 显示播放进度条,你需要遵循以下步骤:
1. 在你的布局文件中添加 SeekBar 组件,例如:
```
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
```
2. 在你的活动或片段中获取 SeekBar 组件的引用,并设置其进度:
```
SeekBar seekBar = findViewById(R.id.seekbar);
int progress = 50; // 设置进度
seekBar.setProgress(progress);
```
3. 在播放过程中更新 SeekBar 的进度。你可以使用一个计时器或线程来定期更新 SeekBar 的进度,例如:
```
final SeekBar seekBar = findViewById(R.id.seekbar);
final int duration = mediaPlayer.getDuration();
new Thread(new Runnable() {
@Override
public void run() {
int currentPosition = 0;
while (currentPosition < duration) {
try {
Thread.sleep(1000); // 每秒更新一次进度
currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition * 100 / duration); // 计算并更新进度
} catch (InterruptedException e) {
return;
}
}
}
}).start();
```
这样,你就可以在 Android 应用程序中使用 SeekBar 显示播放进度条了。
阅读全文
相关推荐

















