闪亮的沃森文本实时演讲

时间:2017-03-16 15:08:08

标签: r shiny watson

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刷新应用程序。如何让音频播放器采用最新的音频文件?

示例:text1:hello 提交和播放将是&#34;你好&#34; text2:再见 提交和播放,仍然&#34;你好&#34; 直到我F5,然后当我点击播放时,它&#34; bye&#34;

1 个答案:

答案 0 :(得分:0)

问题与Shiny的音频文件缓存有关。正如here所述,解决方法可以为您提供帮助:

在查询后重命名wav文件,并使用服务器端的renderUI()和UI中的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)