Skip to content

Commit

Permalink
音乐播放
Browse files Browse the repository at this point in the history
  • Loading branch information
sskEvan committed Mar 19, 2023
1 parent 560d75a commit 7066812
Show file tree
Hide file tree
Showing 47 changed files with 629 additions and 1,043 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ kotlin {

implementation("androidx.datastore:datastore-preferences-core:1.1.0-dev01")
implementation("io.github.succlz123:compose-imageloader-desktop:0.0.2")
implementation("uk.co.caprica:vlcj:4.7.3")
}
}
val jvmTest by getting
Expand Down
Binary file modified cache/qrcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import base.AppConfig
Expand All @@ -11,7 +10,6 @@ import router.NCNavigatorManager
import ui.common.theme.AppTheme
import ui.common.theme.themeTypeState
import ui.main.MainPage
import ui.todo.TestPage
import java.awt.Dimension

fun main() = application {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package viewmodel
package base

import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down
141 changes: 119 additions & 22 deletions src/jvmMain/kotlin/base/MusicPlayController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import base.player.NCPlayer
import base.player.PlayMode
import base.player.*
import model.SongBean
import util.StringUtil
import java.util.*

object MusicPlayController {
object MusicPlayController : IPlayerListener {
// 是否显示音乐播放抽屉组件
var showMusicPlayDrawer by mutableStateOf(false)

var showPlayListSheet by mutableStateOf(false)
// 是否显示播当前播放列表
var showCurPlayListSheet by mutableStateOf(false)

// 原始歌曲列表
var originSongList = mutableStateListOf<SongBean>()
Expand All @@ -28,26 +28,38 @@ object MusicPlayController {
var curRealIndex by mutableStateOf(-1)
private set
// 当前播放进度
var progress by mutableStateOf(0)
var progress by mutableStateOf(0f)
// 当前歌曲播放位置时间文本
var curPositionStr by mutableStateOf("00:00")
// 当前歌曲总时长文本
var totalDuringStr by mutableStateOf("00:00")
// 是否播放中
private var playing by mutableStateOf(false)
// 是否允许拖动进度条
var enableSeeking by mutableStateOf(false)
private set
// 播放模式
var playMode by mutableStateOf(PlayMode.LOOP)
private set
// 当前播放歌曲总时长
private var totalDuring = 0
// 是否拖动进度条中
private var seeking = false
// 当前播放状态
private var playerStatus: PlayerStatus = PlayerStatus.IDLE

val mediaPlayer: IPlayer by lazy { NCPlayer().apply {
addListener(this@MusicPlayController)
} }


fun setDataSource(songBeans: List<SongBean>, originIndex: Int) {
/**
* 播放音乐列表
*/
fun playMusicList(songBeans: List<SongBean>, originIndex: Int) {
originSongList.clear()
originSongList.addAll(songBeans)
println("MusicPlayController setDataSource curOriginIndex=${originIndex}")
println("MusicPlayController playMusicList curOriginIndex=${originIndex}")
generateRealSongList(originIndex)
innerPlay(originSongList[originIndex])
}
Expand Down Expand Up @@ -78,10 +90,10 @@ object MusicPlayController {
curRealIndex = originIndex
}
}
originSongList.forEachIndexed { index, item ->
println("songList $index --> ${item.name}")
}
println("---------------------------------------")
// originSongList.forEachIndexed { index, item ->
// println("songList $index --> ${item.name}")
// }
// println("---------------------------------------")

realSongList.forEachIndexed { index, item ->
println("pagerSongList $index --> ${item.name}")
Expand All @@ -90,31 +102,116 @@ object MusicPlayController {

private fun innerPlay(songBean: SongBean) {
curSongBean = songBean
NCPlayer.setDataSource(songBean)
NCPlayer.start()
mediaPlayer.setDataSource(songBean)
mediaPlayer.start()
}

/**
* 根据原始歌曲列表索引播放音乐
*/
fun playByOriginIndex(originIndex: Int) {
if (originSongList.size > originIndex) {
curOriginIndex = originIndex
curRealIndex = realSongList.indexOfFirst { it.id == originSongList[originIndex].id }
innerPlay(originSongList[curOriginIndex])
}
}
// fun playByOriginIndex(originIndex: Int) {
// if (originSongList.size > originIndex) {
// curOriginIndex = originIndex
// curRealIndex = realSongList.indexOfFirst { it.id == originSongList[originIndex].id }
// innerPlay(originSongList[curOriginIndex])
// }
// }

/**
* 根据实际播放模式中的歌曲列表索引播放音乐
*/
fun playByRealIndex(realIndex: Int) {
if (originSongList.size > realIndex) {
if (originSongList.getOrNull(realIndex) != null) {
curRealIndex = realIndex
curOriginIndex = originSongList.indexOfFirst { it.id == realSongList[realIndex].id }
innerPlay(realSongList[curRealIndex])
}
}

override fun onStatusChanged(status: PlayerStatus) {
playerStatus = status
playing = status == PlayerStatus.STARTED
enableSeeking = status == PlayerStatus.STARTED || status == PlayerStatus.PAUSED
when (status) {
PlayerStatus.COMPLETED -> {
autoPlayNext()
}
PlayerStatus.ERROR -> {
autoPlayNext()
}
PlayerStatus.STOPPED -> {
totalDuringStr = "00:00"
curPositionStr = "00:00"
this.progress = 0f
}
else -> {}
}
}

private fun autoPlayNext() {
if(playMode == PlayMode.SINGLE) {
resume()
}else {
val newIndex = getNextRealIndex()
playByRealIndex(newIndex)
}
}

fun pause() {
if (playerStatus == PlayerStatus.STARTED) {
mediaPlayer.pause()
}
}

fun resume() {
if (playerStatus == PlayerStatus.PAUSED) {
mediaPlayer.resume()
}
}

fun isPlaying(): Boolean {
return playing
}

/**
* 获取当前播放模式下的上一首歌曲索引
*/
fun getPreRealIndex() = if (curRealIndex == 0) realSongList.size - 1 else curRealIndex - 1

/**
* 获取当前播放模式下的下一首歌曲索引
*/
fun getNextRealIndex() = if (curRealIndex == realSongList.size - 1) 0 else curRealIndex + 1

fun changePlayMode(playMode: PlayMode) {
this.playMode = playMode
generateRealSongList(curOriginIndex)
}

fun seekTo(progress: Float) {
this.progress = progress
if (totalDuring != 0) {
mediaPlayer.seekTo(progress)
}
seeking = false
}

fun seeking(progress: Float) {
seeking = true
this.progress = progress
if (totalDuring != 0) {
this.curPositionStr = StringUtil.formatMilliseconds((progress * totalDuring / 100).toInt())
}
}

override fun onProgress(totalDuring: Int, currentPosition: Int, percentage: Float) {
if (!seeking) {
this.totalDuring = totalDuring
totalDuringStr = StringUtil.formatMilliseconds(totalDuring)
curPositionStr = StringUtil.formatMilliseconds(currentPosition)
progress = percentage
}
}


}
5 changes: 4 additions & 1 deletion src/jvmMain/kotlin/base/player/IPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ interface IPlayer {
fun pause()
fun resume()
fun stop()
fun seekTo(position: Int)
fun seekTo(position: Float)

fun addListener(listener: IPlayerListener)
fun removeListener(listener: IPlayerListener)
}
2 changes: 1 addition & 1 deletion src/jvmMain/kotlin/base/player/IPlayerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package base.player
*/
interface IPlayerListener {
fun onStatusChanged(status: PlayerStatus)
fun onProgress(totalDuring: Int, currentPosition: Int, percentage: Int)
fun onProgress(totalDuring: Int, currentPosition: Int, percentage: Float)
}
Loading

0 comments on commit 7066812

Please sign in to comment.