Skip to content

Commit fa1e5c3

Browse files
committed
修复崩溃
1 parent a5d71bc commit fa1e5c3

File tree

6 files changed

+46
-26
lines changed

6 files changed

+46
-26
lines changed

app/src/main/java/remix/myplayer/helper/MusicServiceRemote.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Activity
44
import android.content.*
55
import android.media.MediaPlayer
66
import android.os.IBinder
7+
import androidx.core.content.ContextCompat
78
import remix.myplayer.bean.mp3.Song
89
import remix.myplayer.service.Command
910
import remix.myplayer.service.MusicService
@@ -23,12 +24,16 @@ object MusicServiceRemote {
2324
// if (!Util.isAppOnForeground()) {
2425
// return null
2526
// }
26-
var realActivity: Activity? = (context as Activity).parent
27-
if (realActivity == null)
28-
realActivity = context
27+
val realActivity = (context as Activity).parent ?: context
2928
val contextWrapper = ContextWrapper(realActivity)
30-
contextWrapper.startService(Intent(contextWrapper, MusicService::class.java))
31-
29+
val intent = Intent(contextWrapper, MusicService::class.java)
30+
31+
try {
32+
context.startService(intent)
33+
} catch (e: Exception) {
34+
ContextCompat.startForegroundService(context, intent)
35+
}
36+
3237
val binder = ServiceBinder(callback)
3338

3439
if (contextWrapper.bindService(Intent().setClass(contextWrapper, MusicService::class.java), binder, Context.BIND_AUTO_CREATE)) {

app/src/main/java/remix/myplayer/misc/menu/SongPopupListener.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ class SongPopupListener(activity: BaseActivity,
4343
.putExtra(EXTRA_SONG, song))
4444
}
4545
R.id.menu_add_to_playlist -> {
46-
AddtoPlayListDialog.newInstance(listOf(song.id))
47-
.show(activity.supportFragmentManager, AddtoPlayListDialog::class.java.simpleName)
46+
val fm = activity.supportFragmentManager
47+
if (!fm.isDestroyed && !fm.isStateSaved) {
48+
AddtoPlayListDialog.newInstance(listOf(song.id))
49+
.show(fm, AddtoPlayListDialog::class.java.simpleName)
50+
}
4851
}
4952
R.id.menu_add_to_play_queue -> {
5053
DatabaseRepository.getInstance()

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

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.view.Gravity
2424
import android.view.ViewGroup
2525
import android.view.WindowManager
2626
import androidx.annotation.WorkerThread
27+
import androidx.core.app.ServiceCompat
2728
import androidx.media.AudioAttributesCompat
2829
import androidx.media.AudioFocusRequestCompat
2930
import androidx.media.AudioManagerCompat
@@ -613,6 +614,8 @@ class MusicService : BaseService(), Playback, MusicEventCallback,
613614
}
614615

615616
override fun onStop() {
617+
pause(false)
618+
notify.cancelPlayingNotify()
616619
stopSelf()
617620
}
618621

app/src/main/java/remix/myplayer/service/notification/Notify.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ abstract class Notify internal constructor(internal var service: MusicService) {
109109
}
110110
}
111111
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
112-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
112+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
113113
service.startForeground(PLAYING_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
114-
} else{
114+
} else {
115115
service.startForeground(PLAYING_NOTIFICATION_ID, notification)
116116
}
117117
} else {

app/src/main/java/remix/myplayer/ui/fragment/BottomActionBarFragment.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ class BottomActionBarFragment : BaseMusicFragment<BottomActionbarBinding>() {
109109
return
110110
}
111111
val intent = Intent(requireContext(), PlayerActivity::class.java)
112-
val bundle = Bundle()
113-
intent.putExtras(bundle)
114112
val activity: Activity? = activity
115113
if (activity != null && !activity.isDestroyed) {
116114
activity.startActivity(intent)
117115
}
118116
}
119117

120-
lateinit var gestureDetector: GestureDetector
118+
private lateinit var gestureDetector: GestureDetector
121119

122120
internal class GestureListener(fragment: BottomActionBarFragment) : SimpleOnGestureListener() {
123121
private val reference: WeakReference<BottomActionBarFragment> = WeakReference(fragment)

app/src/main/java/remix/myplayer/util/MediaStoreUtil.kt

+25-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.provider.MediaStore.Audio.AudioColumns
1515
import android.provider.MediaStore.Audio.Genres
1616
import android.provider.Settings
1717
import androidx.annotation.WorkerThread
18+
import com.tencent.bugly.crashreport.CrashReport
1819
import remix.myplayer.App
1920
import remix.myplayer.R
2021
import remix.myplayer.bean.mp3.Album
@@ -249,20 +250,24 @@ object MediaStoreUtil {
249250
}
250251

251252
val genres: MutableList<Genre> = ArrayList()
252-
context.contentResolver.query(
253-
Genres.EXTERNAL_CONTENT_URI,
254-
arrayOf(Genres._ID, Genres.NAME),
255-
null,
256-
null,
257-
SPUtil.getValue(context, SETTING_KEY.NAME, SETTING_KEY.GENRE_SORT_ORDER, SortOrder.GENRE_A_Z)
258-
)?.use { cursor ->
259-
while (cursor.moveToNext()) {
260-
val genreId = cursor.getLong(0)
261-
if (genreId > 0) {
262-
val songs = getSongsByGenreId(genreId)
263-
genres.add(Genre(genreId, cursor.getString(1) ?: "", songs.size))
253+
try {
254+
context.contentResolver.query(
255+
Genres.EXTERNAL_CONTENT_URI,
256+
arrayOf(Genres._ID, Genres.NAME),
257+
null,
258+
null,
259+
SPUtil.getValue(context, SETTING_KEY.NAME, SETTING_KEY.GENRE_SORT_ORDER, SortOrder.GENRE_A_Z)
260+
)?.use { cursor ->
261+
while (cursor.moveToNext()) {
262+
val genreId = cursor.getLong(0)
263+
if (genreId > 0) {
264+
val songs = getSongsByGenreId(genreId)
265+
genres.add(Genre(genreId, cursor.getString(1) ?: "", songs.size))
266+
}
264267
}
265268
}
269+
} catch (e: Exception) {
270+
CrashReport.postCatchedException(e)
266271
}
267272
return genres
268273
}
@@ -651,9 +656,15 @@ object MediaStoreUtil {
651656
newSelectionValues, selectionValues.size,
652657
newSelectionValues.size - selectionValues.size)
653658
}
654-
659+
660+
val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
661+
Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
662+
} else {
663+
Audio.Media.EXTERNAL_CONTENT_URI
664+
}
665+
655666
return try {
656-
context.contentResolver.query(Audio.Media.EXTERNAL_CONTENT_URI,
667+
context.contentResolver.query(uri,
657668
BASE_PROJECTION, selection, newSelectionValues, sortOrder)
658669
} catch (e: SecurityException) {
659670
null

0 commit comments

Comments
 (0)