0

I am trying to autoplay a sound which would act as an app startup sound. Below is what I am using. Now when I add controls = TRUE, and click on play, the audio file plays fine, but when I set autoplay = TRUE, it doesn't play on its own. How can I fix this?

ui = navbarPage("Project", theme = shinytheme("sandstone"),
                tags$audio(src = "Melody.mp3",type = "audio/mp3", autoplay = TRUE),
                img(src="tenor (1).gif", align = "topleft",height='90px',width='90px'),
                tabPanel("Introduction")

UPDATE

I did a find a solution using HTML. How can this be applied to Shiny?

Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 2
    What browser are you using? Most don't allow auto play audio because so many sites abused the attribute. This like likely a browser issue and not specific to R Shiny at all. – MrFlick Apr 22 '21 at 04:31
  • @MrFlick I tried in R-Studio viewer window and Chrome. – Ed_Gravy Apr 22 '21 at 04:49
  • perhaps [this](https://stackoverflow.com/a/62369873/13513328) helps – Waldi Apr 22 '21 at 05:01
  • 2
    You could autoplay files such as `audio` and `video` up to the year 2018 I think, then the policy has changed and unless the user has some sort of interaction (such a click on a page), you will not be allowed to autoplay anything, As pointed out this isn't the `shiny` issue at all, its a standard – Pork Chop Apr 22 '21 at 10:12

2 Answers2

2

After you download the silence.mp3 file (and keep it in www folder) from the link you gave, you can do the following.

ui = navbarPage("Project", theme = shinytheme("sandstone"),
                tags$audio(src = "silence.mp3",type = "audio/mp3" ), 
                tags$audio(src = "Melody.mp3",type = "audio/mp3", autoplay = TRUE),
                img(src="mouse.png", align = "topleft",height='90px',width='90px'),
                tabPanel("Introduction") 
)

server = function(input, output, session) {}

shinyApp(ui, server)            
YBS
  • 19,324
  • 2
  • 9
  • 27
1

Like many have said, this is a browser issue, for as long as autoplay=TRUE on your tags$audio,the user will have to turn on the sound by clicking the permission lock by the URL, then edit the site settings. Set audio permission to allow

Jefferson
  • 11
  • 1