服务未收到多媒体按钮点击事件

问题描述 投票:0回答:1

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

我遵循以下准则编写类似的应用程序:https://developer.android.com/guide/topics/media-apps/mediabuttonshttps://developer.android.com/reference/androidx/media/session/MediaButtonReceiver.htmlhttps://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778?_ga=2.241321730.1029380229.1566994251-780734199.1565702660https://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>

在服务类中:

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中,我看到以下几行确认系统完全不会将我的类视为可以处理此事件的类。 :(

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

我正在制作一个应在按下多媒体按钮时执行某些操作的应用程序。我知道我必须创建一个服务并捕获那里的所有事件。我做了所有文档...

java android kotlin service android-mediaplayer
1个回答
0
投票

我遇到了类似的问题,根本原因是我的媒体会话没有播放音频。尝试在调用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()
© www.soinside.com 2019 - 2024. All rights reserved.