0

I am using a Videoview to play an animation video in the loop. I noticed that whenever the video is playing if any other music app is playing audio and I click on headset buttons(pause/play), the media button events are not dispatched to the music playing app. Instead, it is dispatched to my ExampleService which extends from MediaBrowserServiceCompat. Videoview is stealing those media button events. I am using even AudioManager.AUDIO_FOCUS_NONE.

I noticed when my app opens and video plays, I can see a log in the stack trace that

D/MediaSessionService: Media button session is changed to com.example.mediasessiontest/ExampleService (userId=0)

Is there a way to play video without requesting for Mediabutton event?

  1. If I don't play video in my app, headset events are correctly routed to the app which was playing music
  2. This is happening only on Android 8 and 9. Works fine on Android 10.
  3. Tried even Surfaceview with Mediaplayer to play video. But observed the same issue.

    private val VIDEO_BASE_PATH = "android.resource://"
    videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE)
    videoView.setMediaController(null)
    videoView.setVideoPath(VIDEO_BASE_PATH + packageName + "/" + R.raw.test_video)        
    videoView.setOnPreparedListener { player ->
       player.isLooping = true
       player.start() //This causes the media button sesssion to be changed
    }
    

    Tried using Surfaceview with Mediaplayer as well. But same result. Looks like a bug in OS 8 and 9 ?

Siju
  • 2,585
  • 4
  • 29
  • 53
  • I wonder if your problem is more tied to `MediaBrowserServiceCompat`. You might consider creating a scrap project, putting a `VideoView` or `SurfaceView`/`MediaPlayer` in there, and see if it gets the same behavior. If it does not, then you would need to see if there is a way to control this from the standpoint of `MediaBrowserServiceCompat`. – CommonsWare Feb 19 '20 at 15:11
  • @CommonsWare Without the `MediaBrowserServiceCompat`, there is no change in `Mediasession` event and works as expected. The problem is when both are used together, `Videoview` and `MediaBrowserService`. – Siju Feb 19 '20 at 15:29
  • I have never used `MediaBrowserServiceCompat` and so I do not know what you can do with it to tell it to stop directing media button events to you. Depending on the nature of the animation, you might consider trying to avoid treating it as media and doing something else (e.g., convert it to an animated GIF and use a library to render that GIF). – CommonsWare Feb 19 '20 at 17:05
  • Created an issue `https://issuetracker.google.com/issues/149815041` but Google devs closed it as "Intended Behavior" instead of acknowleding that its a bug which they don't want to fix. – Siju Apr 01 '20 at 12:46
  • 1
    Used a GIF file instead instead of .mp4 for the animation. – Siju Apr 01 '20 at 12:47

0 Answers0