Skip to content

Commit

Permalink
fix: try fix keep playing when screen locked
Browse files Browse the repository at this point in the history
  • Loading branch information
hpp2334 committed Dec 2, 2024
1 parent 6e10b41 commit f7d142f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<!-- Required to maintain app compatibility. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.kutedev.easemusicplayer

import android.Manifest.permission.FOREGROUND_SERVICE
import android.Manifest.permission.POST_NOTIFICATIONS
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -34,6 +38,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.compose.NavHost
Expand Down Expand Up @@ -104,6 +109,7 @@ class MainActivity : ComponentActivity() {
override fun onStart() {
super.onStart()
uiBridge!!.onActivityStart()
ensurePostNotificationsPermission()
}

override fun onStop() {
Expand Down Expand Up @@ -232,5 +238,19 @@ class MainActivity : ComponentActivity() {
private fun registerViewModels() {
registerViewModel<EaseViewModel>(uiBridge!!)
}

private fun ensurePostNotificationsPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(
POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(POST_NOTIFICATIONS),
101
)
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ private class EaseMusicPlayerDelegate : IPlayerDelegateForeign {
_context = context

val player = ExoPlayer.Builder(context)
.setAudioAttributes(AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).build(), true)
.setAudioAttributes(AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build(),
true
)
.setHandleAudioBecomingNoisy(true)
.setWakeMode(WAKE_MODE_NETWORK)
.build()
Expand Down

0 comments on commit f7d142f

Please sign in to comment.