Skip to content

Commit

Permalink
修复弱网下播放webdav的歌曲可能anr
Browse files Browse the repository at this point in the history
  • Loading branch information
rRemix committed Aug 10, 2024
1 parent b603856 commit ce005ab
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/remix/myplayer/bean/mp3/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sealed class Song(

val genre: String
get() {
if (_genre.isNullOrEmpty() && id > 0 && data.isNotEmpty()) {
if (_genre.isNullOrEmpty() && id > 0 && data.isNotEmpty() && this !is Remote) {
val metadataRetriever = MediaMetadataRetriever()
try {
metadataRetriever.setDataSource(data)
Expand Down Expand Up @@ -138,7 +138,7 @@ sealed class Song(
}

is Remote -> {
Remote(title, album, artist, duration, data, size, year, genre, track, dateModified, account, pwd)
Remote(title, album, artist, _duration, data, size, year, _genre ?: "", track, dateModified, account, pwd)
}

}
Expand Down
26 changes: 14 additions & 12 deletions app/src/main/java/remix/myplayer/glide/UriFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,22 @@ object UriFetcher {
}

private fun fetch(song: Song): Uri {
// 自定义封面
val customArtFile = ImageUriUtil.getCustomThumbIfExist(song.albumId, TYPE_ALBUM)
if (customArtFile != null && customArtFile.exists()) {
return Uri.fromFile(customArtFile)
}
if (song.isLocal()) { // 仅本地歌曲
// 自定义封面
val customArtFile = ImageUriUtil.getCustomThumbIfExist(song.albumId, TYPE_ALBUM)
if (customArtFile != null && customArtFile.exists()) {
return Uri.fromFile(customArtFile)
}

// 内置
if (ignoreMediaStore()) {
val songs = getSongs(Audio.Media._ID + "=" + song.id, null)
if (songs.isNotEmpty()) {
return Uri.parse(PREFIX_EMBEDDED + songs[0].data)
// 内置
if (ignoreMediaStore()) {
val songs = getSongs(Audio.Media._ID + "=" + song.id, null)
if (songs.isNotEmpty()) {
return Uri.parse(PREFIX_EMBEDDED + songs[0].data)
}
} else if (ImageUriUtil.isAlbumThumbExistInMediaCache(song.artUri)) {
return song.artUri
}
} else if (ImageUriUtil.isAlbumThumbExistInMediaCache(song.artUri)) {
return song.artUri
}

// 网络
Expand Down
36 changes: 26 additions & 10 deletions app/src/main/java/remix/myplayer/service/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
override fun onPrepared(mp: MediaPlayer?) {
Timber.v("准备完成:%s", firstPrepared)

prepared = true

if (firstPrepared) {
firstPrepared = false
if (lastProgress > 0) {
Expand Down Expand Up @@ -867,6 +869,10 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
*/
override fun play(fadeIn: Boolean) {
Timber.v("play: $fadeIn")
if (!prepared) {
ToastUtil.show(this, getString(R.string.buffering_wait))
return
}
audioFocus = AudioManagerCompat.requestAudioFocus(
audioManager,
focusRequest
Expand Down Expand Up @@ -1459,7 +1465,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
private fun prepare(song: Song, requestFocus: Boolean = true) {
tryLaunch(
block = {
Timber.v("准备播放: %s", song)
Timber.v("prepare start: %s", song)
if (TextUtils.isEmpty(song.data)) {
ToastUtil.show(service, getString(R.string.path_empty))
return@tryLaunch
Expand All @@ -1486,8 +1492,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
mediaPlayer.playbackParams = PlaybackParams().setSpeed(speed)
}
mediaPlayer.prepareAsync()
prepared = true
isLove = withContext(Dispatchers.IO) {
isLove = song.isLocal() && withContext(Dispatchers.IO) {
repository.isMyLove(playQueue.song.id)
.onErrorReturn {
false
Expand All @@ -1503,11 +1508,18 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
}

private suspend fun setDataSource(song: Song) = withContext(Dispatchers.IO) {
Timber.v("setDataSource: ${song.data}")
if (song.isLocal()) {
mediaPlayer.setDataSource(this@MusicService, song.contentUri)
} else if (song is Song.Remote) {
val start = System.currentTimeMillis()
mediaPlayer.setDataSource(this@MusicService, song.contentUri, song.headers)
retrieveRemoteSong(song, playQueue.song as Song.Remote)
Timber.v("setDataSource spend: ${System.currentTimeMillis() - start}")

// 可能会特别耗时
launch(Dispatchers.IO) {
retrieveRemoteSong(song, playQueue.song as Song.Remote)
}
}
}

Expand Down Expand Up @@ -1537,7 +1549,11 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
ToastUtil.show(service, R.string.song_lose_effect)
return
}
isPlaying = true
// 不能在这里设置playing,因为远程的歌曲可能需要缓冲,并且这里需要提前刷新下界面
if (playQueue.song.isRemote()) {
uiHandler.sendEmptyMessage(UPDATE_META_DATA)
}

prepare(playQueue.song)
}

Expand Down Expand Up @@ -2057,10 +2073,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
if (bitmap == null || bitmap.isRecycled) {
return null
}
var config: Bitmap.Config? = bitmap.config
if (config == null) {
config = Bitmap.Config.RGB_565
}
val config: Bitmap.Config = bitmap.config
return try {
bitmap.copy(config, false)
} catch (e: OutOfMemoryError) {
Expand All @@ -2071,6 +2084,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
}

fun retrieveRemoteSong(song: Song.Remote, targetSong: Song.Remote) {
Timber.v("retrieveRemoteSong: ${song.data}")
val start = System.currentTimeMillis()
val metadataRetriever = MediaMetadataRetriever()
try {
metadataRetriever.setDataSource(song.data, song.headers)
Expand Down Expand Up @@ -2115,8 +2130,9 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
dateModified
)
} catch (e: Exception) {
Timber.v("fail to retrieve: $e")
Timber.v("fail to retrieveRemoteSong data: ${song.data} detail: $e")
} finally {
Timber.v("retrieveRemoteSong spend:${System.currentTimeMillis() - start} ${song.data}")
metadataRetriever.release()
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,5 @@
<string name="can_t_be_empty">%s空にすることはできません</string>
<string name="connect">せつぞく</string>
<string name="edit">編集</string>
<string name="buffering_wait">バッファリングです,ちょっと待ってください</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,5 @@
<string name="edit">编辑</string>
<string name="speed_at_2x">2x倍速播放中</string>
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌词文件请确保放在公共目录中(如Downloads、Music等目录)</string>
<string name="buffering_wait">缓冲中,请稍后</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,5 @@
<string name="edit">編輯</string>
<string name="speed_at_2x">2x倍速播放中</string>
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌詞檔案請確保放在公共目錄中(如Downloads、Music等目錄)</string>
<string name="buffering_wait">緩衝中,請稍後</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,5 @@
<string name="edit">編輯</string>
<string name="speed_at_2x">2x倍速播放中</string>
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌詞檔案請確保放在公共目錄中(如Downloads、Music等目錄)</string>
<string name="buffering_wait">緩衝中,請稍後</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,5 @@
<string name="edit">Edit</string>
<string name="speed_at_2x">Playing at 2x speed</string>
<string name="local_lyric_tip">If your Android version is 11 or above, please make sure the local lyrics files are placed in the public directory (such as Downloads, Music, etc. directories)</string>
<string name="buffering_wait">Buffering, please wait</string>
</resources>

0 comments on commit ce005ab

Please sign in to comment.