How to play an mp3 file into a voice call using C#?


This example demonstrates how to create an mp3 player, which is able to play an mp3 file into the call as voice. Before you start to study how to stream mp3 file into a call, you might need to study the How to register to a SIP PBX chapter and the How to ring a SIP extension chapter first.
To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki.dll should be added to your visual studio project.

when the call has benn estabilished the softphone is able to start streaming
Figure 1 - when the call has been established, the softphone is able to start streaming the mp3 file

What is mp3 streamer used for during a SIP voice call? How does it work?

Streaming media is multimedia that is constantly received by and presented to an end-user while being delivered by a provider. An mp3 media player can begin playing the mp3 file's voice data before the entire file has been transmitted.

In the case of softphones, mp3 streamers are used to play mp3 files into calls, for example in the cases of IVRs (Interactive Voice Response), voicemails, auto-answerers etc. Basically, mp3 stream players work almost the same way as microphones, but the stream of audio data is being sent from an mp3 file instead from a microphone, which means mp3 streamers can be attached to calls with the correct mediasender object.

Pulse-code modulation (PCM) is a method, used to digitally represent sampled analog signals. In this example, PCM means the uncompressed form of digital audio data. This example is using the G.711 waveform codec, which is an ITU-T standard for audio companding, and it is primarily used in telephony and to communicate over IP networks. It is a lossless, encoded data (to about 50%), which has got 64 kbit/s transfer rate.

The Real-time Transport Protocol (RTP) defines a standardized packet format for delivering audio and video over IP networks. RTP is used extensively in communication and entertainment systems that involve streaming media, such as telephony, video teleconference applications, television services and web-based push-to-talk features.
RTP is designed for end-to-end, real-time transfer of stream data, and is able to transfer the data to multiple destinations. The RTP package construction:

You can learn more about RTP from the How to work with RTP in VoIP SIP calls article.

How to play mp3 file into call using c#?

Ozeki VoIP SIP SDK provides the MP3StreamPlayback class for the purpose to create mp3 streamer objects, which are receiving the mp3 file's path and name as parameter, and are able to be attached to the call with the correct media sender object. The user only have to call the StartStreaming() method of the object, so the mp3 streamer can begin the streaming.
Please note that, there are much more media handlers provided by the SDK, for example you can even record calls to mp3 files, or play - and record - wav files as well.

Mp3 file streaming into call example in C#

using System;
using Ozeki.Media;
using Ozeki.VoIP;

namespace Play_MP3_Into_Call
    class Program
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneCall call;
        static MediaConnector connector;
        static PhoneCallAudioSender mediaSender;
        static MP3StreamPlayback mp3Player;

        private static void Main(string[] args)
            // Create a softphone object with RTP port range 5000-10000
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            // SIP account registration data, (supplied by your VoIP service provider)
            var registrationRequired = true;
            var userName = "858";
            var displayName = "858";
            var authenticationId = "858";
            var registerPassword = "858";
            var domainHost = "";
            var domainPort = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

            // Send SIP regitration request

            mp3Player = new MP3StreamPlayback("helloworld.mp3");
            mediaSender = new PhoneCallAudioSender();
            connector = new MediaConnector();

            // Prevents the termination of the application

        static void RegisterAccount(SIPAccount account)
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
            catch (Exception ex)
                Console.WriteLine("Error during SIP registration: " + ex);

        static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
            if (e.State == RegState.NotRegistered || e.State == RegState.Error)
                Console.WriteLine("Registration failed!");

            if (e.State == RegState.RegistrationSucceeded)
                Console.WriteLine("Registration succeeded - Online!");

        private static void CreateCall()
            var numberToDial = "853";
            call = softphone.CreateCallObject(phoneLine, numberToDial);
            call.CallStateChanged += call_CallStateChanged;

        static void SetupMp3Player()
            connector.Connect(mp3Player, mediaSender);


            Console.WriteLine("The mp3 player is streaming.");

        static void call_CallStateChanged(object sender, CallStateChangedArgs e)
            Console.WriteLine("Call state: {0}.", e.State);

            if (e.State == CallState.Answered)

Training guides, simple examples

If you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.

Communication throught the network

When the call has been accepted by the called party, the softphone is ready to stream the given mp3 file into the call through the UDP network as RTP packages. This example also writes to the console that the mp3 streamer has started to stream the file.

Step1: the application notifies the user about the beginning of the streaming

The mp3 player is streaming.

Step 2: the mp3 is being sent to the destination as RTP packages (a sample package)

[Stream setup by SDP (frame 28)]
10.. .... = Version: RFC 1889 Version (2)
..0. .... = Padding: False
...0 .... = Extension: False
.... 0000 = Contributing source identifiers count: 0
1... .... = Marker: True
Payload type: ITU-T G.711 PCMU (0)
Sequence number: 7133
[Extended sequence number: 72669]
Timestamp: 85000
Synchronization Source identifier: 0x712f7356 (1898935126)
Payload: d55555545657545756565455575051575650515557515050...

If you have any questions or need assistance, please contact us at

Related Pages

Copyright © 2000- - Ozeki Ltd |
Legal | Privacy | Terms of use | 7345 | | Login