2017-03-16 66 views
2
library(shiny) 
library(cognizer) 


#' the required api keys 
username_TTS <-"" 
password_TTS <- "" 
TEXT_TO_SPEECH_USERNAME_PASSWORD = paste(username_TTS,":",password_TTS,sep="") 

ui <- fluidPage(

    # Application title 
    titlePanel("Just some text to speech example"), 

    fluidRow(textInput("caption","Enter the caption"),actionButton("gobutton","submit")), 
    fluidRow(verbatimTextOutput("answer"), width = 4), 

    # plays from www/ 
    fluidRow(tags$audio(src = "1.wav", type = "audio/wav", controls = NA), helpText("Key in any sentences, wait and press F5. Then you can play your audio")) 


) 



server <- function(input, output) { 

textfunction <- eventReactive(input$gobutton, { 
    thetext <- input$caption 
    text_audio(thetext, TEXT_TO_SPEECH_USERNAME_PASSWORD, directory = 'www', accept = "audio/wav") 
    "Done!" 

     }) 


output$answer <- renderText({textfunction()}) 


} 

# Run the application 
shinyApp(ui = ui, server = server) 

我創建了一個應用程序,使用Watson將文本轉換爲語音。用戶輸入文本句子。點擊提交,watson會將其轉換爲音頻文件。但是,播放的音頻文件始終是以前的文本,除非您通過F5刷新應用程序。我怎樣才能讓音頻播放器採取最新的音頻文件?Shiny Watson文本到語音實時

例如:文本1:你好 提交和播放的是「你好」 文本2:再見 提交玩,仍然是「你好」 直到我F5,然後當我點擊播放,它的「再見」

回答

0

該問題與Shiny的音頻文件緩存有關。一種解決方法,如描述here,可以幫助你:

重命名的wav文件中詢問後,並將其傳遞到在UI使用renderUI()在服務器端和uiOutput()反應UI組件。

請參考下面的工作示例:

library(shiny) 
library(cognizer) 

# the required api keys 
username_TTS <-"" 
password_TTS <- "" 
TEXT_TO_SPEECH_USERNAME_PASSWORD = paste(username_TTS,":",password_TTS,sep="") 

ui <- fluidPage(

    # Application title 
    titlePanel("Just some text to speech example"), 

    fluidRow(textInput("caption","Enter the caption"),actionButton("gobutton","submit")), 
    fluidRow(verbatimTextOutput("answer"), width = 4), 

    # plays from www/ 
    fluidRow(
    uiOutput("play") 
) 
) 

server <- function(input, output) { 
    observeEvent(input$gobutton, { 
    unlink("www/*.wav") 
    thetext <- input$caption 
    text_audio(thetext, TEXT_TO_SPEECH_USERNAME_PASSWORD, directory = 'www', accept = "audio/wav") 
    file.rename("www/1.wav", paste0("www/number",input$gobutton,".wav")) 
    "Done!" 
    output$play <- renderUI(
     tags$audio(src = paste0("temp.wav"), type = "audio/wav", controls = NA) 
    ) 
    output$play <- renderUI(
     tags$audio(src = paste0("number", input$gobutton,".wav"), type = "audio/wav", controls = NA) 
    ) 
    output$answer <- renderText(paste("Done!", input$gobutton)) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

嗨羅伯特,我正在閱讀的文章。這是同樣的問題。但是,我被困在重命名我的wav文件,因爲watson函數輸出一個wav文件爲1.wav – Germ

+0

嘗試了很多方法。我修改函數來做一個file.rename。在UI中呈現並輸出。所有的音頻文件都已創建但未播放。問題2:如何在播放後刪除音頻文件。每次提交文本時,音頻文件都會永久保存在目錄 – Germ

+0

中,我添加了一個工作示例。你可以使用'file.remove()'來刪除不需要的文件。我擔心,如果您有多個客戶端使用您的應用程序,這不是一個特別穩定的版本。 – Robert