Android url MP3实现播放,暂停,上一首,下一首,渐变色进度条和时间点
时间: 2023-10-02 15:04:20 浏览: 201
实现播放、暂停、上一首、下一首、渐变色进度条和时间点可以通过MediaPlayer和SeekBar组合实现。具体步骤如下:
1. 在布局文件中添加MediaPlayer和SeekBar组件,如下所示:
```xml
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:progressDrawable="@drawable/custom_seekbar_progress"
android:thumb="@drawable/custom_seekbar_thumb" />
<Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous" />
<Button
android:id="@+id/btnPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
```
2. 在Activity中实例化MediaPlayer和SeekBar组件,并设置监听器,如下所示:
```java
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private int[] musicList = {R.raw.music1, R.raw.music2, R.raw.music3};
private int currentTrack = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, musicList[currentTrack]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playNextTrack();
}
});
seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Button btnPrevious = findViewById(R.id.btnPrevious);
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playPreviousTrack();
}
});
Button btnPlayPause = findViewById(R.id.btnPlayPause);
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
pauseTrack();
} else {
playTrack();
}
}
});
Button btnNext = findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playNextTrack();
}
});
}
```
3. 实现播放、暂停、上一首、下一首功能,如下所示:
```java
private void playTrack() {
mediaPlayer.start();
updateSeekBar();
Button btnPlayPause = findViewById(R.id.btnPlayPause);
btnPlayPause.setText("Pause");
}
private void pauseTrack() {
mediaPlayer.pause();
Button btnPlayPause = findViewById(R.id.btnPlayPause);
btnPlayPause.setText("Play");
}
private void playPreviousTrack() {
if (currentTrack > 0) {
currentTrack--;
} else {
currentTrack = musicList.length - 1;
}
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, musicList[currentTrack]);
playTrack();
}
private void playNextTrack() {
if (currentTrack < musicList.length - 1) {
currentTrack++;
} else {
currentTrack = 0;
}
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, musicList[currentTrack]);
playTrack();
}
```
4. 实现渐变色进度条和时间点功能,如下所示:
```java
private void updateSeekBar() {
seekBar.setMax(mediaPlayer.getDuration());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
updateSeekBar();
}
}, 1000);
int minutes = (mediaPlayer.getCurrentPosition() / 1000) / 60;
int seconds = (mediaPlayer.getCurrentPosition() / 1000) % 60;
String time = String.format("%d:%02d", minutes, seconds);
TextView txtTime = findViewById(R.id.txtTime);
txtTime.setText(time);
}
```
5. 最后,添加自定义SeekBar的样式,如下所示:
```xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<color android:color="#BDBDBD" />
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="4dp" />
<gradient
android:angle="0"
android:endColor="#FF4081"
android:startColor="#3F51B5" />
</shape>
</clip>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="4dp" />
<gradient
android:angle="0"
android:endColor="#FF4081"
android:startColor="#3F51B5" />
</shape>
</clip>
</item>
</layer-list>
```
至此,实现了Android url MP3实现播放、暂停、上一首、下一首、渐变色进度条和时间点的功能。
阅读全文