Skip to content

Commit

Permalink
重构播放列表
Browse files Browse the repository at this point in the history
  • Loading branch information
zskingking committed Sep 13, 2020
1 parent 799f1d8 commit c7c2b29
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 65 deletions.
134 changes: 101 additions & 33 deletions app/src/main/java/com/zs/zs_jetpack/play/PlayList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.zs.base_library.common.getRandom
import com.zs.base_library.common.isListEmpty
import com.zs.base_library.common.toast
import com.zs.zs_jetpack.play.bean.AudioBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
* des 播放列表
Expand All @@ -15,18 +18,75 @@ import com.zs.zs_jetpack.play.bean.AudioBean
class PlayList constructor(context: Context) {

/**
* 播放列表
* 当前播放列表
*/
private var audioList = mutableListOf<AudioBean>()
private var currentAudioList = mutableListOf<AudioBean>()

/**
* 默认播放列表,本地资源
*/
private var localList = mutableListOf<AudioBean>()

/**
* 默认播放列表,收藏
*/
private var collectList = mutableListOf<AudioBean>()

/**
* 默认播放列表,历史
*/
private var historyList = mutableListOf<AudioBean>()

/**
* 播放模式,默认为顺序播放
*/
private var playMode = PlayMode.ORDER_PLAY_MODE

/**
* 播放列表,默认为本地播放
*/
private var playListType = PlayListType.LOCAL_PLAY_LIST


init {
//读取播放列表
readPlayList(context, audioList)
//通过io线程读取播放列表
GlobalScope.launch(Dispatchers.IO) {
Log.i("PlayList","${System.currentTimeMillis()}")
//读取本地播放列表
localList = readPlayListByLocal(context)
Log.i("PlayList","${System.currentTimeMillis()}")
switchPlayList(playListType)
}
}

/**
* 切换播放列表
*/
private fun switchPlayList(playListType: Int) {
when (playListType) {
//本地列表
PlayListType.LOCAL_PLAY_LIST -> {
replacePlayList(localList)
}
//收藏列表
PlayListType.COLLECT_PLAY_LIST -> {
replacePlayList(collectList)
}
//历史列表
PlayListType.HISTORY_PLAY_LIST -> {
replacePlayList(historyList)
}
}
}

/**
* 替换播放列表
*/
private fun replacePlayList(list: MutableList<AudioBean>) {
currentAudioList.apply {
clear()
addAll(list)
}
}

/**
Expand All @@ -35,9 +95,10 @@ class PlayList constructor(context: Context) {
private var currentAudio: AudioBean? = null

/**
* 当前的角标
* 当前播放音频对象在对应播放列表的角标
*/
private var currentIndex = 0

/**
* 当前正在播放的音频
*/
Expand All @@ -46,19 +107,22 @@ class PlayList constructor(context: Context) {
}

/**
* 设置当前index
* 设置当前播放列表和currentIndex
*/
fun setCurrentAudio(audioBean: AudioBean){
currentAudio = audioBean
currentIndex = getIndexByAudio(audioBean)
fun setCurrentAudio(audioBean: AudioBean) {
GlobalScope.launch (Dispatchers.IO){
//每次切换都做播放列表更新
switchPlayList(audioBean.playListType)
currentIndex = getIndexByAudio(audioBean)
}
}

/**
* 第一次进入播放的音频,默认为播放列表的第一个
*/
fun startAudio(): AudioBean? {
if (!isListEmpty(audioList)) {
currentAudio = audioList[0]
if (!isListEmpty(currentAudioList)) {
currentAudio = currentAudioList[0]
}
return currentAudio
}
Expand All @@ -67,51 +131,53 @@ class PlayList constructor(context: Context) {
* 下一个音频
*/
fun nextAudio(): AudioBean? {
if (!isListEmpty(audioList)) {
when(playMode){
if (!isListEmpty(currentAudioList)) {
when (playMode) {
//顺序
PlayMode.ORDER_PLAY_MODE->{
currentIndex = if (currentIndex < audioList.size - 1) {
PlayMode.ORDER_PLAY_MODE -> {
currentIndex = if (currentIndex < currentAudioList.size - 1) {
currentIndex + 1
} else {
0
}
}
//单曲(不做处理)
PlayMode.SINGLE_PLAY_MODE->{ }
PlayMode.SINGLE_PLAY_MODE -> {
}
//随机
PlayMode.RANDOM_PLAY_MODE->{
currentIndex = getRandom(0,audioList.size-1)
PlayMode.RANDOM_PLAY_MODE -> {
currentIndex = getRandom(0, currentAudioList.size - 1)
}
}
}
currentAudio = audioList[currentIndex]
currentAudio = currentAudioList[currentIndex]
return currentAudio
}

/**
* 上一个音频
*/
fun previousAudio(): AudioBean? {
if (!isListEmpty(audioList)) {
when(playMode){
if (!isListEmpty(currentAudioList)) {
when (playMode) {
//顺序
PlayMode.ORDER_PLAY_MODE->{
PlayMode.ORDER_PLAY_MODE -> {
currentIndex = if (currentIndex > 0) {
currentIndex - 1
} else {
audioList.size-1
currentAudioList.size - 1
}
}
//单曲(不做处理)
PlayMode.SINGLE_PLAY_MODE->{ }
PlayMode.SINGLE_PLAY_MODE -> {
}
//随机
PlayMode.RANDOM_PLAY_MODE->{
currentIndex = getRandom(0,audioList.size-1)
PlayMode.RANDOM_PLAY_MODE -> {
currentIndex = getRandom(0, currentAudioList.size - 1)
}
}
}
currentAudio = audioList[currentIndex]
currentAudio = currentAudioList[currentIndex]
return currentAudio
}

Expand Down Expand Up @@ -142,7 +208,7 @@ class PlayList constructor(context: Context) {
/**
* 获取当前播放模式
*/
fun getCurrentMode(): Int{
fun getCurrentMode(): Int {
return playMode
}

Expand All @@ -158,8 +224,10 @@ class PlayList constructor(context: Context) {
* 之所以没有全局开放一个index,是为了尽可能的降低 index 的操作权限
*/
private fun getIndexByAudio(audioBean: AudioBean): Int {
for (index in 0 until audioList.size) {
if (audioBean == audioList[index]) {
//设置当前正在播放的对象
currentAudio = audioBean
for (index in 0 until currentAudioList.size) {
if (audioBean == currentAudioList[index]) {
return index
}
}
Expand All @@ -171,14 +239,14 @@ class PlayList constructor(context: Context) {
* 获取播放列表长度
*/
fun getPlayListSize(): Int {
return audioList.size
return currentAudioList.size
}

/**
* 获取播放列表长度
* 获取播放列表
*/
fun getPlayList(): MutableList<AudioBean> {
return audioList
return currentAudioList
}


Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/zs/zs_jetpack/play/PlayerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class PlayerManager private constructor() : IPlayerStatus {
PlayerManager()
}

//TODO 播放器状态,当前共4种,可在此处随时扩展

//播放器状态,当前共4种,可在此处随时扩展
/**
* 重置
*/
Expand Down
62 changes: 33 additions & 29 deletions app/src/main/java/com/zs/zs_jetpack/play/ReadAudioListExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,40 @@ import kotlinx.coroutines.launch
/**
* 通过ContentProvider读取本地音频文件
*/
fun readPlayList(context:Context,audioList:MutableList<AudioBean>){
GlobalScope.launch {
val cursor: Cursor? = context.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER
)
if (cursor != null) {
while (cursor.moveToNext()) {
val audioBean = AudioBean()
audioBean.name =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
audioBean.id =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID))
audioBean.singer =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
audioBean.path =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
audioBean.duration =
cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
audioBean.size =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE))
audioBean.albumId =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
//筛选大于一分钟的音频
if (audioBean.duration>60000){
audioList.add(audioBean)
}
fun readPlayListByLocal(context: Context): MutableList<AudioBean> {
val audioList = mutableListOf<AudioBean>()

val cursor: Cursor? = context.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER
)
if (cursor != null) {
while (cursor.moveToNext()) {
val audioBean = AudioBean()
audioBean.name =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
audioBean.id =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID))
audioBean.singer =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
audioBean.path =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
audioBean.duration =
cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
audioBean.size =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE))
audioBean.albumId =
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))
//设置播放列表
audioBean.playListType = PlayListType.LOCAL_PLAY_LIST
//筛选大于一分钟的音频
if (audioBean.duration > 60000) {
audioList.add(audioBean)
}
cursor.close()
Log.i("PlayList","$audioList")
}
cursor.close()
Log.i("PlayList", "$audioList")

}
return audioList
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/zs/zs_jetpack/play/bean/AudioBean.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zs.zs_jetpack.play.bean

import com.zs.zs_jetpack.play.PlayListType

/**
* des mp3文件封装类,遵循mp3格式,从ContentProvider获取
* @author zs
Expand Down Expand Up @@ -35,6 +37,11 @@ class AudioBean {
*/
var id: Long = 0

/**
* 所属播放列表
*/
var playListType = PlayListType.LOCAL_PLAY_LIST

override fun toString(): String {
return "\nAudioBean(name=$name, singer=$singer, size=$size, duration=$duration, path=$path, albumId=$albumId, id=$id)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class HomeFragment : LazyVmFragment(), BGABanner.Adapter<ImageView?, String?>,
})
}


override fun lazyInit() {
initView()
loadData()
Expand Down

0 comments on commit c7c2b29

Please sign in to comment.