简易音乐播放器(Mediaplayer+Broadcast+Service)可拖动进度条
实现效果图以及功能说明
功能说明 : 支持
① 读取sdcard目录下的歌曲,列表展示
② 点击列表切换歌曲,可以切换上一首,下一首,暂停,重置音乐
③ 可以选择播放模式,按照按钮顺序分别是单曲循环,随机播放,列表循环,由于实现逻辑,需要等到当前歌曲结束才能生效
④ 可以拖拽进度条更新进度
⑤自动播放下一首,打开自动播放第一首,后台播放,重新进入状态一致
存在的问题:MainActivity活动销毁启动后台服务,但是无法做到清空后台之后暂停音乐服务(简单来说后台全部清空音乐还是继续播放)
效果图
效果动图
实现框架
实现思路
① 构建一个包装类用来保存歌曲信息、作者以及歌曲路径,作为列表的填充对象
② MainActivity 作为填充文本和图片的活动,MusicService作为更改mediaplayer的类,二者之间采用广播的形式进行数据更新
③ TextAdapter继承BaseAdapter,在原来的基础之上给每一个数据项添加监听,作为信息更改,相应的需要将对应的歌曲序号广播,传回MusicService的广播接收器,进行数据更新
④ 由于偷懒,歌曲的原始数据我在MainActivity和MusicService都获取了一次,有需要可以自己实现数据共享
⑤ 进度条以及按钮点击和列表项点击均是传回control信号给MusicService进行更新,然后MusicService传回update信号要求MainActivity更新歌曲文本
⑥ 需要开启一个线程来不断刷新进度条
实现步骤
① 进入manifest.xml 之中添加权限和服务
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true" />
② 设置列表子项布局
item.xml 两个文本框分别显示歌曲名称以及作者,最后一个文本框作为分割
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:id="@+id/ll1"
android:orientation="vertical">
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
</LinearLayout>
③ 主页面添加进度条,列表以及七个按钮 其中进度条进度点换成了一张icon
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#276893"
android:textSize="20sp" />
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#4a6f5d"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/currenPosition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:thumb="@drawable/pig"
android:thumbOffset="0dp"
android:layout_height="wrap_content"
android:layout_weight="6" />
<TextView
android:id="@+id/duration"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="450dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_btn"
android:src="@drawable/previous_button" />
<ImageButton
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_btn"
android:src="@drawable/play_button" />
<ImageButton
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_btn"
android:src&