Skip to content

Instantly share code, notes, and snippets.

@jlitven
Created November 6, 2016 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlitven/da5572fb09de9709f2626580eebc920a to your computer and use it in GitHub Desktop.
Save jlitven/da5572fb09de9709f2626580eebc920a to your computer and use it in GitHub Desktop.
# Javascript functions to set audio source, play and pause the audio
js_set_audio_src = "shinyjs.setAudioSrc = function(source){document.getElementById('audio_player').src=source};"
js_pause_audio = "shinyjs.pauseAudio = function(){document.getElementById('audio_player').pause()};"
js_play_audio = "shinyjs.playAudio = function(){document.getElementById('audio_player').play()};"
# Values stores the last point so we can turn off the audio
Values<-reactiveValues(old="Start")
# Preview the track when the user clicks on the graph point
# Pause the track if it's the same point
observe({
if(length(input$tooltip)==0){
return()
}
if(input$tooltip != ""){
full_name = unlist(strsplit(input$tooltip,split = "<br>"))[1]
if(isolate({input$tooltip == Values$old})){
js$playAudio()
}else{
preview_url = tracks[tracks$full_name==full_name, 'preview_url']
js$setAudioSrc(preview_url)
isolate({ Values$old<-input$tooltip })
}
}else{
js$pauseAudio()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment