服务不接收多媒体按钮点击事件
Service does not receive multimedia button click events
我正在制作一个应用程序,当我按下多媒体按钮时它应该做一些事情。我知道我必须创建一个服务并捕获那里的所有事件。我按照文档中的建议进行了所有操作,但我仍然没有在应用程序中收到点击事件。
我在编写类似应用程序时遵循了以下准则:
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/
但我仍然没有得到这个事件。
我将所有代码放在存储库中:
https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main
和...以下是我已经遵循的一些建议:
在 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>
在服务中 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)
}
以及 GitLab 中的其他 https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main
... 在 logcat 中,我看到以下几行,确认系统根本不认为我的 class 可以处理此事件。 :(
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}}
我 运行 遇到了类似的问题,根本原因是我的媒体会话没有播放音频。尝试在调用 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()
(注意,AudioTrack 已弃用,我只是还没找到首选方法)
我正在制作一个应用程序,当我按下多媒体按钮时它应该做一些事情。我知道我必须创建一个服务并捕获那里的所有事件。我按照文档中的建议进行了所有操作,但我仍然没有在应用程序中收到点击事件。
我在编写类似应用程序时遵循了以下准则: 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/ 但我仍然没有得到这个事件。 我将所有代码放在存储库中: https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main
和...以下是我已经遵循的一些建议: 在 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>
在服务中 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)
}
以及 GitLab 中的其他 https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main
... 在 logcat 中,我看到以下几行,确认系统根本不认为我的 class 可以处理此事件。 :(
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}}
我 运行 遇到了类似的问题,根本原因是我的媒体会话没有播放音频。尝试在调用 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()
(注意,AudioTrack 已弃用,我只是还没找到首选方法)