Skip to content

Commit

Permalink
fix: don't auto-skip next when offline
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang committed Aug 28, 2024
1 parent ef568af commit 83b74ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import com.zionhuang.music.utils.DiscordRPC
import com.zionhuang.music.utils.dataStore
import com.zionhuang.music.utils.enumPreference
import com.zionhuang.music.utils.get
import com.zionhuang.music.utils.isInternetAvailable
import com.zionhuang.music.utils.reportException
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -585,7 +586,10 @@ class MusicService : MediaLibraryService(),
}

override fun onPlayerError(error: PlaybackException) {
if (dataStore.get(AutoSkipNextOnErrorKey, false) && player.hasNextMediaItem()) {
if (dataStore.get(AutoSkipNextOnErrorKey, false) &&
isInternetAvailable(this) &&
player.hasNextMediaItem()
) {
player.seekToNext()
player.prepare()
player.playWhenReady = true
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/zionhuang/music/utils/NetworkUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.zionhuang.music.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import androidx.core.content.getSystemService

fun isInternetAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService<ConnectivityManager>() ?: return false
val activeNetwork = connectivityManager.activeNetwork ?: return false
val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false

return when {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
}

0 comments on commit 83b74ec

Please sign in to comment.