详解Android应用开发--MP3音乐播放器代码实现(一)
程序员文章站
2024-02-13 20:11:52
需求1:将内存卡中的mp3音乐读取出来并显示到列表当中
1.从数据库中查询所有音乐数据,保存到list集合当中,list当中存放的是mp3info对象
2.迭代...
需求1:将内存卡中的mp3音乐读取出来并显示到列表当中
1.从数据库中查询所有音乐数据,保存到list集合当中,list当中存放的是mp3info对象
2.迭代list集合,把每一个mp3info对象的所有属性,保存到map对象当中
3.定义一个list集合,把map对象添加到list集合当中
4.通过定义一个simpleadpter,调用setadpter方法,将数据显示到列表当中
/** * 用于从数据库中查询歌曲的信息,保存在list当中 * * @return */ public list<mp3info> getmp3infos() { cursor cursor = getcontentresolver().query( mediastore.audio.media.external_content_uri, null, null, null, mediastore.audio.media.default_sort_order); list<mp3info> mp3infos = new arraylist<mp3info>(); for (int i = 0; i < cursor.getcount(); i++) { mp3info mp3info = new mp3info(); cursor.movetonext(); long id = cursor.getlong(cursor .getcolumnindex(mediastore.audio.media._id)); //音乐id string title = cursor.getstring((cursor .getcolumnindex(mediastore.audio.media.title)));//音乐标题 string artist = cursor.getstring(cursor .getcolumnindex(mediastore.audio.media.artist));//艺术家 long duration = cursor.getlong(cursor .getcolumnindex(mediastore.audio.media.duration));//时长 long size = cursor.getlong(cursor .getcolumnindex(mediastore.audio.media.size)); //文件大小 string url = cursor.getstring(cursor .getcolumnindex(mediastore.audio.media.data)); //文件路径 int ismusic = cursor.getint(cursor .getcolumnindex(mediastore.audio.media.is_music));//是否为音乐 if (ismusic != 0) { //只把音乐添加到集合当中 mp3info.setid(id); mp3info.settitle(title); mp3info.setartist(artist); mp3info.setduration(duration); mp3info.setsize(size); mp3info.seturl(url); mp3infos.add(mp3info); } } return mp3infos; }
/** * 填充列表 * @param mp3infos */ public void setlistadpter(list<mp3info> mp3infos) { list<hashmap<string, string>> mp3list = new arraylist<hashmap<string, string>>(); for (iterator iterator = mp3infos.iterator(); iterator.hasnext();) { mp3info mp3info = (mp3info) iterator.next(); hashmap<string, string> map = new hashmap<string, string>(); map.put("title", mp3info.gettitle()); map.put("artist", mp3info.getartist()); map.put("duration", string.valueof(mp3info.getduration())); map.put("size", string.valueof(mp3info.getsize())); map.put("url", mp3info.geturl()); mp3list.add(map); } madapter = new simpleadapter(this, mp3list, r.layout.music_list_item_layout, new string[] { "title", "artist", "duration" }, new int[] { r.id.music_title, r.id.music_artist, r.id.music_duration }); mmusiclist.setadapter(madapter); }
实现效果如图:
需求2:点击列表的每一项,启动一个服务来播放音乐
1. 注册条目点击监听器onitemclicklistener
2. 定义一个实现onitemclicklistener接口的内部类
3. 定义一个继承service的服务类playerservie
1. 注册条目点击监听器onitemclicklistener
mmusiclist = (listview) findviewbyid(r.id.music_list); mmusiclist.setonitemclicklistener(new musiclistitemclicklistener()); mp3infos = getmp3infos(); setlistadpter(getmp3infos());
2. 定义一个实现onitemclicklistener接口的内部类
private class musiclistitemclicklistener implements onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if(mp3infos != null) { mp3info mp3info = mp3infos.get(position); log.d("mp3info-->", mp3info.tostring()); intent intent = new intent(); intent.putextra("url", mp3info.geturl()); intent.putextra("msg", appconstant.playermsg.play_msg); intent.setclass(mainactivity.this, playerservice.class); startservice(intent); //启动服务 } } }
3. 定义一个继承service的服务类playerservie(未完善的service类)
package com.wwj.sb.service; import android.annotation.suppresslint; import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.media.mediaplayer.onpreparedlistener; import android.os.ibinder; import com.wwj.sb.domain.appconstant; @suppresslint("newapi") public class playerservice extends service { private mediaplayer mediaplayer = new mediaplayer(); //媒体播放器对象 private string path; //音乐文件路径 private boolean ispause; //暂停状态 @override public ibinder onbind(intent arg0) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { if(mediaplayer.isplaying()) { stop(); } path = intent.getstringextra("url"); int msg = intent.getintextra("msg", 0); if(msg == appconstant.playermsg.play_msg) { play(0); } else if(msg == appconstant.playermsg.pause_msg) { pause(); } else if(msg == appconstant.playermsg.stop_msg) { stop(); } return super.onstartcommand(intent, flags, startid); } /** * 播放音乐 * @param position */ private void play(int position) { try { mediaplayer.reset();//把各项参数恢复到初始状态 mediaplayer.setdatasource(path); mediaplayer.prepare(); //进行缓冲 mediaplayer.setonpreparedlistener(new preparedlistener(position));//注册一个监听器 } catch (exception e) { e.printstacktrace(); } } /** * 暂停音乐 */ private void pause() { if (mediaplayer != null && mediaplayer.isplaying()) { mediaplayer.pause(); ispause = true; } } /** * 停止音乐 */ private void stop(){ if(mediaplayer != null) { mediaplayer.stop(); try { mediaplayer.prepare(); // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数 } catch (exception e) { e.printstacktrace(); } } } @override public void ondestroy() { if(mediaplayer != null){ mediaplayer.stop(); mediaplayer.release(); } } /** * * 实现一个onpreparelister接口,当音乐准备好的时候开始播放 * */ private final class preparedlistener implements onpreparedlistener { private int positon; public preparedlistener(int positon) { this.positon = positon; } @override public void onprepared(mediaplayer mp) { mediaplayer.start(); //开始播放 if(positon > 0) { //如果音乐不是从头播放 mediaplayer.seekto(positon); } } } }
以上代码实现了两个需求,显示音乐列表和实现列表点击事件启动servic。下次会实现控制音乐播放的需求。希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Java比较问题详细分析