Skip to content

Playing an audio file

Jean-Philippe Gariépy edited this page Apr 25, 2015 · 2 revisions

To play a prerecorded audio file, an AudioFile object is needed. This object can be created by a factory method. Here, since we want to play an audio file from a location (URI or path), we will use the fromLocation static method. This AudioFile must then be used in a Message output turn:

Dialogue.java:

//Play an audio file
String recordingLocation = context.getContextPath() + "/audio/test.wav";
Message message = new Message("recording-message", AudioFile.fromLocation(recordingLocation));
DialogueUtils.doTurn(message, context);

Note that here, the path of the audio file will be based on the context path, i.e. the path under which the application is deployed on the web application server. The context path is exposed as a property of the VoiceXmlDialogueContext.


Running this example

You can download or browse the complete code for this example at GitHub.This is a complete working application that you can build and run for yourself.

You can also clone the Rivr Cookbook repository and checkout this example:

git clone -b message-audio-file git@github.com:nuecho/rivr-cookbook.git

Then, to build and run it:

cd rivr-cookbook

./gradlew jettyRun

The VoiceXML dialogue should be available at http://localhost:8080/rivr-cookbook/dialogue

To stop the application, press Control-C in the console.