修复download地址获取。

This commit is contained in:
ocean 2025-06-12 14:33:11 +08:00
parent 5006cf3e11
commit a4427edd1b

View File

@ -3,7 +3,9 @@ package melody.offline.music.util
import android.app.Notification import android.app.Notification
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.util.Log
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.core.content.getSystemService
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.media3.common.PlaybackException import androidx.media3.common.PlaybackException
import androidx.media3.common.util.NotificationUtil import androidx.media3.common.util.NotificationUtil
@ -36,6 +38,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import melody.offline.music.App import melody.offline.music.App
import melody.offline.music.playernew.YTPlayerUtils.playerResponseForPlayback
import melody.offline.music.util.LogTag.LogD
import java.net.CookieHandler import java.net.CookieHandler
import java.net.CookieManager import java.net.CookieManager
import java.net.CookiePolicy import java.net.CookiePolicy
@ -103,40 +107,70 @@ object DownloadUtil {
when (videoId) { when (videoId) {
ringBuffer.getOrNull(0)?.first -> dataSpec.withUri(ringBuffer.getOrNull(0)!!.second) ringBuffer.getOrNull(0)?.first -> dataSpec.withUri(ringBuffer.getOrNull(0)!!.second)
ringBuffer.getOrNull(1)?.first -> dataSpec.withUri(ringBuffer.getOrNull(1)!!.second) ringBuffer.getOrNull(1)?.first -> dataSpec.withUri(ringBuffer.getOrNull(1)!!.second)
else -> { // else -> {
val urlResult = runBlocking(Dispatchers.IO) { // val urlResult = runBlocking(Dispatchers.IO) {
Innertube.player(PlayerBody(videoId = videoId)) // Innertube.player(PlayerBody(videoId = videoId))
}?.mapCatching { body -> // }?.mapCatching { body ->
if (body.videoDetails?.videoId != videoId) { // if (body.videoDetails?.videoId != videoId) {
throw VideoIdMismatchException() // throw VideoIdMismatchException()
// }
//
// when (val status = body.playabilityStatus?.status) {
// "OK" -> body.streamingData?.highestQualityFormat?.let { format ->
// format.url
// } ?: throw PlayableFormatNotFoundException()
//
// "UNPLAYABLE" -> throw UnplayableException()
// "LOGIN_REQUIRED" -> throw LoginRequiredException()
// else -> throw PlaybackException(
// status,
// null,
// PlaybackException.ERROR_CODE_REMOTE_ERROR
// )
// }
// }
// LogTag.LogD(TAG, "DownloadUtil urlResult->$urlResult")
//
// urlResult?.getOrThrow()?.let { url ->
// ringBuffer.append(videoId to url.toUri())
// dataSpec.withUri(url.toUri())
// } ?: throw PlaybackException(
// null,
// urlResult?.exceptionOrNull(),
// PlaybackException.ERROR_CODE_REMOTE_ERROR
// )
// }
} }
when (val status = body.playabilityStatus?.status) { // 进入协程安全调用你提供的获取方法
"OK" -> body.streamingData?.highestQualityFormat?.let { format -> val playbackResult = runBlocking(Dispatchers.IO) {
format.url playerResponseForPlayback(
} ?: throw PlayableFormatNotFoundException() videoId = videoId,
connectivityManager = context.getSystemService()!!
)
}
"UNPLAYABLE" -> throw UnplayableException() val playbackData = playbackResult.getOrElse { ex ->
"LOGIN_REQUIRED" -> throw LoginRequiredException() Log.e(TAG, "Failed to get playback data", ex)
else -> throw PlaybackException( throw PlaybackException(
status, ex.message ?: "Failed to get playback data",
null, ex,
PlaybackException.ERROR_CODE_REMOTE_ERROR PlaybackException.ERROR_CODE_REMOTE_ERROR
) )
} }
}
LogTag.LogD(TAG, "DownloadUtil urlResult->$urlResult")
urlResult?.getOrThrow()?.let { url -> val streamUrl = playbackData.streamUrl
ringBuffer.append(videoId to url.toUri()) if (streamUrl.isEmpty()) {
dataSpec.withUri(url.toUri()) throw PlaybackException("Stream URL is empty", null, PlaybackException.ERROR_CODE_REMOTE_ERROR)
} ?: throw PlaybackException(
null,
urlResult?.exceptionOrNull(),
PlaybackException.ERROR_CODE_REMOTE_ERROR
)
}
} }
val uri = streamUrl.toUri()
// 缓存
ringBuffer.append(videoId to uri)
LogD(TAG, "Resolved URL for $videoId: $streamUrl")
dataSpec.withUri(uri)
} }
} }