Uploaded audio files does not play in r shiny

♀尐吖头ヾ 提交于 2021-02-10 05:41:25

问题


I have developed an app which takes an .wav file as input and plays it. However it seems to be not working. On the other hand, if the audio file is placed in /www folder and path name is given, it is playing fine.

What am I doing wrong ?

app.R

library( shinydashboard )

ui = source( file.path( "ui", "ui.R" ), local = T )$value   #..... ui for ocr

server = function( input, output, session ){

  #..... Include server logic for each tab .....

  source( file.path( "server", "server.R" ), local = T )$value    #..... server logic for ocrs

}

shinyApp( ui = ui, server = server )

ui.R

header = dashboardHeader( title = 'Speech 2 Text' )

sidebar =  dashboardSidebar( collapsed = F,

  sidebarMenu(

  menuItem( strong( '  Speech to Text' ), tabName = 'tab1' )

  )

)

body =  dashboardBody(

  tabItems(

    #####.... tab1 ....####

    tabItem( tabName = 'tab1',

             fluidRow(

               box( title = 'Actions', status = 'success', collapsible = T, width = 12, solidHeader = T,

                    column( 6, fileInput( 'uploaded_audio', 'Choose WAV File', multiple = FALSE, accept = '.wav' ) ),

                    br(),

                    column( 3, align = 'left',

                            actionButton( 'play_audio', 'Play Audio' ),

                            tags$style( "#play_audio { vertical-align: middle; height: 30px; width: 70%; font-size: 15px;color: white;background-color:#1B618D;border-color: #374645 }" )

                    )

                  )

             )

    )

  )

)

dashboardPage( header, sidebar, body, skin = 'yellow' )

server.R

observeEvent( input$play_audio, {

  req( input$uploaded_audio )

  insertUI( selector = "#play_audio", where = "afterEnd",

           ui = tags$audio( src = input$uploaded_audio$datapath, type = "audio/wav", autoplay = NA, controls = NA )  
  )

})

Also I have printed str( input$uploaded_audio ) to see the path where the file gets stored.

It shows output something like this :

data.frame':    1 obs. of  4 variables:
 $ name    : chr "newOSR_us_000_0034_8k.wav"
 $ size    : int 568810
 $ type    : chr "audio/wav"
 $ datapath: chr "C:\\Users\\MACHIN~1\\AppData\\Local\\Temp\\Rtmp2zx4tW/c3d8af2a9ed3e4b8cd415aea/0.wav"

回答1:


You can base64-encode the file:

library(base64enc)

server <- function(input, output){
  observeEvent( input$play_audio, {

    req( input$uploaded_audio )

    base64 <- dataURI(file = input$uploaded_audio$datapath, mime = "audio/wav")

    insertUI( selector = "#play_audio", where = "afterEnd",

              ui = tags$audio( src = base64, type = "audio/wav", autoplay = NA, controls = NA )  
    )

  })
}


来源:https://stackoverflow.com/questions/56926161/uploaded-audio-files-does-not-play-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!