|
|
|
@ -50,37 +50,6 @@ class MusicService : MediaBrowserServiceCompat() { |
|
|
|
fun isPlaying() = currentStatus |
|
|
|
fun getCurrentId(): Long? = currentIdInternal |
|
|
|
|
|
|
|
// // 获取播放进度信息 |
|
|
|
// fun getProgress(): Map<String, Any> { |
|
|
|
// return if (instance != null && instance!!.mediaPlayer != null) { |
|
|
|
// val player = instance!!.mediaPlayer!! |
|
|
|
// try { |
|
|
|
// val currentPosition = if (player.isPlaying || currentStatus) player.currentPosition else 0 |
|
|
|
// val duration = player.duration |
|
|
|
// val progress = if (duration > 0) currentPosition.toDouble() / duration.toDouble() else 0.0 |
|
|
|
|
|
|
|
// mapOf( |
|
|
|
// "currentPosition" to currentPosition, |
|
|
|
// "duration" to duration, |
|
|
|
// "progress" to progress |
|
|
|
// ) |
|
|
|
// } catch (e: Exception) { |
|
|
|
// Log.e(TAG, "获取播放进度失败: ${e.message}") |
|
|
|
// mapOf( |
|
|
|
// "currentPosition" to 0, |
|
|
|
// "duration" to 0, |
|
|
|
// "progress" to 0.0 |
|
|
|
// ) |
|
|
|
// } |
|
|
|
// } else { |
|
|
|
// mapOf( |
|
|
|
// "currentPosition" to 0, |
|
|
|
// "duration" to 0, |
|
|
|
// "progress" to 0.0 |
|
|
|
// ) |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
private var instance: MusicService? = null |
|
|
|
private var flutterChannel: io.flutter.plugin.common.MethodChannel? = null |
|
|
|
|
|
|
|
@ -544,17 +513,70 @@ private fun updateProgress() { |
|
|
|
} |
|
|
|
|
|
|
|
private fun play(song: Map<String,Any>) { |
|
|
|
val url = song["url"] as? String ?: return |
|
|
|
Log.i(TAG, "开始播放: $url") |
|
|
|
val id = (song["id"] as? Number)?.toLong() ?: 0L |
|
|
|
Log.i(TAG, "开始播放play id: $id") |
|
|
|
val title = song["title"] as? String ?: "未知标题" |
|
|
|
val artist = song["artist"] as? String ?: "未知歌手" |
|
|
|
val coverUrl = song["coverUrl"] as String |
|
|
|
|
|
|
|
// 检查 URL 和时间戳 |
|
|
|
val url = song["url"] as? String ?: "" |
|
|
|
val ts = song["ts"] as? Long |
|
|
|
val currentTime = System.currentTimeMillis() / 1000 // 当前时间戳(秒) |
|
|
|
|
|
|
|
// 判断是否需要重新获取 URL |
|
|
|
val needNewUrl = url.isEmpty() || ts == null || ts <= currentTime |
|
|
|
|
|
|
|
if (needNewUrl) { |
|
|
|
Log.i(TAG, "URL 不存在或已过期,重新获取 URL") |
|
|
|
// 调用 MusicSingleton 获取新的 URL |
|
|
|
MusicSingleton.getInstance().getSongUrl(id) { result -> |
|
|
|
result.fold( |
|
|
|
onSuccess = { urlData -> |
|
|
|
val newUrl = urlData["url"] as? String |
|
|
|
if (!newUrl.isNullOrEmpty()) { |
|
|
|
// 更新 song 数据 |
|
|
|
val updatedSong = song.toMutableMap().apply { |
|
|
|
put("url", newUrl) |
|
|
|
put("ts", urlData["ts"] ?: 0L) |
|
|
|
} |
|
|
|
// 更新 playlist 中对应的歌曲数据 |
|
|
|
updatePlaylistSong(id, updatedSong) |
|
|
|
// 使用新 URL 开始播放 |
|
|
|
startPlayback(updatedSong, newUrl, title, artist, coverUrl, id) |
|
|
|
} else { |
|
|
|
Log.e(TAG, "获取到的 URL 为空") |
|
|
|
} |
|
|
|
}, |
|
|
|
onFailure = { exception -> |
|
|
|
Log.e(TAG, "获取 URL 失败: ${exception.message}") |
|
|
|
} |
|
|
|
) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// URL 有效,直接播放 |
|
|
|
startPlayback(song, url, title, artist, coverUrl, id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 新增方法:更新 playlist 中指定 ID 的歌曲数据 |
|
|
|
private fun updatePlaylistSong(songId: Long, updatedSong: Map<String, Any>) { |
|
|
|
for (i in playlist.indices) { |
|
|
|
val playlistSong = playlist[i] |
|
|
|
val playlistSongId = (playlistSong["id"] as? Number)?.toLong() ?: 0L |
|
|
|
if (playlistSongId == songId) { |
|
|
|
// 找到匹配的歌曲,更新数据 |
|
|
|
playlist[i] = updatedSong.toMap() |
|
|
|
Log.i(TAG, "已更新 playlist 中 ID 为 $songId 的歌曲数据") |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private fun startPlayback(song: Map<String,Any>, url: String, title: String, artist: String, coverUrl: String, id: Long) { |
|
|
|
mediaPlayer?.release() |
|
|
|
currentStatus = true |
|
|
|
currentIdInternal = id |
|
|
|
Log.i(TAG, "开始播放play currentIdInternal: $currentIdInternal") |
|
|
|
Log.i(TAG, "开始播放play currentIdInternal: $currentIdInternal") |
|
|
|
mediaPlayer = MediaPlayer().apply { |
|
|
|
setDataSource(url) |
|
|
|
setAudioStreamType(AudioManager.STREAM_MUSIC) |
|
|
|
|