问题描述
我正在构建一个音乐播放器应用。
在那里,有各种细分:
现在,我已经设置好了一切,我可以播放“所有”歌曲等......
我想要的
当用户在:
播放列表 - 仅播放该播放列表中的歌曲列表
收藏夹 - 仅播放收藏夹中的歌曲列表
艺人歌曲 - 仅播放艺人的歌曲列表
我有各个片段的歌曲列表
现在,我只能播放用户媒体中的所有歌曲......所以,队列来自设备中的所有歌曲......我不想要。
我对ALL SONGS Segment 没有任何问题...
示例
最喜欢的歌曲片段
private var allSongs: List<Songs>? = null
roomFavSongviewmodel.getAllFavSongs.observe(viewLifecycleOwner) { songs ->
allSongs = songs .....//Now I have a list of songs a user has marked as favorite...Is there a way I can add this as the queue for exoplayer to play only this list of songs,instead of all songs from media
}
favouriteSongsAdapter.setonItemClickListener {
mainviewmodel.playOrToggleSong(it)..//On click of a song from the list of song
}
主视图模型
class Mainviewmodel @viewmodelInject constructor(
private val musicServiceConnection: MusicServiceConnection,) : viewmodel() {
private val _mediaItems = mutablelivedata<Resource<List<Songs>>>()
val mediaItems: LiveData<Resource<List<Songs>>> = _mediaItems
val isConnected = musicServiceConnection.isConnected
val networkError = musicServiceConnection.networkError
val currentlyPlayingSong = musicServiceConnection.currentPlayingSong
val playBackState = musicServiceConnection.playbackState
init {
_mediaItems.postValue(Resource.loading(null))
musicServiceConnection.subscribe(
MEDIA_ROOT_ID,object : MediabrowserCompat.SubscriptionCallback() {
override fun onChildrenLoaded(
parentId: String,children: MutableList<MediabrowserCompat.MediaItem>
) {
super.onChildrenLoaded(parentId,children)
val items = children.map {
val album = it.description.extras?.get("album")
val duration = it.description.extras?.get("duration")
val size = it.description.extras?.get("size")
val dateAdded = it.description.extras?.get("dateAdded")
Songs(
mediaId = it.mediaId?.toLong(),title = it.description.title as String?,subtitle = it.description.subtitle as String?,songUri = it.description.mediaUri,albumartUri = it.description.iconUri,duration = duration as Long?,album = album as String?,size = size as Long?,dateAdded = dateAdded as String?
)
}
_mediaItems.value = Resource.success(items)
}
})
}
fun skipToNextSong(){
musicServiceConnection.transportControls.skipToNext()
}
fun setShuffle(){
musicServiceConnection.transportControls.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_ALL)
}
fun offShuffle(){
musicServiceConnection.transportControls.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE)
}
fun setRepeat(){
musicServiceConnection.transportControls.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_ONE)
}
fun offRepeat(){
musicServiceConnection.transportControls.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE)
}
fun skipToPrevIoUsSong(){
musicServiceConnection.transportControls.skipToPrevIoUs()
}
fun seekTo(pos: Long){
musicServiceConnection.transportControls.seekTo(pos)
}
fun playOrToggleSong(
mediaItem: Songs,toggle: Boolean = false
){
val isPrepared = playBackState.value?.isPrepared ?: false
if (isPrepared && mediaItem.mediaId.toString() ==
currentlyPlayingSong.value?.getString(MetaDATA_KEY_MEDIA_ID)){
playBackState.value?.let { playbackState ->
when{
playbackState.isPlaying -> if (toggle) musicServiceConnection.transportControls.pause()
playbackState.isPlayEnabled -> musicServiceConnection.transportControls.play()
else -> Unit
}
}
}
else{
musicServiceConnection.transportControls.playFromMediaId(
mediaItem.mediaId.toString(),null
)
}
}
override fun onCleared() {
super.onCleared()
musicServiceConnection.unSubscribe(
MEDIA_ROOT_ID,object : MediabrowserCompat.SubscriptionCallback() {
})
}
}
有没有办法告诉 exoplayer 从特定的歌曲列表中建立它的队列??
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)