E / MediaPlayerNative: ошибка (100, 1) при обратном вызове mediasession

В своем приложении для Android я создаю службу, которая использует MediaPlayer для воспроизведения удаленного потока.

Каждый раз при воспроизведении потока приложение показывает уведомление. В этом уведомлении отображается простая кнопка воспроизведения / паузы и другая информация для запуска и остановки потока.

Сервис создает уведомление следующим образом:

private fun buildNotification() {
        val notification = NotificationCompat.Builder(this, this.notificationChannelId)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.ic_pnr_round)
                .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                .setContentText(this.currentArtist)
                .setContentTitle(this.currentTitle)
                .setSubText(this.defaultAlbum)
                .setStyle(MediaStyle()
                 .setMediaSession(this.mediaSession.value.sessionToken)
                )
                .setContentIntent(PendingIntent.getActivity(applicationContext, 0, Intent(applicationContext, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))


        if (this.mediaSession.value.controller.playbackState.state == PlaybackStateCompat.STATE_PLAYING) {
            notification.addAction(NotificationCompat.Action(
                    R.drawable.ic_pause, getString(R.string.notification_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_STOP))
            )
        } else {
            notification.addAction(NotificationCompat.Action(
                    R.drawable.ic_play, getString(R.string.notification_play),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY))
            )
        }


        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val nc = NotificationChannel(this.notificationChannelId, this.notificationChannel, NotificationManager.IMPORTANCE_LOW)
            nc.enableLights(false)
            nc.enableVibration(false)
            notificationManager.createNotificationChannel(nc)
        }

        notificationManager.notify(0, notification.build())
    }

Чтобы поймать действия уведомлений, я использую обратный вызов MediaSession

private val callbacks = object : MediaSessionCompat.Callback() {
    override fun onPlay() {
        play()
    }

    override fun onPause() {
        pause()
    }

    override fun onStop() {
        stop()
    }
}

Сервис и получатель зарегистрированы в AndroidManifest.xml

<service
    android:name = ".services.MediaPlayerService"
    android:exported = "false">
    <intent-filter>
        <action android:name = "android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</service>
<receiver android:name = "android.support.v4.media.session.MediaButtonReceiver">
    <intent-filter>
        <action android:name = "android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

Проблема в том, что каждый раз, когда я нажимаю кнопку в уведомлении, приложение останавливается, показывая

"APP isn't responding"

Журнал показывает эту ошибку:

E/MediaPlayerNative: error (100, 1)
                     error (100, 2)
E/MediaPlayer: Error (100,1)
E/MediaPlayer: Error (100,2)
E/MediaPlayerNative: error (1, -38)
E/MediaPlayerNative: error (1, -32)
E/MediaPlayerNative: error (1, -38)
E/MediaPlayer: Error (1,-38)
E/MediaPlayer: Error (1,-32)
E/MediaPlayer: Error (1,-38)

Использование этой службы в другой части приложения работает должным образом.

Я подозреваю что MediaButtonReceiver.buildMediaButtonPendingIntent вызывает проблемы.

Проблемы, похоже, ограничиваются Android 8 и 8.1, другая протестированная мной версия (5, 6, 7.1, 9), похоже, свободна от этого странного поведения.

Заранее спасибо за помощь!

0
0
898
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Хорошо, мне удалось разгадать эту загадку

Изменение

notificationManager.notify(0, notification.build())

с участием

startForeground(0, notification.build())

решил проблему.

Я жду, когда команда ASOP в Issuetracker разъяснит, почему это происходит только на Oreo.

Другие вопросы по теме