Service does not receive multimedia button click events

﹥>﹥吖頭↗ 提交于 2021-01-01 07:29:41

问题


I am making an application that should do some things when I press the multimedia buttons. I know that I have to create a service and catch all the events there. I did everything the documentation recommended, but I still do not receive click events in the application.

I followed the following guidelines for writing a similar application: https://developer.android.com/guide/topics/media-apps/mediabuttons https://developer.android.com/reference/androidx/media/session/MediaButtonReceiver.html https://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778?_ga=2.241321730.1029380229.1566994251-780734199.1565702660 https://habr.com/ru/post/339416/ But I still don't get the event. I put all my code in the repository: https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main

and ... Here are some recommendations that I have already followed: in AndroidManifest ->

<receiver android:name="androidx.media.session.MediaButtonReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>
...
        <service android:name="ru.exsoft.mediaretranslator.RetranslatorService">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </service>

in the Service class:

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        MediaButtonReceiver.handleIntent(mediaSession, intent)
        return START_STICKY
    }

 override fun onCreate() {
        val stateBuilder: Builder = Builder().setActions(ACTION_PLAY or ACTION_STOP or ACTION_PAUSE or ACTION_PLAY_PAUSE or ACTION_SKIP_TO_NEXT or ACTION_SKIP_TO_PREVIOUS)
        ...
        mediaSession!!.setCallback(mediaSessionCallback)
        mediaSession!!.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
        val state =  PlaybackStateCompat.Builder().setActions(
            ACTION_PLAY or ACTION_PLAY_PAUSE or
                    ACTION_PLAY_FROM_MEDIA_ID or ACTION_PAUSE or
                    ACTION_SKIP_TO_NEXT or ACTION_SKIP_TO_PREVIOUS
        ).setState(STATE_PLAYING, 5, 1.0f, SystemClock.elapsedRealtime())
            .build()
        mediaSession?.setPlaybackState(state!!)
        mediaSession?.isActive = true
        ...
        val notificationBuilder = NotificationCompat.Builder(this, channelId )
        val notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(PRIORITY_MAX)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build()
        startForeground(1, notification)
    }

And other in GitLab https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main

... And in logcat, I see the following lines that confirm that the system simply does not perceive my class as one that can handle this event. :(

2019-08-29 14:08:44.122 23691-23691/? V/Avrcp_ext: recordKeyDispatched: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_MEDIA_PAUSE, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=508419172, downTime=508419172, deviceId=-1, source=0x0 } dispatched to com.vanced.android.youtube
2019-08-29 14:08:44.122 1675-2510/? D/MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PAUSE, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=508419172, downTime=508419172, deviceId=-1, source=0x0 } to the last known PendingIntent PendingIntent{e2b4330: PendingIntentRecord{7ce4b98 com.vanced.android.youtube broadcastIntent}}

回答1:


I ran into a similar issue, the root cause was that my media session wasn't playing audio. Try adding the following after the line where you call mediaSession?.isActive = true

        val dummyAudioTrack = AudioTrack(
             AudioManager.STREAM_MUSIC,
             48000,
             AudioFormat.CHANNEL_OUT_STEREO,
             AudioFormat.ENCODING_PCM_16BIT,
             AudioTrack.getMinBufferSize(
                  48000,
                  AudioFormat.CHANNEL_OUT_STEREO,
                  AudioFormat.ENCODING_PCM_16BIT
             ),
             AudioTrack.MODE_STREAM
         )

         dummyAudioTrack.play()

(Note, AudioTrack is deprecated, I just haven't gotten around to looking for the preferred method)



来源:https://stackoverflow.com/questions/57709401/service-does-not-receive-multimedia-button-click-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!