Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions n_player/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">
<meta-data
android:name="com.enn3developer.monochrome_icon"
android:resource="@drawable/ic_launcher_monochrome" />
<activity
android:name=".MainActivity"
android:label="N Player"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -28,18 +33,6 @@
android:value="n_player"
/>
</activity>
<service
android:name=".DummyService"
android:parentActivityName=".MainActivity"
android:exported="true"
android:foregroundServiceType="mediaPlayback"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
<action android:name="androidx.media3.session.MediaLibraryService" />
<action android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
<receiver
android:name="androidx.media3.session.MediaButtonReceiver"
android:exported="true">
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MainActivity : NativeActivity() {
const val ASK_DIRECTORY = 0
const val ASK_FILE = 1
const val REQUEST_PERMISSION_CODE = 1
const val ACTIONS = PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_SKIP_TO_NEXT or PlaybackState.ACTION_SKIP_TO_PREVIOUS or PlaybackState.ACTION_SEEK_TO
}

@SuppressLint("RestrictedApi")
Expand Down Expand Up @@ -130,12 +131,13 @@ class MainActivity : NativeActivity() {
mediaSession = MediaSession(applicationContext, TAG)
val handler = Handler(Looper.getMainLooper())
handler.post {
mediaSession?.setCallback(MediaCallback(mediaSession!!, playback!!))
mediaSession?.setCallback(MediaCallback(mediaSession!!, this))
}
val bluetoothReceiver = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
applicationContext.registerReceiver(bluetoothBroadcastReceiver, bluetoothReceiver)
playback = PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_SKIP_TO_NEXT or PlaybackState.ACTION_SKIP_TO_PREVIOUS or PlaybackState.ACTION_SEEK_TO)
.setActions(ACTIONS)
.setActiveQueueItemId(ACTIONS)
val channel = NotificationChannel(
CHANNEL_ID,
NOTIFICATION_NAME_SERVICE,
Expand All @@ -148,8 +150,9 @@ class MainActivity : NativeActivity() {
mediaSession?.setPlaybackState(playback?.build())
NotificationManagerCompat.from(applicationContext).createNotificationChannel(channel)
notification = Notification.Builder(applicationContext, CHANNEL_ID).apply {
setSmallIcon(R.mipmap.ic_launcher_round)
setSmallIcon(R.drawable.ic_launcher_monochrome)
style = Notification.MediaStyle().setMediaSession(mediaSession?.sessionToken)
setOngoing(true)
}
}

Expand Down Expand Up @@ -186,9 +189,14 @@ class MainActivity : NativeActivity() {
coverPath: String,
songLength: Double
) {
val intent = Intent(applicationContext, DummyService::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
var intent = applicationContext.packageManager.getLaunchIntentForPackage(packageName)

if (intent == null) {
intent = Intent(applicationContext, MainActivity::class.java)
}

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

val pendingIntent =
PendingIntent.getActivity(
applicationContext, 0, intent,
Expand Down Expand Up @@ -242,7 +250,6 @@ class MainActivity : NativeActivity() {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(NOTIFICATION_ID)
stopService(Intent(this, DummyService::class.java))
super.onDestroy()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@ import android.media.session.MediaSession
import android.media.session.PlaybackState

// Called in mediaSession callback when we interact with notification
class MediaCallback(private val mediaSession: MediaSession,
private val playback: PlaybackState.Builder
class MediaCallback(
private val mediaSession: MediaSession,
private val activity: MainActivity
) : MediaSession.Callback() {

private external fun TogglePause()
private external fun PlayNext()
private external fun PlayPrevious()
private external fun Seek(seek: Double)

override fun onPause() {
TogglePause()
mediaSession.controller.playbackState?.position?.let {
playback.setState(
PlaybackState.STATE_PAUSED,
it, 1.0f
)
}
mediaSession.setPlaybackState(playback.build())
val position = mediaSession.controller.playbackState?.position ?: 0L

activity.playback?.setState(
PlaybackState.STATE_PAUSED,
position, 1.0f
)

mediaSession.setPlaybackState(activity.playback?.build())
super.onPause()
}

override fun onPlay() {
TogglePause()
mediaSession.controller.playbackState?.position?.let {
playback.setState(
PlaybackState.STATE_PLAYING,
it, 1.0f)
}
mediaSession.setPlaybackState(playback.build())
val position = mediaSession.controller.playbackState?.position ?: 0L

activity.playback?.setState(
PlaybackState.STATE_PLAYING,
position, 1.0f
)
mediaSession.setPlaybackState(activity.playback?.build())
super.onPlay()
}

Expand All @@ -42,15 +46,15 @@ class MediaCallback(private val mediaSession: MediaSession,

override fun onSkipToPrevious() {
PlayPrevious()
playback.setState(PlaybackState.STATE_PLAYING, 0L, 1.0f)
mediaSession.setPlaybackState(playback.build())
activity.playback?.setState(PlaybackState.STATE_PLAYING, 0L, 1.0f)
mediaSession.setPlaybackState(activity.playback?.build())
super.onSkipToPrevious()
}

override fun onSeekTo(pos: Long) {
Seek((pos / 1000).toDouble())
playback.setState(PlaybackState.STATE_PLAYING, pos, 1.0f)
mediaSession.setPlaybackState(playback.build())
activity.playback?.setState(PlaybackState.STATE_PLAYING, pos, 1.0f)
mediaSession.setPlaybackState(activity.playback?.build())
super.onSeekTo(pos)
}
}
Loading