RemoteControlReceiver.kt 1.03 KB
Newer Older
konghaorui committed
1
package com.ydl.media.audio.receiver
konghaorui committed
2 3 4 5 6 7

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.support.v4.media.session.MediaSessionCompat
import android.view.KeyEvent
konghaorui committed
8
import com.ydl.media.audio.AudioPlayer
konghaorui committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29


/**
 * Created by haorui on 2019-10-27 .
 * Des: 耳机线控,仅在5.0以下有效,5.0以上被[MediaSessionCompat]接管。
 */
class RemoteControlReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val event = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
        if (event == null || event.action != KeyEvent.ACTION_UP) {
            return
        }

        when (event.keyCode) {
            KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, KeyEvent.KEYCODE_HEADSETHOOK -> AudioPlayer.get().playPause()
            KeyEvent.KEYCODE_MEDIA_NEXT -> AudioPlayer.get().next()
            KeyEvent.KEYCODE_MEDIA_PREVIOUS -> AudioPlayer.get().prev()
        }
    }
}