android startForeground playerNotificationManagerBuilder.setNotificationListener的错误通知

8dtrkrch  于 2023-09-28  发布在  Android
关注(0)|答案(2)|浏览(111)

你好,我正在构建一个流音乐应用程序,当我试图设置setNotificationListener时,我收到此错误,应用程序崩溃
为了记录在案,我已经可以显示通知,但重新安装应用程序后,我收到这个错误
这是我的代码

public void startToPlay(Context context){
        // Global settings.

        playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(context,
                PLAYBACK_NOTIFICATION_ID,
                PLAYBACK_CHANNEL_ID);

        playerNotificationManagerBuilder.setSmallIconResourceId(R.drawable.ic_image_ip);

        playerNotificationManagerBuilder.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
            @Override
            public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
                PlayerNotificationManager.NotificationListener.super.onNotificationCancelled(notificationId, dismissedByUser);
                stopSelf();
            }
            @Override
            public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
                PlayerNotificationManager.NotificationListener.super.onNotificationPosted(notificationId, notification, ongoing);
                if (ongoing) {
                    // Here Audio is playing, so we need to make sure the service will not get destroyed by calling startForeground.
                    startForeground(notificationId, notification);
                } else {
                    //Here audio has stopped playing, so we can make notification dismissible on swipe.
                    stopForeground(false);
                }
            }
        });
uurv41yg

uurv41yg1#

在调用之前,只需创建通知通道
startForeground(notificationId, notification)

override fun onNotificationPosted(
        notificationId: Int,
        notification: Notification,
        ongoing: Boolean
    ) {
        // create channel for the audio player notificaiton
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_LOW
            val channel = NotificationChannel(
                "audio_player",
                "channel_name",
                importance
            )
            channel.setSound(null, null)
            notificationManager.createNotificationChannel(channel)
        }

        startForeground(notificationId, notification)
    }
xzv2uavs

xzv2uavs2#

我是这样做的
在Manifest.xml中

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" android:maxSdkVersion="34" />

<service android:name=".ForegroundService"
  android:foregroundServiceType="mediaPlayback"
  android:exported="false">
  <intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
  </intent-filter>
  <property android:name="android.app.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
    android:value="test"/>
 </service>

<receiver android:name="androidx.media3.session.MediaButtonReceiver"
  android:exported="true">
  <intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
  </intent-filter>
</receiver>

在你的服务

package com.example

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import androidx.media3.ui.PlayerNotificationManager

class ForegroundService: MediaSessionService() {

private var playerNotificationManager: PlayerNotificationManager? = null

private var notificationManager: NotificationManager? = null

private var descriptionAdapter = DescriptionAdapter()

private var notificationListener = notificationListener()

    override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
        return null
    }

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  super.onStartCommand(intent, flags, startId)
    addNotificationToPlayer()
  return START_NOT_STICKY
}

    override fun onCreate() {
        super.onCreate()
         createNotificationChannel()            
    }

    override fun onDestroy() {
      super.onDestroy()      
        playerNotificationManager?.setPlayer(null)
    }

    @UnstableApi
    private fun addNotificationToPlayer() {
     if(playerNotificationManager == null){
         playerNotificationManager = PlayerNotificationManager.Builder(
           this, 1, "NOTIFICATION_CHANNEL_NAME",)
           .setMediaDescriptionAdapter(descriptionAdapter)
           .setNotificationListener(notificationListener)
        .build()
        
        playerNotificationManager?.setPlayer(mediaSession?.player)
     }
       
    }
    
    private inner class DescriptionAdapter : MediaDescriptionAdapter {
        override fun getCurrentContentTitle(player: Player): CharSequence {
            TODO("Not yet implemented")
        }

        override fun createCurrentContentIntent(player: Player): PendingIntent? {
            TODO("Not yet implemented")
        }

        override fun getCurrentContentText(player: Player): CharSequence? {
            TODO("Not yet implemented")
        }

        override fun getCurrentLargeIcon(
            player: Player,
            callback: PlayerNotificationManager.BitmapCallback
        ): Bitmap? {
            TODO("Not yet implemented")
        }
    }

    @UnstableApi
    private fun notificationListener() = object : PlayerNotificationManager.NotificationListener {
        override fun onNotificationPosted(
            notificationId: Int,
            notification: Notification,
            ongoing: Boolean
        ) {
            super.onNotificationPosted(notificationId, notification, ongoing)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                startForeground(
                    notificationId,
                    notification,
                    ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
                )
            } else {
                startForeground(
                    notificationId,
                    notification
                )
            }
        }

        override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
            super.onNotificationCancelled(notificationId, dismissedByUser)
            stopSelf()
        }
    }

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

    private fun getManager(context: Context): NotificationManager {
        if (notificationManager == null) {
            notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
        return notificationManager as NotificationManager
    }
    
}

相关问题