Recording User Voice Message in ASP.NET MVC

Recording user input in a VoiceXML application using ASP.NET MVC is very easy if you use VoiceModel.  VoiceModel is an open source project that abstracts the VoiceXML to an easy to use object hierarchy that is meant to work with ASP.NET MVC 3 and Razor.  In this post I will walk you through a simple demonstration on using VoiceModel to record user input.  The source code for this demo is available on CodePlex.

 In this demonstration the application will prompt the user to make a recording after a beep and to press the pound (#) key when they are done. When the user is finished recording the recorded message will be played back to them and they will be asked if they want to save it. If they respond yes, by either saying "yes" or pressing the 1 key on the phone, the audio recording will be saved in a directory on the application server.  If they respond no, by saying "no" or pressing the 2 key on the phone, they will be prompted to re-record the message.  Messages are saved in an audio wav file format the could be played at another time by an IVR application.  An example might be a voice mail application where the user leaves a message that the owner of the voice mail box could listen to at a later time.

To initialize the objects we need for this application is very straight forward and is shown in the code snippet below.  We use the constructor for the Record object that accepts the id of the view and the prompt we want to present the user to make the recording.  The prompt will use Text-To-Speech (TTS) to voice the prompt.  The Record object also lets you pass in a pre-recorded audio file if you do not want to use TTS.  Notice that we are also setting the confirm property to true.  This tells the Record object to provide the confirmation step to ask the caller if they want to save the message or not.  There is a default confirmation prompt that can be overridden.

public static VoiceModels Recreate()
{
    VoiceModels views = new VoiceModels();
    Record getRecording = new Record("getRecording", "Please record your information after the beep.  Press the pound key when you are finished.")
    {
        confirm = true
    };
    views.Add(getRecording);
    views.Add(new Exit("goodbye", "Your message has been saved. Goodbye."));

    return views;

}


After the recording is complete we will tell the user that their message has been saved and hangup by using the Exit object.

Next we need to setup our state machine for this application, which is shown in the code snippet below.  We just need to setup two states with one transition.  The start state is where we get the recording in the Record object and the last state is the Exit where we say "goodbye" to the caller and hangup.  The transition is between the "getRecording" and "goodbye".


public static CallFlow Recreate()
{
    CallFlow flow = new CallFlow();
    flow.AddStartState(new State("getRecording", "goodbye"));
    flow.AddState(new State("goodbye"));
    return flow;

}

Finally we need to initialize the Controller that will be used in this MVC application.  This is shown in the code snippet below.  To setup the Controller we inherit from VoiceController and override the Initialize method.  The Initialize method creates an instance of our Views for the IVR and the state machine used by the Controller.  There is also a new property of the VoiceController called recordingPath that lets you set where the saved messages will go.


public class RecordController : VoiceController
{
    protected override void Initialize(RequestContext rc)
    {
        base.Initialize(rc);
        voiceModels = RecordVoiceModelBuilder.Build();
        callFlow = RecordCallFlowBuilder.Build();
        recordingPath = "~/App_Data/recordings";
    }
 
}


That is all there is to create a VoiceXML application to record user input.  Download the Visual Studio 2010 solution from CodePlex and give it a try.  You can test this application on a freely available IVR from Voxeo.  Just follow the instructions in this Blog for either a hosted IVR in the Cloud or an IVR running on your development environment.

Comments

Popular posts from this blog

Using Claims in ASP.NET Identity

Seeding & Customizing ASP.NET MVC SimpleMembership

Customizing Claims for Authorization in ASP.NET Core 2.0