简单音乐播放器有进度条
程序员文章站
2024-01-11 20:59:04
...
Bean类
package com.example.month1.xin;
public class Music {
private String data;
public Music() {
}
public Music(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "Music{" +
"data='" + data + '\'' +
'}';
}
}
MainActivity
package com.example.month1.xin;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import com.example.month1.R;
import java.io.File;
import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class Main2Activity extends AppCompatActivity {
ListView listView;
SeekBar seekBar;
Button play;
Button b_stop;
Button b_Top;
Button b_below;
Button b_pause;
ArrayList<String>arr=new ArrayList<>();
ArrayList<Music> Music_List=new ArrayList<Music>();
MediaPlayer mediaPlayer;
int cur_postion=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView=findViewById(R.id.list);
seekBar=findViewById(R.id.seek);
play=findViewById(R.id.play);
b_stop=findViewById(R.id.stop);
b_Top=findViewById(R.id.top);
b_below=findViewById(R.id.below);
b_pause=findViewById(R.id.Pause);
ContentResolver resolver = getContentResolver();
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI ;
String song=MediaStore.Audio.Media.TITLE;
String singer=MediaStore.Audio.Media.ARTIST;
String Sum_Time=MediaStore.Audio.Media.DURATION;
String data= MediaStore.Audio.Media.DATA;
Cursor cursor = resolver.query(uri, null, null, null, null);
while (cursor.moveToNext()){
String s1 = cursor.getString(cursor.getColumnIndex(song));
String s2 = cursor.getString(cursor.getColumnIndex(singer));
String s3 = cursor.getString(cursor.getColumnIndex(Sum_Time));
String s4 = cursor.getString(cursor.getColumnIndex(data));
//获得总时长
Long aLong = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
Music_List.add(new Music(s4));
//获取分和秒
int second= (int) (aLong/1000/60);
int minutes= (int) (aLong/1000%60);
Log.e("##",second+"."+minutes);
arr.add(s1+" "+second+"."+minutes);
arr.add(s1);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, arr);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
playMusic(position);
}
});
b_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stop();
}
});
b_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pause();
}
});
b_Top.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TOP();
}
});
b_below.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
next();
}
});
// String music_sd="sdcard/歌曲资源";
// if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// File fil = new File(music_sd);
// findFile(fil);
// }
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取托拽的进度
int progress=seekBar.getProgress();
//设置播放的进度
mediaPlayer.seekTo(progress);
}
});
}
public void playMusic(int position){
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
}
//重置
mediaPlayer.reset();
//新设置资源
try {
mediaPlayer.setDataSource(this, Uri.parse(Music_List.get(position).getData()));
//准备
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//获取当前音乐的长度
int currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition);
}
},0,100);
}
});
} catch (IOException e) {
e.printStackTrace();
}
//设置播放完毕下一首
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
next();
}
});
cur_postion=position;
}
public void start(){
if(mediaPlayer!=null){
mediaPlayer.start();
}
}
public void Pause(){
if(mediaPlayer!=null){
mediaPlayer.pause();
}
}
public void stop(){
if(mediaPlayer!=null){
mediaPlayer.stop();
}
}
public void TOP(){
if(cur_postion==0){
cur_postion=arr.size()-1;
}else{
cur_postion--;
}
playMusic(cur_postion);
}
private void next() {
if(cur_postion==arr.size()-1){
cur_postion=0;
}else{
cur_postion++;
}
playMusic(cur_postion);
}
private void findFile(File fil) {
if(fil.isDirectory()){
File[] files = fil.listFiles();
for(File f:files){
if(f.isFile()){
if(f.getName().endsWith(".mp3")){
arr.add(f.getAbsolutePath());
}
}else{
findFile(f);
}
}
}
}
}