一.项目需求:
1.播放本地音乐:mediaPlayer.setDataSource(文件路径);
2.播放网络音乐:mediaPlayer.setDataSource(“http://www.ytmp3.cn/down/57799.mp3”);
3.完成音乐列表,实现播放/暂停/上一首/下一首/播放模式切换(随机播放/单曲循环/顺序播放)
二.MediaPlayer常用的方法:
void setDataSource(String path) :通过一个具体的路径来设置MediaPlayer的数据源,path可以是本地的一个路径,也可以是一个网络路径
int getCurrentPosition() 获取当前播放的位置
int getAudioSessionId() 返回音频的session ID
int getDuration() 得到文件的时间
boolean isLooping () 是否循环播放
boolean isPlaying() 是否正在播放
void pause () 暂停
void start () 开始
void stop () 停止
void prepare() 同步的方式装载流媒体文件。
void prepareAsync() 异步的方式装载流媒体文件。
void reset() 重置MediaPlayer至未初始化状态。
void release () 回收流媒体资源。
void seekTo(int msec) 指定播放的位置(以毫秒为单位的时间)
void setLooping(boolean looping) 设置是否单曲循环
setOnCompletionListener(MediaPlayer.OnCompletionListener listener) 网络流媒体播放结束时回调
setOnErrorListener(MediaPlayer.OnErrorListener listener) 发生错误时回调 **
setOnPreparedListener(MediaPlayer.OnPreparedListener listener):当装载流媒体完毕的时候回调。
三 效果图 实现代码
package com.example.day08_mediaplayer;
import android.media.MediaPlayer;
import java.io.IOException;
import java.util.ArrayList;
public class Music_Uitls {
int is_item;
ArrayList<Music> list;
MediaPlayer mediaPlayer;
int mode;//0代表单曲 1代表顺序 2 代表
public static final int MODE_DANQU=0;
public static final int MODE_SHUNXU=1;
public static final int MODE_SUIJI=2;
//私有化构造
private Music_Uitls() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
switch (mode){
case MODE_DANQU:
play(is_item);
break;
case MODE_SHUNXU:
next();
break;
case MODE_SUIJI:
int num= (int) Math.random()*list.size();
play(num);
break;
}
}
});
}
//自行实例化
private static Music_Uitls music_uitls;
//提供对外方法
public static Music_Uitls getInstance(){
if (music_uitls == null){
synchronized (String.class){
if (music_uitls == null){
music_uitls = new Music_Uitls();
}
}
}
return music_uitls;
}
//提供集合
public void setList(ArrayList<Music> list){
this.list = list;
}
//准备播放方法
public void play(int position) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(list.get(position).getPath());
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
is_item = position;
} catch (IOException e) {
e.printStackTrace();
}
}
//播放方法
public void start(){
mediaPlayer.start();
}
//暂停方法
public void pause(){
mediaPlayer.pause();
}
//上一曲
public void pre(){
if (is_item==0){
is_item=list.size()-1;
}else {
is_item--;
}
play(is_item);
}
//下一曲
public void next(){
if (is_item==list.size()-1){
is_item=0;
}else {
is_item++;
}
play(is_item);
}
//播放模式切换
public int mode(){
if(mode==0){
mode=1;
}else if(mode==1){
mode=2;
}else{
mode=0;
}
return mode;
}
//获得音乐总时长
public long getDuration(){
if(mediaPlayer!=null){
return mediaPlayer.getDuration();
}
return 0;
}
//获得当前时长
public long getCurrent(){
if(mediaPlayer!=null){
return mediaPlayer.getCurrentPosition();
}
return 0;
}
//设置当前位置
public void seekto(int position){
if(mediaPlayer!=null){
mediaPlayer.seekTo(position);
}
}
//回收资源
public void release() {
if (mediaPlayer != null) {
mediaPlayer.stop();//停止
mediaPlayer.release(); //弄死
mediaPlayer = null;//垃圾回收机制回收掉 火葬
}
}
}
Activity代码
package com.example.day08_mediaplayer;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import java.util.ArrayList;
import java.util.Timer;
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 101){
bar.setMax((int) Music_Uitls.getInstance().getDuration());
bar.setProgress((int) Music_Uitls.getInstance().getCurrent());
}
}
};
int strat_stop_flag = 0;
int type_flag = 0;
ArrayList<Music> list = new ArrayList<>();
ImageView type;
ImageView pre;
ImageView strat_stop;
ImageView next;
SeekBar bar;
BaseRvAdapter<Music> adapter;
RecyclerView recyclerView;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv);
type = findViewById(R.id.type);
pre = findViewById(R.id.pre);
strat_stop = findViewById(R.id.start_stop);
next = findViewById(R.id.next);
bar = findViewById(R.id.bar);
setData();
Music_Uitls.getInstance().setList(list);
adapter = new BaseRvAdapter<Music>(list,this,R.layout.layout) {
@Override
public void bind(BaseViewHolder baseViewHolder, final int i) {
baseViewHolder.setText(R.id.music,list.get(i).getMusic());
baseViewHolder.setText(R.id.name,list.get(i).getName());
baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Music_Uitls.getInstance().play(i);
strat_stop_flag = 1;
strat_stop.setImageResource(R.drawable.stop_icon);
}
});
}
};
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
strat_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (strat_stop_flag == 0){
strat_stop_flag=1;
strat_stop.setImageResource(R.drawable.stop_icon);
Music_Uitls.getInstance().start();
}else if(strat_stop_flag == 1){
strat_stop_flag = 0;
strat_stop.setImageResource(R.drawable.start_icon);
Music_Uitls.getInstance().pause();
}
}
});
pre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Music_Uitls.getInstance().pre();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Music_Uitls.getInstance().next();
}
});
type.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = Music_Uitls.getInstance().mode();
if(num==0){
type.setImageResource(R.mipmap.danquxunhuan);
}else if(num==1){
type.setImageResource(R.mipmap.shunxubofang);
} else {
type.setImageResource(R.mipmap.suijibofang);
}
}
});
bar.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) {
bar.setMax((int) Music_Uitls.getInstance().getDuration());
int progress = bar.getProgress();
Music_Uitls.getInstance().seekto(progress);
}
});
}
public void setData(){
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor query = contentResolver.query(uri, null, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex(MediaStore.Video.Media.ARTIST));
String path = query.getString(query.getColumnIndex(MediaStore.Video.Media.DATA));
String music = query.getString(query.getColumnIndex(MediaStore.Video.Media.TITLE));
list.add(new Music(path, music, name));
}
}
}