修复download地址获取。
This commit is contained in:
parent
5006cf3e11
commit
a4427edd1b
@ -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) {
|
// when (val status = body.playabilityStatus?.status) {
|
||||||
"OK" -> body.streamingData?.highestQualityFormat?.let { format ->
|
// "OK" -> body.streamingData?.highestQualityFormat?.let { format ->
|
||||||
format.url
|
// format.url
|
||||||
} ?: throw PlayableFormatNotFoundException()
|
// } ?: throw PlayableFormatNotFoundException()
|
||||||
|
//
|
||||||
"UNPLAYABLE" -> throw UnplayableException()
|
// "UNPLAYABLE" -> throw UnplayableException()
|
||||||
"LOGIN_REQUIRED" -> throw LoginRequiredException()
|
// "LOGIN_REQUIRED" -> throw LoginRequiredException()
|
||||||
else -> throw PlaybackException(
|
// else -> throw PlaybackException(
|
||||||
status,
|
// status,
|
||||||
null,
|
// null,
|
||||||
PlaybackException.ERROR_CODE_REMOTE_ERROR
|
// PlaybackException.ERROR_CODE_REMOTE_ERROR
|
||||||
)
|
// )
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
LogTag.LogD(TAG, "DownloadUtil urlResult->$urlResult")
|
// LogTag.LogD(TAG, "DownloadUtil urlResult->$urlResult")
|
||||||
|
//
|
||||||
urlResult?.getOrThrow()?.let { url ->
|
// urlResult?.getOrThrow()?.let { url ->
|
||||||
ringBuffer.append(videoId to url.toUri())
|
// ringBuffer.append(videoId to url.toUri())
|
||||||
dataSpec.withUri(url.toUri())
|
// dataSpec.withUri(url.toUri())
|
||||||
} ?: throw PlaybackException(
|
// } ?: throw PlaybackException(
|
||||||
null,
|
// null,
|
||||||
urlResult?.exceptionOrNull(),
|
// urlResult?.exceptionOrNull(),
|
||||||
PlaybackException.ERROR_CODE_REMOTE_ERROR
|
// PlaybackException.ERROR_CODE_REMOTE_ERROR
|
||||||
)
|
// )
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 进入协程安全调用你提供的获取方法
|
||||||
|
val playbackResult = runBlocking(Dispatchers.IO) {
|
||||||
|
playerResponseForPlayback(
|
||||||
|
videoId = videoId,
|
||||||
|
connectivityManager = context.getSystemService()!!
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val playbackData = playbackResult.getOrElse { ex ->
|
||||||
|
Log.e(TAG, "Failed to get playback data", ex)
|
||||||
|
throw PlaybackException(
|
||||||
|
ex.message ?: "Failed to get playback data",
|
||||||
|
ex,
|
||||||
|
PlaybackException.ERROR_CODE_REMOTE_ERROR
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val streamUrl = playbackData.streamUrl
|
||||||
|
if (streamUrl.isEmpty()) {
|
||||||
|
throw PlaybackException("Stream URL is empty", null, PlaybackException.ERROR_CODE_REMOTE_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
val uri = streamUrl.toUri()
|
||||||
|
|
||||||
|
// 缓存
|
||||||
|
ringBuffer.append(videoId to uri)
|
||||||
|
|
||||||
|
LogD(TAG, "Resolved URL for $videoId: $streamUrl")
|
||||||
|
dataSpec.withUri(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user