High performance VoIP SDK for .Net developers

VoIP SIP SDK

How to play an mp3 file into a voice call

Explanation

Prerequisities

Download: Mp3PlayerSoftphone.zip

This article is a detailed guide about playing mp3 audio files into a SIP phone call in relation with Ozeki VoIP SIP SDK. After reading through this page you will be fully familiar with all the essential terms concerning playing mp3 into a SIP call and what you will need for creating your own solution using Ozeki VoIP SIP SDK.

Introduction

When a VoIP communication software is built there is a need for the ability of playing audio files into the call like sending audio messages or using audio data for navigating the caller in an interactive voice response (IVR) System. The .mp3 format is a collection of audio file definitions that is widely used among computer users, therefore it is essential to have support for using .mp3 files in a VoIP solution.


Figure 1 - Incoming SIP message

The following VoIP solution example uses the background support of Ozeki VoIP SIP SDK. Before you can use the knowledge written in the following section you will need to download and install Ozeki VoIP SIP SDK on your computer. You will also need to have Visual Studio 2010 and .NET Framework installed as the program below is written in C# language.

Playing audio file

The audio file playing is a basic support in Ozeki VoIP SIP SDK. You can choose between .wav or .mp3 files and you can play them directly on your speaker or play them into a SIP call. The example program presented in this article introduces all these variations.

The user interface of this softphone has two new elements, namely groupboxes that contain the support for the .wav and mp3 playing (Figure 1). The GUI also contains a groupbox for the PBX registration but that is not different from any simple softphone registration forms, therefore this article will not cover that topic.


Figure 2 - Graphical user interface for an audio playing softphone

The Ozeki VoIP SIP SDK provides different tools for playing .wav and .mp3 files. They are special MediaHandler object that can handle the audio streams. Code 1 shows the audio players that can be used for audio file playing.

//.wav player
WaveStreamPlayback WaveStreamPlayback;
//.mp3 player
MP3StreamPlayback Mp3StreamPlayback;

Code 1 - The audio player objects

The player objects need a file name or file path to open. On the GUI there is a textbox for each player. The buttons work similar in both groupboxes, the only difference is the player object they operate with.

The following code snippets will show the methods for the .mp3 player. The .wav player's methods work exactly the same way.

As it was mentioned before, you need a file name or path that specifies the .mp3 file to play. In this example, you can browse for the .mp3 file by pressing the Open button in the mp3 groupbox. Code 2 shows the event handler for that button.

The first lines of the code make some reinitialization steps. When you want to open a new audio file to play, both the .mp3 and the .wav file playing tools need to be set back to the starting states. This means that the actual audio playing, if there is one, should be stopped, the start buttons need to have the Text "Start" on them (it can be changed to "Pause" as you will see later) and the file name textboxes need to set empty.

The actual file opening is done by using a standard file open dialog window that is provided by the C#.Net. You specify the file extension and you get a familiar pop-up window where you can browse for the .mp3 to play.

When you have chosen the .mp3 file, pressing the OK button will close the window and the Mp3TextBox will contain the file path for the chosen mp3 file.

private void Mp3Open_Click(object sender, EventArgs e)
{
    WavStop.PerformClick();
    Mp3Stop.PerformClick();
    WavStart.Text = "Start";
    WavTextBox.Text = string.Empty;
    Mp3Start.Text = "Start";
    Mp3TextBox.Text = string.Empty;
    using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, Filter = "Mp3 Files (*.mp3)|*.mp3" })
    {
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            Mp3TextBox.Text = ofd.FileName;
    }
}

Code 2 - Open an .mp3 file

Playing an .mp3 file is the key element in this program. You need to initialize the player object, connect it to the proper MediaHandler(s) and start the streaming. In this case the Start button in the mp3 groupbox is used for starting and pausing the audio playing. When you start the playing, the button's text will change to "Pause" and you can use the same button to pause the stream.

Code 3 shows the event handler method that performs these actions. The first part of the method is for the initialization if it is necessary - that means that there is no player initialized yet.

The player is only initialized once with the same filename. This means that if you open the "audiofile.mp3" at the first time, the MP3Playback object will be initialized and the file path will be stored in the prevmp3file string. When you try to open the "audiofile.mp3" again, the system checks the file path and for the same file it will not initialize a new player object.

In the second part of the method there is the code for the actual playing and pausing according to the fact if the stream has been already started or not.

private void Mp3Start_Click(object sender, EventArgs e)
{
    if (WaveStreamPlayback != null)
        return;
    WavTextBox.Text = string.Empty;
    prevwavfile = string.Empty;
    if (!string.IsNullOrEmpty(Mp3TextBox.Text))
    {
        if (Mp3StreamPlayback == null || prevmp3file != Mp3TextBox.Text)
        {
            try
            {
                WavStop.PerformClick();
                Mp3Stop.PerformClick();
                Mp3StreamPlayback = new MP3StreamPlayback(Mp3TextBox.Text);
                Mp3StreamPlayback.Stopped += (Mp3StreamPlayback_Stopped);
                prevmp3file = Mp3TextBox.Text;
                if (call != null && call.CallState == CallState.InCall)
                    connector.Connect(Mp3StreamPlayback, mediaSender);
                else
                {
                    speaker.Start();
                    connector.Connect(Mp3StreamPlayback, speaker);
                }
            }
            catch (Exception ex)
            {
                ...
            }
        }
        if (!Mp3StreamPlayback.IsStreaming)
        {
            Mp3Start.Text = "Pause";
            Mp3StreamPlayback.StartStreaming();
            Mp3StreamPlayback.IsStreaming = true;
        }
        else
        {
            Mp3Start.Text = "Start";
            Mp3StreamPlayback.PauseStreaming();
            Mp3StreamPlayback.IsStreaming = false;
        }
    }
}

Code 3 - Play an .mp3 file

Stopping an mp3 stream (Code 4) can be done by pressing the Stop button in the Mp3 groupbox. The streaming stops, if there was one, and the Start button will get back the "Start" text.

Stopping a stream differs from pausing it. In both cases the streaming stops, but in case of pausing, it can be started again from the point it stopped. In the case of stopping, the stream can only be started from the very beginning.

private void Mp3Stop_Click(object sender, EventArgs e)
{
    if (Mp3StreamPlayback != null)
    {
        Mp3StreamPlayback.StopStreaming();
        Mp3Start.Text = "Start";
        if (call == null)
            speaker.Stop();
        connector.Disconnect(Mp3StreamPlayback, speaker);
        connector.Disconnect(Mp3StreamPlayback, mediaSender);
        Mp3StreamPlayback.Dispose();
        Mp3StreamPlayback = null;
    }
}

Code 4 - Stop .mp3 playing

The player object needs an event handler for the Stopped event. The handling is done by the tools shown in Code 5.

public event EventHandler<EventArgs> Mp3PlaybackStopped;

void Mp3StreamPlayback_Stopped(object sender, EventArgs e)
{
    if (Mp3PlaybackStopped != null)
        Mp3PlaybackStopped(this, EventArgs.Empty);
}

Code 5 - The event handlers for the player object

The audio playing is set to play the selected file onto the speaker if there is no established call or into the call if there is one. You can try every variation and see how easy is to play an audio file with Ozeki VoIP SIP SDK, and then you can use your latest knowledge to create your own softphone solution with .wav and/or .mp3 support.

This article introduced you the basic knowledge about playing mp3 audio file into a SIP phone call 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 Pricing and licensing information 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