How to record voice calls (C#, SIP)

DZone 's Guide to

How to record voice calls (C#, SIP)

· ·
Free Resource

Do you need a couple of lines of codes to implement voice call recording? Just copy and use this code! It really helped me! To share my success, this content is intended to be a short tutorial on how to record SIP voice calls using C#.NET. This guide is recommended for intermediate-level C# developers primarily, since only one complete code is presented instead of explaining its snippets step-by-step. First of all, carry out the following configuration steps to make sure that your system is ready for coding:

  • Create a new project (Visual C# Console Application) in Visual Studio
  • Add the VoIPSDK.dll  file to your references  - it is available on this website 
  • Install a VoIP PBX (allowing you to initiate and accept voice calls) and create a new SIP account for the call recorder

The brief description of the code: After the necessary using lines there is a need a softphone and  a phone line object, and some further media handler objects to be able to record audio streams into .wav audio files. Thereafter, you need to specify a SIP account for the call recorder (including the data of your PBX). To ensure that the two audio streams will be mixed you need to connect the microphone and the mediaReceiver objects to the mixer. After this you need to connect the mixer to the recorder that will record the mixed audio stream into the file specified by the filename parameter. The rest of the code presents how to save and store a call as .wav file.

using System;
using Ozeki.Media.MediaHandlers;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;
namespace Record_Voice_Call
    class Program
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneCall call;
        static string caller;
        static string filename;
        static Microphone microphone;
        static Speaker speaker;
        static PhoneCallAudioSender mediaSender;
        static PhoneCallAudioReceiver mediaReceiver;
        static MediaConnector connector;
        static WaveStreamRecorder recorder;
        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 = "100";
            var displayName = "100";
            var authenticationId = "100";
            var registerPassword = "100";
            var domainHost = "";
            var domainPort = 5060;
            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
            // Send SIP registration request
            microphone = Microphone.GetDefaultDevice();
            speaker = Speaker.GetDefaultDevice();
            mediaSender = new PhoneCallAudioSender();
            mediaReceiver = new PhoneCallAudioReceiver();
            connector = new MediaConnector();
            // Prevents the termination of the application
        static void RegisterAccount(SIPAccount account)
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
                softphone.IncomingCall += softphone_IncomingCall;
            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!");
        static void softphone_IncomingCall(object sender, VoIPEventArgs
            call = e.Item;
            caller = call.DialInfo.CallerID;
            call.CallStateChanged += call_CallStateChanged;
        static void call_CallStateChanged(object sender, CallStateChangedArgs e)
            Console.WriteLine("Call state: {0}.", e.State);
            if (e.State == CallState.Answered)
            if (e.State.IsCallEnded())
        static void SetupDevices()
            connector.Connect(microphone, mediaSender);
            connector.Connect(mediaReceiver, speaker);
            filename = string.Format("{0}-{1}-{2}-{3}.wav", caller, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); // a string to generate file name
            recorder = new WaveStreamRecorder(filename);    // new recorder object
            connector.Connect(microphone, recorder);        // connects the outgoing voice to the recorder
            connector.Connect(mediaReceiver, recorder);     // connects the incoming voice to the recorder
            recorder.Start();  // starts the recording
        static void CloseDevices()

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}