I’m having an issue with my mediafiles in my Shiny app: I can’t get them to stop playing when the user navigates to a different page.
Background/dependencies:
I’m using shiny.router for basic routing between the app’s pages. I’m using shinyjs and jquery here and there to allow for some dynamic div appending and accordions.
Problem:
I’ve noticed that for some reason my </audio/> and </video/> files keep playing even when the user changes the page they’re on. This is strange as the audio element isn’t ‘visible’ to the user, and the page (and associated url) changes, yet the audio or video files from different pages continue playing.
The only way to ensure there is no overlap in the mediafiles playing is to make sure to ‘pause’ before exiting the page.
I would like to make this behavior automatic, and thought it was for default HTML5 audio controls. I suspect it may have to do with how shiny.router creates different ‘pages’, however, I’m not advanced enough in my JS and web dev knowledge to understand how these ‘pages’ are navigated between internally. It seems, though, as if they are all overlapping in some way and the ‘routing’ offered by the shiny.router package isn’t necessarily routing so much as it is hiding/revealing the different elements or ‘pages’.
Attempted solutions: My first thought was to use JQuery or Javascript to check, each time an audioplayer’s play/pause button is clicked, if any other audio elements are playing and pause them if they don’t match the ID of the clicked audioplayer’s play/pause button like this:
$(currentAudioPlayButtonID).clicked( // when audio button clicked
$("audio").each( // look at all audio elements
if $(this).attr("id") != currentAudioPlayButtonID
$(this).pause() //
)
)
// OR invert the logic:
$(currentAudioPlayButtonID).clicked( // when audio button clicked
$("audio").each( // look at all audio elements
$(this).pause() // pause all audio elements
)
currentAudioPlayButtonID.play() // play the relevant audio
)
However, I’ve found that the play/pause toggler button that comes with the HTML5 audio element doesn’t seem to have a selector. I could create a new separate play/pause button with JQuery, however, I’d like to stick to using the simple element and its controls.
I found that there is the ‘.paused’ class that can be used to check if an HTML5 audio player is playing, however, I’m not sure how to use this in a function as I still ultimately would have no button to ‘trigger’ this ‘are other audios playing’ check. Would there potentially be a solution using $(document).load, where I would insert the ‘check’ code there?
If anyone has had a similar issue with shiny/shiny.router or may have any advice for how to ensure all other audio/video elements on the website are paused when a new one is clicked, I would deeply appreciate it! I think the answer may be simple but I am not very experienced in web dev and Shiny. Thank you for your help! I appreciate it.