diff --git a/app/src/main/java/com/zs/zs_jetpack/play/PlayList.kt b/app/src/main/java/com/zs/zs_jetpack/play/PlayList.kt index 1dc1a74..ca5ec75 100644 --- a/app/src/main/java/com/zs/zs_jetpack/play/PlayList.kt +++ b/app/src/main/java/com/zs/zs_jetpack/play/PlayList.kt @@ -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 播放列表 @@ -15,18 +18,75 @@ import com.zs.zs_jetpack.play.bean.AudioBean class PlayList constructor(context: Context) { /** - * 播放列表 + * 当前播放列表 */ - private var audioList = mutableListOf() + private var currentAudioList = mutableListOf() + + /** + * 默认播放列表,本地资源 + */ + private var localList = mutableListOf() + + /** + * 默认播放列表,收藏 + */ + private var collectList = mutableListOf() + + /** + * 默认播放列表,历史 + */ + private var historyList = mutableListOf() /** * 播放模式,默认为顺序播放 */ 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) { + currentAudioList.apply { + clear() + addAll(list) + } } /** @@ -35,9 +95,10 @@ class PlayList constructor(context: Context) { private var currentAudio: AudioBean? = null /** - * 当前的角标 + * 当前播放音频对象在对应播放列表的角标 */ private var currentIndex = 0 + /** * 当前正在播放的音频 */ @@ -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 } @@ -67,25 +131,26 @@ 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 } @@ -93,25 +158,26 @@ class PlayList constructor(context: Context) { * 上一个音频 */ 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 } @@ -142,7 +208,7 @@ class PlayList constructor(context: Context) { /** * 获取当前播放模式 */ - fun getCurrentMode(): Int{ + fun getCurrentMode(): Int { return playMode } @@ -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 } } @@ -171,14 +239,14 @@ class PlayList constructor(context: Context) { * 获取播放列表长度 */ fun getPlayListSize(): Int { - return audioList.size + return currentAudioList.size } /** - * 获取播放列表长度 + * 获取播放列表 */ fun getPlayList(): MutableList { - return audioList + return currentAudioList } diff --git a/app/src/main/java/com/zs/zs_jetpack/play/PlayerManager.kt b/app/src/main/java/com/zs/zs_jetpack/play/PlayerManager.kt index b5d4278..eb7b2a1 100644 --- a/app/src/main/java/com/zs/zs_jetpack/play/PlayerManager.kt +++ b/app/src/main/java/com/zs/zs_jetpack/play/PlayerManager.kt @@ -28,8 +28,7 @@ class PlayerManager private constructor() : IPlayerStatus { PlayerManager() } - //TODO 播放器状态,当前共4种,可在此处随时扩展 - + //播放器状态,当前共4种,可在此处随时扩展 /** * 重置 */ diff --git a/app/src/main/java/com/zs/zs_jetpack/play/ReadAudioListExt.kt b/app/src/main/java/com/zs/zs_jetpack/play/ReadAudioListExt.kt index c5edb17..37b019c 100644 --- a/app/src/main/java/com/zs/zs_jetpack/play/ReadAudioListExt.kt +++ b/app/src/main/java/com/zs/zs_jetpack/play/ReadAudioListExt.kt @@ -18,36 +18,40 @@ import kotlinx.coroutines.launch /** * 通过ContentProvider读取本地音频文件 */ -fun readPlayList(context:Context,audioList:MutableList){ - 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 { + val audioList = mutableListOf() + + 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 } \ No newline at end of file diff --git a/app/src/main/java/com/zs/zs_jetpack/play/bean/AudioBean.kt b/app/src/main/java/com/zs/zs_jetpack/play/bean/AudioBean.kt index 1aa9cbd..718d073 100644 --- a/app/src/main/java/com/zs/zs_jetpack/play/bean/AudioBean.kt +++ b/app/src/main/java/com/zs/zs_jetpack/play/bean/AudioBean.kt @@ -1,5 +1,7 @@ package com.zs.zs_jetpack.play.bean +import com.zs.zs_jetpack.play.PlayListType + /** * des mp3文件封装类,遵循mp3格式,从ContentProvider获取 * @author zs @@ -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)" } diff --git a/app/src/main/java/com/zs/zs_jetpack/ui/main/home/HomeFragment.kt b/app/src/main/java/com/zs/zs_jetpack/ui/main/home/HomeFragment.kt index cb1b8b8..b0edf6e 100644 --- a/app/src/main/java/com/zs/zs_jetpack/ui/main/home/HomeFragment.kt +++ b/app/src/main/java/com/zs/zs_jetpack/ui/main/home/HomeFragment.kt @@ -49,7 +49,6 @@ class HomeFragment : LazyVmFragment(), BGABanner.Adapter, }) } - override fun lazyInit() { initView() loadData()