DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Dynamic File Upload Component in Salesforce LWC
  • Introduction to Salesforce Batch Apex [Video]
  • Automate DNS Records Creation With ExternalDNS on AWS Elastic Kubernetes Service
  • Migrating From Lombok to Records in Java

Trending

  • Enhance User Experience With a Formatted Credit Card Input Field
  • Summary of the AJAX Frameworks Comparison
  • Right-Sizing GPU and CPU Resources for Training and Inferencing Using Kubernetes
  • Multimodal RAG Is Not Scary, Ghosts Are Scary

How to record voice calls (C#, SIP)

By 
Jay Hillary user avatar
Jay Hillary
·
Feb. 02, 15 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

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 = "192.168.114.7";
            var domainPort = 5060;
 
            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
 
            // Send SIP registration request
            RegisterAccount(account);
 
            microphone = Microphone.GetDefaultDevice();
            speaker = Speaker.GetDefaultDevice();
            mediaSender = new PhoneCallAudioSender();
            mediaReceiver = new PhoneCallAudioReceiver();
            connector = new MediaConnector();
 
            // Prevents the termination of the application
            Console.ReadLine();
        }
 
        static void RegisterAccount(SIPAccount account)
        {
            try
            {
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
                softphone.IncomingCall += softphone_IncomingCall;
                softphone.RegisterPhoneLine(phoneLine);
            }
            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 e)
        {
            call = e.Item;
            caller = call.DialInfo.CallerID;
            call.CallStateChanged += call_CallStateChanged;
            call.Answer();
        }
 
        static void call_CallStateChanged(object sender, CallStateChangedArgs e)
        {
            Console.WriteLine("Call state: {0}.", e.State);
 
            if (e.State == CallState.Answered)
                SetupDevices();
 
            if (e.State.IsCallEnded())
                CloseDevices();
        }
 
        static void SetupDevices()
        {
            microphone.Start();
            connector.Connect(microphone, mediaSender);
 
            speaker.Start();
            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
 
            mediaSender.AttachToCall(call);
            mediaReceiver.AttachToCall(call);
 
            recorder.Start();  // starts the recording
        }
 
        static void CloseDevices()
        {
            recorder.Dispose();
            microphone.Dispose();
            speaker.Dispose();
 
            mediaReceiver.Detach();
            mediaSender.Detach();
            connector.Dispose();
        }
    }
}
Session Initiation Protocol Record (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Dynamic File Upload Component in Salesforce LWC
  • Introduction to Salesforce Batch Apex [Video]
  • Automate DNS Records Creation With ExternalDNS on AWS Elastic Kubernetes Service
  • Migrating From Lombok to Records in Java

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: