E-Kuerschner / useAudioPlayer

React hooks for controlling audio on the web
MIT License
332 stars 37 forks source link

Playback events are not triggered by media session API #37

Open humbkr opened 4 years ago

humbkr commented 4 years ago

Describe the bug Chrome and other browsers now ship with a feature to control media directly from the browser using the media session API: https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API https://techcrunch.com/2020/01/16/chrome-gets-global-media-controls

Problem: when using the Chrome media controls, the playback events like play, pause, ... are not triggered, ie isPlaying is not updated, resulting in an out of sync UI.

To Reproduce Steps to reproduce the behavior:

  1. Using Chrome, start playing an audio file via an html button
  2. Use the browser media controls to pause the playback
  3. The audio is paused but the isPlaying variable is not updated

Expected behavior The useAudioPlayer() state reflects the state of the audio playback.

Environment (please complete the following information):

Related issue: https://github.com/goldfire/howler.js/issues/1262

joeyiny commented 1 year ago

+1, lmk if I can help with this @E-Kuerschner

E-Kuerschner commented 1 year ago

@joeyiny I have something working locally! Hoping to get a release out this week cc @humbkr

E-Kuerschner commented 1 year ago

@joeyiny sorry for the delay! I still have a branch open for this but i ran into some difficulties with the global version of the hook. Will keep posting updates here

joeyiny commented 1 year ago

@E-Kuerschner got it, let me know if i can help

E-Kuerschner commented 1 year ago

hey @joeyiny, @humbkr are you able to use the media session if you create the sound with html5 set to true?

const { load } = useAudioPlayer()

load({
  src: '/myFile.mp3',
  html5: true
})

After doing some research it appears that the media notification will only be set if an audio or video element is present on the page.

see: https://stackoverflow.com/questions/52226454/media-notifications-using-the-media-session-web-api-doesnt-work-with-web-audio

E-Kuerschner commented 1 year ago

pausing work on this for the time being since the Media Session Notification feature will not work with the Web Audio Api which is the default mode of operation for Howler. In order to integrate with the MediaSession the Howl needs to be created with the html5 option set to true.