Skip to content

Commit

Permalink
修复崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
rRemix committed Jul 24, 2024
1 parent a5d71bc commit fa1e5c3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
15 changes: 10 additions & 5 deletions app/src/main/java/remix/myplayer/helper/MusicServiceRemote.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.*
import android.media.MediaPlayer
import android.os.IBinder
import androidx.core.content.ContextCompat
import remix.myplayer.bean.mp3.Song
import remix.myplayer.service.Command
import remix.myplayer.service.MusicService
Expand All @@ -23,12 +24,16 @@ object MusicServiceRemote {
// if (!Util.isAppOnForeground()) {
// return null
// }
var realActivity: Activity? = (context as Activity).parent
if (realActivity == null)
realActivity = context
val realActivity = (context as Activity).parent ?: context
val contextWrapper = ContextWrapper(realActivity)
contextWrapper.startService(Intent(contextWrapper, MusicService::class.java))

val intent = Intent(contextWrapper, MusicService::class.java)

try {
context.startService(intent)
} catch (e: Exception) {
ContextCompat.startForegroundService(context, intent)
}

val binder = ServiceBinder(callback)

if (contextWrapper.bindService(Intent().setClass(contextWrapper, MusicService::class.java), binder, Context.BIND_AUTO_CREATE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class SongPopupListener(activity: BaseActivity,
.putExtra(EXTRA_SONG, song))
}
R.id.menu_add_to_playlist -> {
AddtoPlayListDialog.newInstance(listOf(song.id))
.show(activity.supportFragmentManager, AddtoPlayListDialog::class.java.simpleName)
val fm = activity.supportFragmentManager
if (!fm.isDestroyed && !fm.isStateSaved) {
AddtoPlayListDialog.newInstance(listOf(song.id))
.show(fm, AddtoPlayListDialog::class.java.simpleName)
}
}
R.id.menu_add_to_play_queue -> {
DatabaseRepository.getInstance()
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/remix/myplayer/service/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.WorkerThread
import androidx.core.app.ServiceCompat
import androidx.media.AudioAttributesCompat
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat
Expand Down Expand Up @@ -613,6 +614,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
}

override fun onStop() {
pause(false)
notify.cancelPlayingNotify()
stopSelf()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ abstract class Notify internal constructor(internal var service: MusicService) {
}
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
service.startForeground(PLAYING_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
} else{
} else {
service.startForeground(PLAYING_NOTIFICATION_ID, notification)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ class BottomActionBarFragment : BaseMusicFragment<BottomActionbarBinding>() {
return
}
val intent = Intent(requireContext(), PlayerActivity::class.java)
val bundle = Bundle()
intent.putExtras(bundle)
val activity: Activity? = activity
if (activity != null && !activity.isDestroyed) {
activity.startActivity(intent)
}
}

lateinit var gestureDetector: GestureDetector
private lateinit var gestureDetector: GestureDetector

internal class GestureListener(fragment: BottomActionBarFragment) : SimpleOnGestureListener() {
private val reference: WeakReference<BottomActionBarFragment> = WeakReference(fragment)
Expand Down
39 changes: 25 additions & 14 deletions app/src/main/java/remix/myplayer/util/MediaStoreUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.provider.MediaStore.Audio.AudioColumns
import android.provider.MediaStore.Audio.Genres
import android.provider.Settings
import androidx.annotation.WorkerThread
import com.tencent.bugly.crashreport.CrashReport
import remix.myplayer.App
import remix.myplayer.R
import remix.myplayer.bean.mp3.Album
Expand Down Expand Up @@ -249,20 +250,24 @@ object MediaStoreUtil {
}

val genres: MutableList<Genre> = ArrayList()
context.contentResolver.query(
Genres.EXTERNAL_CONTENT_URI,
arrayOf(Genres._ID, Genres.NAME),
null,
null,
SPUtil.getValue(context, SETTING_KEY.NAME, SETTING_KEY.GENRE_SORT_ORDER, SortOrder.GENRE_A_Z)
)?.use { cursor ->
while (cursor.moveToNext()) {
val genreId = cursor.getLong(0)
if (genreId > 0) {
val songs = getSongsByGenreId(genreId)
genres.add(Genre(genreId, cursor.getString(1) ?: "", songs.size))
try {
context.contentResolver.query(
Genres.EXTERNAL_CONTENT_URI,
arrayOf(Genres._ID, Genres.NAME),
null,
null,
SPUtil.getValue(context, SETTING_KEY.NAME, SETTING_KEY.GENRE_SORT_ORDER, SortOrder.GENRE_A_Z)
)?.use { cursor ->
while (cursor.moveToNext()) {
val genreId = cursor.getLong(0)
if (genreId > 0) {
val songs = getSongsByGenreId(genreId)
genres.add(Genre(genreId, cursor.getString(1) ?: "", songs.size))
}
}
}
} catch (e: Exception) {
CrashReport.postCatchedException(e)
}
return genres
}
Expand Down Expand Up @@ -651,9 +656,15 @@ object MediaStoreUtil {
newSelectionValues, selectionValues.size,
newSelectionValues.size - selectionValues.size)
}


val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} else {
Audio.Media.EXTERNAL_CONTENT_URI
}

return try {
context.contentResolver.query(Audio.Media.EXTERNAL_CONTENT_URI,
context.contentResolver.query(uri,
BASE_PROJECTION, selection, newSelectionValues, sortOrder)
} catch (e: SecurityException) {
null
Expand Down

0 comments on commit fa1e5c3

Please sign in to comment.