Skip to content

Commit ce005ab

Browse files
committed
修复弱网下播放webdav的歌曲可能anr
1 parent b603856 commit ce005ab

File tree

8 files changed

+47
-24
lines changed

8 files changed

+47
-24
lines changed

app/src/main/java/remix/myplayer/bean/mp3/Song.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ sealed class Song(
8282

8383
val genre: String
8484
get() {
85-
if (_genre.isNullOrEmpty() && id > 0 && data.isNotEmpty()) {
85+
if (_genre.isNullOrEmpty() && id > 0 && data.isNotEmpty() && this !is Remote) {
8686
val metadataRetriever = MediaMetadataRetriever()
8787
try {
8888
metadataRetriever.setDataSource(data)
@@ -138,7 +138,7 @@ sealed class Song(
138138
}
139139

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

144144
}

app/src/main/java/remix/myplayer/glide/UriFetcher.kt

+14-12
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,22 @@ object UriFetcher {
145145
}
146146

147147
private fun fetch(song: Song): Uri {
148-
// 自定义封面
149-
val customArtFile = ImageUriUtil.getCustomThumbIfExist(song.albumId, TYPE_ALBUM)
150-
if (customArtFile != null && customArtFile.exists()) {
151-
return Uri.fromFile(customArtFile)
152-
}
148+
if (song.isLocal()) { // 仅本地歌曲
149+
// 自定义封面
150+
val customArtFile = ImageUriUtil.getCustomThumbIfExist(song.albumId, TYPE_ALBUM)
151+
if (customArtFile != null && customArtFile.exists()) {
152+
return Uri.fromFile(customArtFile)
153+
}
153154

154-
// 内置
155-
if (ignoreMediaStore()) {
156-
val songs = getSongs(Audio.Media._ID + "=" + song.id, null)
157-
if (songs.isNotEmpty()) {
158-
return Uri.parse(PREFIX_EMBEDDED + songs[0].data)
155+
// 内置
156+
if (ignoreMediaStore()) {
157+
val songs = getSongs(Audio.Media._ID + "=" + song.id, null)
158+
if (songs.isNotEmpty()) {
159+
return Uri.parse(PREFIX_EMBEDDED + songs[0].data)
160+
}
161+
} else if (ImageUriUtil.isAlbumThumbExistInMediaCache(song.artUri)) {
162+
return song.artUri
159163
}
160-
} else if (ImageUriUtil.isAlbumThumbExistInMediaCache(song.artUri)) {
161-
return song.artUri
162164
}
163165

164166
// 网络

app/src/main/java/remix/myplayer/service/MusicService.kt

+26-10
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
689689
override fun onPrepared(mp: MediaPlayer?) {
690690
Timber.v("准备完成:%s", firstPrepared)
691691

692+
prepared = true
693+
692694
if (firstPrepared) {
693695
firstPrepared = false
694696
if (lastProgress > 0) {
@@ -867,6 +869,10 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
867869
*/
868870
override fun play(fadeIn: Boolean) {
869871
Timber.v("play: $fadeIn")
872+
if (!prepared) {
873+
ToastUtil.show(this, getString(R.string.buffering_wait))
874+
return
875+
}
870876
audioFocus = AudioManagerCompat.requestAudioFocus(
871877
audioManager,
872878
focusRequest
@@ -1459,7 +1465,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
14591465
private fun prepare(song: Song, requestFocus: Boolean = true) {
14601466
tryLaunch(
14611467
block = {
1462-
Timber.v("准备播放: %s", song)
1468+
Timber.v("prepare start: %s", song)
14631469
if (TextUtils.isEmpty(song.data)) {
14641470
ToastUtil.show(service, getString(R.string.path_empty))
14651471
return@tryLaunch
@@ -1486,8 +1492,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
14861492
mediaPlayer.playbackParams = PlaybackParams().setSpeed(speed)
14871493
}
14881494
mediaPlayer.prepareAsync()
1489-
prepared = true
1490-
isLove = withContext(Dispatchers.IO) {
1495+
isLove = song.isLocal() && withContext(Dispatchers.IO) {
14911496
repository.isMyLove(playQueue.song.id)
14921497
.onErrorReturn {
14931498
false
@@ -1503,11 +1508,18 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
15031508
}
15041509

15051510
private suspend fun setDataSource(song: Song) = withContext(Dispatchers.IO) {
1511+
Timber.v("setDataSource: ${song.data}")
15061512
if (song.isLocal()) {
15071513
mediaPlayer.setDataSource(this@MusicService, song.contentUri)
15081514
} else if (song is Song.Remote) {
1515+
val start = System.currentTimeMillis()
15091516
mediaPlayer.setDataSource(this@MusicService, song.contentUri, song.headers)
1510-
retrieveRemoteSong(song, playQueue.song as Song.Remote)
1517+
Timber.v("setDataSource spend: ${System.currentTimeMillis() - start}")
1518+
1519+
// 可能会特别耗时
1520+
launch(Dispatchers.IO) {
1521+
retrieveRemoteSong(song, playQueue.song as Song.Remote)
1522+
}
15111523
}
15121524
}
15131525

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

@@ -2057,10 +2073,7 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
20572073
if (bitmap == null || bitmap.isRecycled) {
20582074
return null
20592075
}
2060-
var config: Bitmap.Config? = bitmap.config
2061-
if (config == null) {
2062-
config = Bitmap.Config.RGB_565
2063-
}
2076+
val config: Bitmap.Config = bitmap.config
20642077
return try {
20652078
bitmap.copy(config, false)
20662079
} catch (e: OutOfMemoryError) {
@@ -2071,6 +2084,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
20712084
}
20722085

20732086
fun retrieveRemoteSong(song: Song.Remote, targetSong: Song.Remote) {
2087+
Timber.v("retrieveRemoteSong: ${song.data}")
2088+
val start = System.currentTimeMillis()
20742089
val metadataRetriever = MediaMetadataRetriever()
20752090
try {
20762091
metadataRetriever.setDataSource(song.data, song.headers)
@@ -2115,8 +2130,9 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
21152130
dateModified
21162131
)
21172132
} catch (e: Exception) {
2118-
Timber.v("fail to retrieve: $e")
2133+
Timber.v("fail to retrieveRemoteSong data: ${song.data} detail: $e")
21192134
} finally {
2135+
Timber.v("retrieveRemoteSong spend:${System.currentTimeMillis() - start} ${song.data}")
21202136
metadataRetriever.release()
21212137
}
21222138
}

app/src/main/res/values-ja-rJP/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,5 @@
472472
<string name="can_t_be_empty">%s空にすることはできません</string>
473473
<string name="connect">せつぞく</string>
474474
<string name="edit">編集</string>
475+
<string name="buffering_wait">バッファリングです,ちょっと待ってください</string>
475476
</resources>

app/src/main/res/values-zh-rCN/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,5 @@
474474
<string name="edit">编辑</string>
475475
<string name="speed_at_2x">2x倍速播放中</string>
476476
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌词文件请确保放在公共目录中(如Downloads、Music等目录)</string>
477+
<string name="buffering_wait">缓冲中,请稍后</string>
477478
</resources>

app/src/main/res/values-zh-rHK/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,5 @@
473473
<string name="edit">編輯</string>
474474
<string name="speed_at_2x">2x倍速播放中</string>
475475
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌詞檔案請確保放在公共目錄中(如Downloads、Music等目錄)</string>
476+
<string name="buffering_wait">緩衝中,請稍後</string>
476477
</resources>

app/src/main/res/values-zh-rTW/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,5 @@
474474
<string name="edit">編輯</string>
475475
<string name="speed_at_2x">2x倍速播放中</string>
476476
<string name="local_lyric_tip">如果你的Android版本在11以及以上,本地歌詞檔案請確保放在公共目錄中(如Downloads、Music等目錄)</string>
477+
<string name="buffering_wait">緩衝中,請稍後</string>
477478
</resources>

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,5 @@
474474
<string name="edit">Edit</string>
475475
<string name="speed_at_2x">Playing at 2x speed</string>
476476
<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>
477+
<string name="buffering_wait">Buffering, please wait</string>
477478
</resources>

0 commit comments

Comments
 (0)