google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.74k stars 6.03k forks source link

Can you can again provide the code to setup notification because functions used in this code is deprecated and i am not finding any suitable example for seeting it #10129

Open HarshSinghgithub opened 2 years ago

HarshSinghgithub commented 2 years ago

Unfortunately we can't answer all questions. Unclear questions or questions with insufficient information may not get attention.

Before filing a question:

When filing a question:

Describe your question in detail.

In case your question refers to a problem you are seeing in your app:

In case your question is related to a piece of media:

Don't forget to check supported formats and devices (https://exoplayer.dev/supported-formats.html).

If there's something you don't want to post publicly, please submit the issue, then email the link/bug report to dev.exoplayer@gmail.com using a subject in the format "Issue #1234", where #1234 is your issue number (we don't reply to emails).

marcbaechinger commented 2 years ago

Please provide complete information as requested in the issue template. The issue template can be found here.

HarshSinghgithub commented 2 years ago
private fun setNotification() {
        playerNotificationManager =
            PlayerNotificationManager.Builder(
                applicationContext,
                NOTIFICATION_ID,
                NOTIFICATION_CHANNEL_ID
            )
                .setChannelImportance(NotificationManager.IMPORTANCE_HIGH)
                .setMediaDescriptionAdapter(object :
                    PlayerNotificationManager.MediaDescriptionAdapter {
                    override fun getCurrentContentTitle(player: Player): CharSequence {
                        //var window = exoPlayer?.currentMediaItemIndex
                        return "Music"
                    }

                    override fun createCurrentContentIntent(player: Player): PendingIntent? {
                        return null
                    }

                    override fun getCurrentContentText(player: Player): CharSequence? {
                        return "Notification"
                    }

                    override fun getCurrentLargeIcon(
                        player: Player,
                        callback: PlayerNotificationManager.BitmapCallback
                    ): Bitmap? {
                        return null
                    }
                }).setNotificationListener(object : PlayerNotificationManager.NotificationListener {
                    override fun onNotificationCancelled(
                        notificationId: Int,
                        dismissedByUser: Boolean
                    ) {
                        super.onNotificationCancelled(notificationId, dismissedByUser)
                        stopSelf()
                    }

                    override fun onNotificationPosted(
                        notificationId: Int,
                        notification: Notification,
                        ongoing: Boolean
                    ) {
                        super.onNotificationPosted(notificationId, notification, ongoing)
                        if (!ongoing)
                            stopForeground(true)
                        else
                            startForeground(notificationId, notification)
                    }
                })
                .build()
        playerNotificationManager.setPlayer(exoPlayer)

    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createChannel() =
        NotificationChannel("2", "Music", NotificationManager.IMPORTANCE_HIGH).apply {
            description = "HEMLO"
            setSound(null, null)
        }

Why this code is not working

marcbaechinger commented 2 years ago

We are not able to do general code review of app code I'm afraid.

From looking at the code I would think it could be an issue with the notification channel that is not existing and hence the notification is not shown. If this is the case that would be logged to the logcat I think. So looking into the logs may give you some indications about the channel not being created correctly. The createChannel method from above can be improved to actually create a notification channel by following the documentation about how to create a notification channel.

HarshSinghgithub commented 2 years ago

how can i resolve these logcat errors

2022-04-02 09:53:01.391 27541-27541/? E/libc: Access denied finding property "persist.vendor.sys.activitylog" 2022-04-02 09:53:03.703 27541-27573/com.harsh.musicplayer E/libc: Access denied finding property "persist.vendor.log.tel_dbg" 2022-04-02 09:53:03.879 27541-27568/com.harsh.musicplayer E/GED: Failed to get GED Log Buf, err(0) 2022-04-02 09:53:03.942 27541-27568/com.harsh.musicplayer E/ion: ioctl c0044901 failed with code -1: Invalid argument 2022-04-02 09:53:03.943 27541-27568/com.harsh.musicplayer E/GED: Failed to get GED Log Buf, err(0) 2022-04-02 09:53:04.418 27541-27568/com.harsh.musicplayer E/GED: Failed to get GED Log Buf, err(0) 2022-04-02 09:53:11.347 27541-27571/com.harsh.musicplayer E/AudioTrack: start(): 0x7a4374d000, mState = 1

HarshSinghgithub commented 2 years ago

this is my full service class if anything is not coreect please let me know

class PlayerService : Service() {

var exoPlayer: ExoPlayer? = null
private lateinit var playerNotificationManager: PlayerNotificationManager
private val NOTIFICATION_CHANNEL_ID = "com.harsh.musicPlayer.services.notification"
private val NOTIFICATION_ID = 2

override fun onBind(p0: Intent?): IBinder {
    return MusicBinder()
}

override fun onCreate() {
    super.onCreate()
    exoPlayer = ExoPlayer.Builder(this).build()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    val url = intent?.extras?.get("URL")
    val mediaUri = Uri.parse(url as String?)
    val mediaItem = MediaItem.fromUri(mediaUri)
    exoPlayer?.prepare()
    exoPlayer?.setMediaItem(mediaItem)
    exoPlayer?.prepare()
    Thread{ Runnable {
        createChannel()
    }}.start()

    return START_STICKY
}

inner class MusicBinder : Binder() {
    fun getExoPLayer() = exoPlayer
}

private fun setNotification() {
    playerNotificationManager =
        PlayerNotificationManager.Builder(
            applicationContext,
            NOTIFICATION_ID,
            NOTIFICATION_CHANNEL_ID
        )
            .setChannelImportance(NotificationManager.IMPORTANCE_HIGH)
            .setSmallIconResourceId(R.drawable.img)
            .setMediaDescriptionAdapter(object :
                PlayerNotificationManager.MediaDescriptionAdapter {
                override fun getCurrentContentTitle(player: Player): CharSequence {
                    return "Music"
                }

                override fun createCurrentContentIntent(player: Player): PendingIntent? {
                    val newIntent = Intent(this@PlayerService,MainActivity::class.java)
                    val pendingIntent = PendingIntent.getActivity(this@PlayerService,0,newIntent,0)
                    return null
                }

                override fun getCurrentContentText(player: Player): CharSequence {
                    return "Notification"
                }

                override fun getCurrentLargeIcon(
                    player: Player,
                    callback: PlayerNotificationManager.BitmapCallback
                ): Bitmap? {
                    return  BitmapFactory.decodeResource(resources, R.drawable.img)
                }
            }).setNotificationListener(object : PlayerNotificationManager.NotificationListener {
                override fun onNotificationCancelled(
                    notificationId: Int,
                    dismissedByUser: Boolean
                ) {
                    super.onNotificationCancelled(notificationId, dismissedByUser)
                    stopSelf()
                }

                override fun onNotificationPosted(
                    notificationId: Int,
                    notification: Notification,
                    ongoing: Boolean
                ) {
                    super.onNotificationPosted(notificationId, notification, ongoing)
                    if (!ongoing)
                        stopForeground(false)
                    else {
                        startForeground(2, notification)
                        Toast.makeText(applicationContext, "Channel", Toast.LENGTH_LONG).show()
                    }
                }
            })
            .build()
    if(exoPlayer != null)
    playerNotificationManager.setPlayer(exoPlayer)
}

private fun createChannel() {
     val channel = NotificationChannel("2", "Playback Channel", NotificationManager.IMPORTANCE_NONE).apply {
        description = "HELLO"
        setSound(null, null)
    }
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(channel)
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC)
    setNotification()
}

}