High performance VoIP SDK for .Net developers

VoIP SIP SDK

Recording / playing back media in a softphone

Explanation

Prerequisities

Download: SoftphoneExample.zip

This article is a brief introduction about recording media streams in a softphone application in relation with Ozeki VoIP SIP SDK. After reading through this page you will be fully familiar with all the essential terms concerning media stream recording in softphones and what you will need for creating your own solution using Ozeki VoIP SIP SDK.

Introduction

One of the most essential VoIP communication applications is the softphone that is the software model of a physical telephone. The role is the same and it also functions in a similar way. The GUI of the softphone is also resembles a telephone device.


Figure 1 - VoIP call recording

As a software solution the softphone is in some aspects an extension of the traditional telephone headset, for example it is capable for recording and playing back media data.

The VoIP communication software solutions operate with digitized media data, audio and video, and they can easily record it during the communication process. The record itself can be done by mixing the incoming and outgoing data, but in special cases you can record only one of the participants' talks.

Ozeki VoIP SDK provides a great set of classes and methods that support softphone development and also recording and playing back media data. You can use these in your application for gaining a fully featured softphone the easiest way you can.

The following program code uses the background support of Ozeki VoIP SIP SDK, therefore you will need to download and install the SDK on your computer before starting to use the program code. You will also need to have Visual Studio 2010 or compatible IDE and .NET Framework installed on your system, as the program code below is written in C# language.

Replaying Audio Files with Ozeki VoIP SIP SDK

The sample program for this article contains the basic support for audio recording and playing. This is a simple example, the possibilities of audio file handling are more versatile, but the basics are also coded in this sample program.

Playing an uncompressed .wav audio file, you need to be able to find and open one. Ozeki VoIP SIP SDK provides you a useful tool WaveStreamPlayback that is for audio playing in case of .wav files (Code 1).

private WaveStreamPlayback WaveStreamPlayback;

Code 1 - The definition of a WaveStreamPlayback audio handler

The WaveStreamPlayback is a MediaHandler that can be initialized by setting the audio file to be played. You can do this by adding the filename (if it is in the same directory as the program) or the file path. You can also specify the repetition, the packetization time and if it is necessary you can set that a cache stream needs to be used.

In the sample program the audio file to play is specified by a textbox element. In this textbox you can give the file path or filename to be played. You can also use the Open button to pop up a browsing window where you can browse for the proper audio file (Code 2).

The Open dialog window that pops up is a standard Windows tool that can be used in any .NET application.

buttonStartPlayback.Text = "Start";
textBoxRecordingFile.Text = "";
using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, Filter = "Wave Files (*.wav)|*.wav" })
{
      if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      textBoxPlaybackFile.Text = ofd.FileName;
}

buttonStopPlayback.PerformClick();

Code 2 - The code that runs when you press the Open button

When the audio file is specified, you can start playing with pressing the Start button on the GUI. If every parameter is set properly the playback will start instantly. Code 3 shows the code snippet that runs in that case.

try
    {
        buttonStopPlayback.PerformClick();
        WaveStreamPlayback = new WaveStreamPlayback(textBoxPlaybackFile.Text);
        WaveStreamPlayback.Stopped += (WaveStreamPlayback_Stopped);
        prevaudiofile = textBoxPlaybackFile.Text;
        speaker.Start();
        connector.Connect(WaveStreamPlayback, speaker);
    }
    catch (Exception ex)
    {
        if (WaveStreamPlayback == null)
        {
            MessageBox.Show(String.Format("The file location of your .wav file is incorrect.\n {0}", ex.Message), string.Empty, MessageBoxButtons.OK,
     MessageBoxIcon.Error);
            return;
        }else
            MessageBox.Show(String.Format(ex.Message), string.Empty, MessageBoxButtons.OK,
     MessageBoxIcon.Error);
    }
}
if (!WaveStreamPlayback.IsStreaming)
{
    buttonStartPlayback.Text = "Pause";
    WaveStreamPlayback.StartStreaming();
    WaveStreamPlayback.IsStreaming = true;
}

Code 3 - Starting the .wav audio playing

The Start button is also used for pausing a started stream. In this case the instruction in Code 4 will run. The difference between pausing and stopping a stream is that in the case of pausing, you can restart the streaming from the exact point it has been paused. In case of stopping the restart will be started from the beginning of the file.

buttonStartPlayback.Text = "Start";
WaveStreamPlayback.PauseStreaming();
WaveStreamPlayback.IsStreaming = false;

Code 4 - Pausing the audio streaming

If you want to stop the audio streaming, you need to press the stop button and the following code will run (Code 5)

textBoxRecordingFile.Text = string.Empty;
if (WaveStreamPlayback != null)
{
        WaveStreamPlayback.StopStreaming();
        buttonStartPlayback.Text = "Start";
        speaker.Stop();
        connector.Disconnect(WaveStreamPlayback, speaker);
        WaveStreamPlayback.Dispose();
        WaveStreamPlayback = null;
}

Code 5 - Stopping the audio streaming

The audio stream playing can also be used for playing an audio file into a call. In this case you need to connect the WaveStreamPlayback object to the PhoneCallAudioSender object with the connector. In that case the StartStreaming() method will play the audio file directly into the established call.

Recording Audio Data into Files

Ozeki VoIP SIP SDK provides developer tools for recording audio files too. This can be used for recording messages, IVR tree information or for recording a complete phone call.

The tool for recording a .wav audio file is a MediaHandler called WaveStreamRecorder. Code 6 shows the declaration of this handler. It can be initialized by setting the filename (in local directory) or the file path of the file to be recorded into. You can also set the wave format of the file.

WaveStreamRecorder WaveStreamRecorder;

Code 6 - The declaration of the recorder object

You can specify the audio file by opening a SaveFileDialog window that is a standard window used by any .NET application. In this window you can set the file name and the path (by browsing). Code 7 shows the code for opening this dialog and for the specification. It is the event handler method for the Save button on the GUI.

private void buttonSave_Click(object sender, EventArgs e)
{
    textBoxPlaybackFile.Text = string.Empty;
    using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Wave Files (*.wav)|*.wav", FileName = textBoxRecordingFile.Text })
    {
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            textBoxRecordingFile.Text = sfd.FileName;
    }
}

Code 7 - specifying the audio file to record into

The audio stream can be recorded by pressing the Start button. You can pause and restart the recording with the same button. Pausing an audio recording means that the recording stops and when you start it again the audio data will be appended to the already saved audio (Code 8).

textBoxPlaybackFile.Text = string.Empty;
    if (WaveStreamRecorder == null)
    {
        if (string.IsNullOrEmpty(textBoxRecordingFile.Text))
            return; // Select a file first

        WaveStreamRecorder = new WaveStreamRecorder(textBoxRecordingFile.Text);
        WaveStreamRecorder.Stopped += (WaveStreamRecorder_Stopped);
        microphone.Start();
        connector.Connect(microphone, WaveStreamRecorder);
        WaveStreamRecorder.IsStreaming = false;
    }
    if (!WaveStreamRecorder.IsStreaming)
    {
        buttonStartRecording.Text = "Pause";
        WaveStreamRecorder.IsStreaming = true;
        WaveStreamRecorder.StartStreaming();
    }
    else
    {
        buttonStartRecording.Text = "Start";
        WaveStreamRecorder.PauseStreaming();
        WaveStreamRecorder.IsStreaming = false;
    }

Code 8 - Starting and pausing audio recording

You can stop the audio recording by pressing the Stop button (Code 9). In this case the audio file will be finalized and disposed and all the used devices and tools will be stopped, closed and set to the initial value.

textBoxPlaybackFile.Text = string.Empty;
WaveStreamRecorder.StopStreaming();
connector.Disconnect(microphone, WaveStreamRecorder);
microphone.Stop();
WaveStreamRecorder.Dispose();
WaveStreamRecorder = null;
buttonStartRecording.Text = "Start";

Code 9 - Stopping the audio recording

With the above mentioned methods you can record an uncompressed .wav audio file using Ozeki VoIP SIP SDK. If you want to record a phone call, you will need to connect your recorder object to the PhoneCallAudioReceiver object. In this case you can record the incoming audio data. If you want to record both parties' voice, you will need an AudioMixerMediaHandler object.

Ozeki VoIP SIP softphone sample Visual Studio project can be obtained by
opening the download page:
Download Ozeki VoIP SIP softphone sample project now

Summary

This article introduced you the basic knowledge about recording media streams in softphones and showed how Ozeki VoIP SIP SDK can help you to fulfill your wishes about this topic. If you have read through this page carefully, you already have all the knowledge you need to start on your own solution.

As you are now familiar with all the terms concerning this topic, now it is time to take a step further and explore what other extraordinary solution Ozeki VoIP SIP SDK can provide to you.

If you have any questions or need assistance, please contact us at info@voip-sip-sdk.com

You can select a suitable Ozeki VoIP SIP SDK license for your project on licensing page

Related Pages

Operating system: Windows 8, Windows 7, Vista, 200x, XP
System memory: 512 MB+
Free disk space: 100 MB+
Development environment: Visual Studio 2010 (Recommended), Visual Studio 2008, Visual Studio 2005
Programming language: C#.NET
Supported .NET framework: .NET Framework 4.5, .NET Framework 4.0, .NET Framework 3.5 SP1
Software development kit: OZEKI VoIP SIP SDK(Download)
VoIP connection: 1 SIP account