服务未收到多媒体按钮单击事件 [英] Service does not receive multimedia button click events

查看:89
本文介绍了服务未收到多媒体按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应在按下多媒体按钮时执行某些操作的应用程序.我知道我必须创建一个服务并捕获那里的所有事件.我已完成了文档建议的所有操作,但仍未在应用程序中收到点击事件.

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.

我遵循以下准则编写类似的应用程序: https://developer.android.com/guide/topics/media-apps/媒体按钮 https://developer.android.com/reference/androidx/media/session/MediaButtonReceiver.html https://habr.com/ru/post/339416/ 但是我仍然没有得到这个事件. 我将所有代码放在存储库中: https://gitlab.com/ICaxapI/mediaretranslator/tree/master/app/src/main

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

和...这是我已经遵循的一些建议: 在AndroidManifest中->

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>

在服务"类中:

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

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

...在logcat中,我看到以下几行内容确认系统完全不会将我的类视为可以处理此事件的类. :(

... 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}}

推荐答案

我遇到了类似的问题,根本原因是我的媒体会话没有播放音频.尝试在调用mediaSession?.isActive = true

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()

(请注意,AudioTrack已过时,我只是没有四处寻找首选方法)

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

这篇关于服务未收到多媒体按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆