Android编程开发音乐播放器实例
程序员文章站
2023-12-20 17:36:16
本文实例讲述了android编程开发音乐播放器,分享给大家供大家参考,具体如下:
音乐播放器中综合了以下内容:
seekbar、listview、广播接收者(以代码的形...
本文实例讲述了android编程开发音乐播放器,分享给大家供大家参考,具体如下:
音乐播放器中综合了以下内容:
seekbar、listview、广播接收者(以代码的形式注册receiver)、系统服务、mediaplayer
实现的功能:
1.暂停/播放、下一首/上一首,点击某一首时播放
2.支持拖动进度条快进
3.列表排序
4.来电话时,停止播放,挂断后继续播放
5.可在后台播放
效果图:
界面:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <seekbar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginbottom="5dp" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginbottom="20dp" > <button android:layout_width="40dp" android:layout_height="40dp" android:text="|◀" android:onclick="previous" /> <button android:id="@+id/pp" android:layout_width="40dp" android:layout_height="40dp" android:text="▶" android:onclick="pp" /> <button android:layout_width="40dp" android:layout_height="40dp" android:text="▶|" android:onclick="next" /> </linearlayout> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" > <textview android:id="@+id/mname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="15sp" /> </linearlayout>
mainactivity:
package cn.test.audio; import java.io.file; import java.util.arraylist; import java.util.collections; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.bundle; import android.os.environment; import android.os.handler; import android.telephony.phonestatelistener; import android.telephony.telephonymanager; import android.view.view; import android.widget.adapterview; import android.widget.button; import android.widget.listview; import android.widget.seekbar; import android.widget.simpleadapter; import android.widget.textview; import android.widget.adapterview.onitemclicklistener; import android.widget.seekbar.onseekbarchangelistener; public class mainactivity extends activity { private textview nametextview; private seekbar seekbar; private listview listview; private list<map<string, string>> data; private int current; private mediaplayer player; private handler handler = new handler(); private button ppbutton; private boolean ispause; private boolean isstarttrackingtouch; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); nametextview = (textview) findviewbyid(r.id.name); seekbar = (seekbar) findviewbyid(r.id.seekbar); listview = (listview) findviewbyid(r.id.list); ppbutton = (button) findviewbyid(r.id.pp); //创建一个音乐播放器 player = new mediaplayer(); //显示音乐播放列表 generatelistview(); //进度条监听器 seekbar.setonseekbarchangelistener(new myseekbarlistener()); //播放器监听器 player.setoncompletionlistener(new myplayerlistener()); //意图过滤器 intentfilter filter = new intentfilter(); //播出电话暂停音乐播放 filter.addaction("android.intent.action.new_outgoing_call"); registerreceiver(new phonelistener(), filter); //创建一个电话服务 telephonymanager manager = (telephonymanager) getsystemservice(telephony_service); //监听电话状态,接电话时停止播放 manager.listen(new myphonestatelistener(), phonestatelistener.listen_call_state); } /* * 监听电话状态 */ private final class myphonestatelistener extends phonestatelistener { public void oncallstatechanged(int state, string incomingnumber) { pause(); } } /* * 播放器监听器 */ private final class myplayerlistener implements oncompletionlistener { //歌曲播放完后自动播放下一首歌曲 public void oncompletion(mediaplayer mp) { next(); } } /* * 下一首按钮 */ public void next(view view) { next(); } /* * 前一首按钮 */ public void previous(view view) { previous(); } /* * 播放前一首歌 */ private void previous() { current = current - 1 < 0 ? data.size() - 1 : current - 1; play(); } /* * 播放下一首歌 */ private void next() { current = (current + 1) % data.size(); play(); } /* * 进度条监听器 */ private final class myseekbarlistener implements onseekbarchangelistener { //移动触发 public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } //起始触发 public void onstarttrackingtouch(seekbar seekbar) { isstarttrackingtouch = true; } //结束触发 public void onstoptrackingtouch(seekbar seekbar) { player.seekto(seekbar.getprogress()); isstarttrackingtouch = false; } } /* * 显示音乐播放列表 */ private void generatelistview() { list<file> list = new arraylist<file>(); //获取sdcard中的所有歌曲 findall(environment.getexternalstoragedirectory(), list); //播放列表进行排序,字符顺序 collections.sort(list); data = new arraylist<map<string, string>>(); for (file file : list) { map<string, string> map = new hashmap<string, string>(); map.put("name", file.getname()); map.put("path", file.getabsolutepath()); data.add(map); } simpleadapter adapter = new simpleadapter(this, data, r.layout.item, new string[] { "name" }, new int[] { r.id.mname }); listview.setadapter(adapter); listview.setonitemclicklistener(new myitemlistener()); } private final class myitemlistener implements onitemclicklistener { public void onitemclick(adapterview<?> parent, view view, int position, long id) { current = position; play(); } } private void play() { try { //重播 player.reset(); //获取歌曲路径 player.setdatasource(data.get(current).get("path")); //缓冲 player.prepare(); //开始播放 player.start(); //显示歌名 nametextview.settext(data.get(current).get("name")); //设置进度条长度 seekbar.setmax(player.getduration()); //播放按钮样式 ppbutton.settext("||"); //发送一个runnable, handler收到之后就会执行run()方法 handler.post(new runnable() { public void run() { // 更新进度条状态 if (!isstarttrackingtouch) seekbar.setprogress(player.getcurrentposition()); // 1秒之后再次发送 handler.postdelayed(this, 1000); } }); } catch (exception e) { e.printstacktrace(); } } /** * 查找文件路径中所有mp3文件 * @param file 要找的目录 * @param list 用来装文件的list */ private void findall(file file, list<file> list) { file[] subfiles = file.listfiles(); if (subfiles != null) for (file subfile : subfiles) { if (subfile.isfile() && subfile.getname().endswith(".mp3")) list.add(subfile); else if (subfile.isdirectory())//如果是目录 findall(subfile, list); //递归 } } /* * 暂停/播放按钮 */ public void pp(view view) { //默认从第一首歌曲开始播放 if (!player.isplaying() && !ispause) { play(); return; } button button = (button) view; //暂停/播放按钮 if ("||".equals(button.gettext())) { pause(); button.settext("▶"); } else { resume(); button.settext("||"); } } /* * 开始操作 */ private void resume() { if (ispause) { player.start(); ispause = false; } } /* * 暂停操作 */ private void pause() { if (player != null && player.isplaying()) { player.pause(); ispause = true; } } /* * 收到广播时暂停 */ private final class phonelistener extends broadcastreceiver { public void onreceive(context context, intent intent) { pause(); } } /* * 恢复播放 * @see android.app.activity#onresume() */ protected void onresume() { super.onresume(); resume(); } }
注册权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.audio" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion="8" /> <!-- 监听电话呼出 --> <uses-permission android:name="android.permission.process_outgoing_calls"/> <!-- 监听电话状态改变 --> <uses-permission android:name="android.permission.read_phone_state" /> </manifest>
希望本文所述对大家android程序设计有所帮助。
推荐阅读
-
Android编程中Handler原理及用法实例分析
-
Android编程之九宫格实现方法实例分析
-
Android编程四大组件之Activity用法实例分析
-
Android编程开发实现多线程断点续传下载器实例
-
Android编程四大组件之BroadcastReceiver(广播接收者)用法实例
-
Android编程中自定义dialog用法实例
-
Android编程开发之ScrollView嵌套GridView的方法
-
Android编程自定义Notification实例分析
-
Android编程开发之在Canvas中利用Path绘制基本图形(圆形,矩形,椭圆,三角形等)
-
Android编程设置TextView颜色setTextColor用法实例