Skip to content

Bluetooth headset handle a command custom way #249

@baruckis

Description

@baruckis

Hi, I would like to handle Bluetooth headset commands COMMAND_SEEK_TO_PREVIOUS and COMMAND_SEEK_TO_NEXT differently. Instead of jumping back to the previous item or next item from the playlist, I want to only seek back and seek forward in the current item by a specific number of seconds (common behaviour in podcasts apps). I have followed your documentation note on how to do that https://developer.android.com/guide/topics/media/media3/getting-started/playing-in-background?authuser=3#handling-ui

The problem is if I have only one item in my playlist or my item is last in the playlist. Then COMMAND_SEEK_TO_NEXT is not available because there is no next item. So I can not catch this command and re-map action. I could set player.repeatMode = Player.REPEAT_MODE_ALL to have button next enabled but that is also not acceptable to repeat content always. Any ideas what else I can try to do? Or is this missing funcionallity?

 override fun onPlayerCommandRequest(session: MediaSession, controller: ControllerInfo, playerCommand: Int): Int {
      Log.i("TESTING", "onPlayerCommandRequest $playerCommand")

      return if (controller.packageName.contains("com.android.bluetooth")) {
        when (playerCommand) {
          Player.COMMAND_SEEK_TO_PREVIOUS -> {
            session.player.seekBack()
            SessionResult.RESULT_INFO_SKIPPED
          }
          Player.COMMAND_SEEK_TO_NEXT -> {
            session.player.seekForward()
            SessionResult.RESULT_INFO_SKIPPED
          }
          else -> super.onPlayerCommandRequest(session, controller, playerCommand)
        }
      } else {
        super.onPlayerCommandRequest(session, controller, playerCommand)
      }
    }

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions