简单音乐播放器有进度条

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);
                }
            }
        }
    }
}

效果图

在这里插入图片描述

音频播放实现暂停开始等 }); btnStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub musicStop(); } }); player.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub if(currentPlayMusicNum<musicList.size()){ currentPlayMusicNum+=1; }else{ currentPlayMusicNum=0; } musicStart(currentPlayMusicNum); } }); lvShowMusicList.setOnItemClickListener(new OnItemClickListener() { @SuppressLint("ResourceAsColor") @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if(saveView==view){ saveView.setBackgroundResource(R.color.nocolor); currentPlayMusicNum=0; } else{ if(saveView!=null) saveView.setBackgroundResource(R.color.nocolor); view.setBackgroundResource(R.color.colorblue); saveView=view; currentPlayMusicNum=position; musicStart(currentPlayMusicNum); } //id_this=position; } }); sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if(fromUser){ sb.setProgress(progress); //musicPause(); MusicModel music=musicList.get(currentPlayMusicNum); music.setPlayTime(music.getAllTime()*progress/100); //pausePosition=(int) (music.getAllTime()*progress/100); player.seekTo((int) music.getPlayTime()); player.start(); } } }); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值