How to listen to an Onvif IP camera audio on speakers in C#
In this guide you will find more information on how to get audio stream from your RTSP/IP camera device. To implement this example, you need to have Ozeki Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.
How to get audio stream from an IP camera device using C#?
Windows forms version
To establish the connection properly between your application and an IP camera you should apply the same code snippet what you have used in the example (How to connect to an IP camera device using C#?). Important: you should study this article in order to find out how to setup your Windows Forms Application correctly.
Getting startedTo get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:
Download Ozeki Camera SDK: | https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html |
Windows forms version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Camera\03_IPCamera \AudioStreamToSpeakers\AudioStreamToSpeakers.sln |
To compile this example you will need Microsoft Visual Studio installed on your computer.
To find more about the previously used functions included here as well, please, visit our previous tutorial
The additional statements and methods of this example are the following:
private Speaker _speaker: with the help of the private Speaker you can create an extra member, a speaker in the class that is able to play the audio stream that we get from the camera
_speaker = Speaker.GetDefaultDevice(): the previously mentioned new object has to be initialized. This is the recommended way for the initializing is getting the default device for playing audio.
_connector.Connect(_camera.AudioChannel, _speaker): besides the video stream connection we need another connection for the audio stream in order to be able to play audio. We use the same MediaConnector object for this task that we have used for the video connection. The arguments of the method call are extremely important: the first one is now the audio channel of the camera and the second one is the speaker object.
If you wish the speaker to work properly you should use the _speaker.Start()
IP camera's audio stream example in C#
MainForm.cs
using System; using System.Windows.Forms; using Ozeki.Camera; using Ozeki.Media; namespace AudioStreamToSpeakers { public partial class MainForm : Form { private OzekiCamera _camera; private CameraURLBuilderWF _myCameraUrlBuilder; private DrawingImageProvider _imageProvider; private MediaConnector _connector; private Speaker _speaker; public MainForm() { InitializeComponent(); _myCameraUrlBuilder = new CameraURLBuilderWF(); _connector = new MediaConnector(); _imageProvider = new DrawingImageProvider(); _speaker = Speaker.GetDefaultDevice(); videoViewerWF1.SetImageProvider(_imageProvider); } private void btn_Compose_Click(object sender, EventArgs e) { var result = _myCameraUrlBuilder.ShowDialog(); if (result != DialogResult.OK) return; tb_CameraURL.Text = _myCameraUrlBuilder.CameraURL; btn_Connect.Enabled = true; } private void btn_Connect_Click(object sender, EventArgs e) { if (_camera != null) { videoViewerWF1.Stop(); _camera.Disconnect(); _connector.Disconnect(_camera.VideoChannel, _imageProvider); _connector.Disconnect(_camera.AudioChannel, _speaker); } btn_Connect.Enabled = false; _camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL); _camera.CameraStateChanged += _camera_CameraStateChanged; _connector.Connect(_camera.AudioChannel, _speaker); _connector.Connect(_camera.VideoChannel, _imageProvider); _camera.Start(); if (_speaker != null) _speaker.Start(); videoViewerWF1.Start(); } void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { InvokeGuiThread(() => { switch (e.State) { case CameraState.Streaming: btn_Disconnect.Enabled = true; break; case CameraState.Disconnected: btn_Disconnect.Enabled = false; btn_Connect.Enabled = true; break; } }); } private void InvokeGuiThread(Action action) { BeginInvoke(action); } private void btn_Disconnect_Click(object sender, EventArgs e) { videoViewerWF1.Stop(); _camera.Disconnect(); _speaker.Stop(); _connector.Disconnect(_camera.VideoChannel, _imageProvider); _connector.Disconnect(_camera.AudioChannel, _speaker); } } }
Code 1 - IP camera's audio stream example in C#
Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.
GUI
After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture. Besides this you will hear the audio stream of the camera on your default speaker device.
Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.
Form1.Designer.cs
namespace AudioStreamToSpeakers { partial class MainForm { ////// Required designer variable. /// private System.ComponentModel.IContainer components = null; ////// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code ////// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF(); this.btn_Compose = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.tb_CameraURL = new System.Windows.Forms.TextBox(); this.btn_Connect = new System.Windows.Forms.Button(); this.btn_Disconnect = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.btn_Disconnect); this.groupBox1.Controls.Add(this.btn_Connect); this.groupBox1.Controls.Add(this.tb_CameraURL); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.btn_Compose); this.groupBox1.Location = new System.Drawing.Point(13, 13); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(320, 77); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Connect"; // // videoViewerWF1 // this.videoViewerWF1.BackColor = System.Drawing.Color.Black; this.videoViewerWF1.FlipMode = Ozeki.Media.FlipMode.None; this.videoViewerWF1.FrameStretch = Ozeki.Media.FrameStretch.Uniform; this.videoViewerWF1.FullScreenEnabled = true; this.videoViewerWF1.Location = new System.Drawing.Point(13, 96); this.videoViewerWF1.Name = "videoViewerWF1"; this.videoViewerWF1.RotateAngle = 0; this.videoViewerWF1.Size = new System.Drawing.Size(320, 240); this.videoViewerWF1.TabIndex = 1; this.videoViewerWF1.Text = "videoViewerWF1"; // // btn_Compose // this.btn_Compose.Location = new System.Drawing.Point(239, 15); this.btn_Compose.Name = "btn_Compose"; this.btn_Compose.Size = new System.Drawing.Size(75, 23); this.btn_Compose.TabIndex = 0; this.btn_Compose.Text = "Compose"; this.btn_Compose.UseVisualStyleBackColor = true; this.btn_Compose.Click += new System.EventHandler(this.btn_Compose_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(7, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(71, 13); this.label1.TabIndex = 1; this.label1.Text = "Camera URL:"; // // tb_CameraURL // this.tb_CameraURL.Location = new System.Drawing.Point(75, 17); this.tb_CameraURL.Name = "tb_CameraURL"; this.tb_CameraURL.Size = new System.Drawing.Size(158, 20); this.tb_CameraURL.TabIndex = 2; // // btn_Connect // this.btn_Connect.Enabled = false; this.btn_Connect.Location = new System.Drawing.Point(75, 43); this.btn_Connect.Name = "btn_Connect"; this.btn_Connect.Size = new System.Drawing.Size(75, 23); this.btn_Connect.TabIndex = 3; this.btn_Connect.Text = "Connect"; this.btn_Connect.UseVisualStyleBackColor = true; this.btn_Connect.Click += new System.EventHandler(this.btn_Connect_Click); // // btn_Disconnect // this.btn_Disconnect.Enabled = false; this.btn_Disconnect.Location = new System.Drawing.Point(158, 43); this.btn_Disconnect.Name = "btn_Disconnect"; this.btn_Disconnect.Size = new System.Drawing.Size(75, 23); this.btn_Disconnect.TabIndex = 4; this.btn_Disconnect.Text = "Disconnect"; this.btn_Disconnect.UseVisualStyleBackColor = true; this.btn_Disconnect.Click += new System.EventHandler(this.btn_Disconnect_Click); // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(346, 354); this.Controls.Add(this.videoViewerWF1); this.Controls.Add(this.groupBox1); this.Name = "MainForm"; this.Text = "Audio stream to speakers"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btn_Disconnect; private System.Windows.Forms.Button btn_Connect; private System.Windows.Forms.TextBox tb_CameraURL; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button btn_Compose; private Ozeki.Media.VideoViewerWF videoViewerWF1; } }
Code 2 - GUI example in C#
DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.
Related Pages
FAQ
Below you can find the answers for the most frequently asked questions related to this topic:
-
How can I get the URL of the camera?
You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)
-
I have not managed to build the solution. How to solve it?
- Please set the Target framework property of the project to .NET 4.0.
- You should add the OzekiSDK.dll and the System.Drawing.dll to the references of the solution.
- Please import the missing classes.
-
I cannot hear anything. How to solve this?
- There are cameras which do not support the audio sending or do not have audio recorder.
- You should verify whether the speaker is not muted.
- You should verify whether the sound card works properly.
More information
- How to connect to an USB camera and display the picture in C#
- How to connect to an RTSP camera and display the picture in C#
- How to connect to an ONVIF camera and display the image in C#
- C# Play audio from RTSP/ONVIF camera
- How to send audio to a camera in C#
- Query the audio video frame rate resolution codec
- How to resize the picture of a broadcasted camera for displaying in C#
- How to mirror the camera picture vertically and horizontally in C#
- Setup camera image Brightness Saturation Contrast values in C#
- How to control the frame rate in C#
- How to setup the white balance in C#
- How to use backlight compensation in C#
- Change the video resolution
- Concat multiple video source in C#