自定义MediaPlayer播放音乐路劲(内存,内存卡,网络)

效果图:
自定义MediaPlayer播放音乐路劲(内存,内存卡,网络)_第1张图片

下面是实现代码:

package com.example.android26_playmusic;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.sb_main_bar);
        //给进度条设置滑动的监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress=seekBar.getProgress();
                //在当前位置播放
                mediaPlayer.seekTo(progress);
            }
        });
    }

    public void isPlayOrPause(View view){
        final ImageButton imageButton = (ImageButton) view;
        if(mediaPlayer==null){
            //播放内存中的音乐
//            mediaPlayer = MediaPlayer.create(this, R.raw.laojie);
            //播放内存卡中的文件
            mediaPlayer = new MediaPlayer();
            //设置音乐来源路劲
            try {
                //内存卡路劲,要加上file
                mediaPlayer.setDataSource(this, Uri.parse("file://qqmusic/song/JC.mp3"));
                //网络上拿音乐播放

                mediaPlayer.setDataSource(this, Uri.parse("http://193.168.4.135(tomcat端口)/internet.mp3"));
                //准备
//                mediaPlayer.prepare();
//              获取当前位置
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

            //开始播放
            mediaPlayer.start();
            //把播放图标换成暂停图标
            imageButton.setImageResource(android.R.drawable.ic_media_pause);


            //设置音乐准备完成的监听
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    //下载操作(复制)
                    mediaPlayer.start();
                    imageButton.setImageResource(android.R.drawable.ic_media_pause);

                    //获取音乐的总时长
                    int pd = mediaPlayer.getDuration();
                    //将进度条设置最大值为:音乐的总时长
                    seekBar.setMax(pd);

                    //实例化线程
                   new myThread().start();
                }
            });
//            计算机\vivo X7\内部存储设备\qqmusic\song/JC.mp3

        }else if(mediaPlayer.isPlaying()){//判断是否正在播放
            //暂停播放
            mediaPlayer.pause();
            imageButton.setImageResource(android.R.drawable.ic_media_play);
        }else {
            //开始播放
            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }
    }

    public void getMedieo(View view){
        Intent intent = new Intent(this,SoundPoolActivity.class);
        startActivity(intent);
    }

    class myThread extends Thread{
        @Override
        public void run() {
            super.run();
            while (seekBar.getProgress()<=seekBar.getMax()){
                //获取音乐当前播放的位置
                int cp = mediaPlayer.getCurrentPosition();
                seekBar.setProgress(cp);
            }

        }
    }

}
清单文件中添加的权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    <uses-permission android:name="android.permission.INTERNET" />

你可能感兴趣的:(自定义MediaPlayer播放音乐路劲(内存,内存卡,网络))