Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny - Audio Playback

For this question, I'm using

library("shiny")
library("tuneR")
library("markdown")

though I'm sure only shiny is relevant.

Per the Shiny tag glossary, I should be able to use

tags$audio(src = "wavs/tempwav.wav", type = "audio/wav", autoplay = NA, controls = NA)

which generates the html

<audio src="wavs/tempwav.wav" type="audio/wav" autoplay controls></audio> 

to play a sound in a Shiny server.

I can't get this to work with any kind of mp3, wav, or other file. Edge gives a, "this type of audio file is not supported" error, while the RStudio webpage and Chrome just show a blank playback control. I want it to work with *.wav files generated on the fly by the Shiny program. The wav files are generated correctly and play correctly in every music player I tried (eg, VLC, WMP, MMgold, WinAmp, etc).

Using the code in an HTML file, eg

<HTML>
<audio src="wavs/tempwav.wav" type="audio/wav" autoplay controls></audio>
</HTML>

works just fine in any browser. I think the issue may have to do with how R Shiny deals with directories. Suggestions?

like image 582
Zediiiii Avatar asked Mar 24 '16 16:03

Zediiiii


People also ask

Do companies use R Shiny?

Fortune 500 companies trust Appsilon to build custom Shiny apps, scale PoCs, improve Shiny app performance, and enhance dashboard UI. We're also a pioneer in Shiny open source. Our R packages are actively used by global organizations such as Merck, Johnson & Johnson, WHO, Ubisoft, Bank of America, and Renault.

Is R Shiny different from R?

Using R, you create a user interface and server, and Shiny compiles your code into the HTML, CSS and JavaScript needed to display your application on the web.

Is R Shiny difficult?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.


1 Answers

Solution here. Technically this was written in the Shiny tag glossary - though it was easy to miss. The www sub-directory must be in the present working directory for this to operate correctly.

To be really clear, if your pwd is "c:\dir", then a directory "c:\dir\www" must contain the audio files to be played, and the tag would be written as shown in the glossary:

tags$audio(src = "sound.mp3", type = "audio/mp3", autoplay = NA, controls = NA)

it works for wav files too

tags$audio(src = "sound.wav", type = "audio/wav", autoplay = NA, controls = NA)
like image 141
Zediiiii Avatar answered Oct 01 '22 01:10

Zediiiii