Skip to content

Commit

Permalink
fix: Made media player service work in APIs lower than 26
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <gabilessto@gmail.com>
  • Loading branch information
BobbyESP committed Apr 14, 2024
1 parent 658093b commit 148b28e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.bobbyesp.mediaplayer.service

import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaSession
Expand All @@ -20,7 +18,6 @@ class MediaService : MediaSessionService() {
@Inject
lateinit var notificationManager: MediaNotificationManager

@RequiresApi(Build.VERSION_CODES.O)
@UnstableApi
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
notificationManager.startNotificationService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
Expand All @@ -16,7 +15,6 @@ import com.bobbyesp.mediaplayer.R
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

@RequiresApi(Build.VERSION_CODES.O)
class MediaNotificationManager @Inject constructor(
@ApplicationContext private val context: Context,
private val player: ExoPlayer
Expand Down Expand Up @@ -59,23 +57,33 @@ class MediaNotificationManager @Inject constructor(
}

private fun startForegroundNotification(mediaSessionService: MediaSessionService) {
val notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
.setCategory(Notification.CATEGORY_SERVICE)
.build()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
.setCategory(Notification.CATEGORY_SERVICE)
.build()

mediaSessionService.startForeground(NOTIFICATION_ID, notification)
} else {
val notification = NotificationCompat.Builder(context)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()

mediaSessionService.startForeground(NOTIFICATION_ID, notification)
mediaSessionService.startForeground(NOTIFICATION_ID, notification)
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
)
notificationManager.createNotificationChannel(channel)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
)
notificationManager.createNotificationChannel(channel)
}
}


companion object {
private const val NOTIFICATION_ID = 200
private const val NOTIFICATION_CHANNEL_NAME = "notification_channel_1"
Expand Down

0 comments on commit 148b28e

Please sign in to comment.