How to record audio stream into .WAV in C#
In this guide you can find information on how to record audio stream in .wav file format coming from an IP camera. 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 record audio stream with an IP camera device using C#?
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/WPF Application correctly.
Getting started
To 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\Other\AudioStreamRecorder |
To compile this example you will need Microsoft Visual Studio installed on your computer.
The additional statements and methods of this example are the following:
_mediaConnector.Connect(__camera.VideoChannel, _imageProvider): This method establishes
the connection between the image we get from our camera and the image provider object
that is used to display the camera's image on the graphical user interface.
_videoViewer.SetImageProvider(_imageProvider): We set the image provider to
the video viewer object in order to make the application be able to display
the image we get from the camera.
_camera.Start(): In order to receive image from the camera we need to start this method.
_videoViewer.Start(): In order to display the image on the GUI we need to start this method as well.
_recorder.Start(): In order to record the sound from the camera we need to start this method.
_connector.Connect(_camera.AudioChannel, _recorder): You need to connect the audio channel of your camera to record.
Implement image settings of an IP camera in C#
Windows Form |
Windows forms version
Mainform.cs
using System; using System.Windows.Forms; using Ozeki.Camera; using Ozeki.Media; namespace AudioStreamRecorder { public partial class Mainform : Form { private OzekiCamera _camera; private DrawingImageProvider _imageProvider; private MediaConnector _connector; private CameraURLBuilderWF _myCameraUrlBuilder; static WaveStreamRecorder _recorder; static string _filename; public Mainform() { InitializeComponent(); _connector = new MediaConnector(); _imageProvider = new DrawingImageProvider(); videoViewerWF1.SetImageProvider(_imageProvider); _myCameraUrlBuilder = new CameraURLBuilderWF(); _filename = string.Format("{0}-{1}-{2}.wav", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); _recorder = new WaveStreamRecorder(_filename); } private void connectBtn_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) { _camera.CameraStateChanged -= _camera_CameraStateChanged; _camera.Disconnect(); _connector.Disconnect(_camera.VideoChannel, _imageProvider); _camera.Dispose(); _camera = null; } _camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL); _camera.CameraStateChanged += _camera_CameraStateChanged; _connector.Connect(_camera.AudioChannel, _recorder); _connector.Connect(_camera.VideoChannel, _imageProvider); _camera.Start(); videoViewerWF1.Start(); } void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { InvokeThread(() => { statelabel.Text = e.State.ToString(); if (e.State == CameraState.Streaming) { Streaming(); } if (e.State == CameraState.Disconnected) Disconnect(); }); } private void Disconnect() { btn_Connect.Enabled = true; btn_Disconnect.Enabled = false; btn_Start.Enabled = false; btn_Stop.Enabled = false; } private void Streaming() { btn_Connect.Enabled = false; btn_Disconnect.Enabled = true; btn_Start.Enabled = true; btn_Stop.Enabled = false; } private void btn_Disconnect_Click(object sender, EventArgs e) { if (_camera == null) return; _camera.Disconnect(); _connector.Disconnect(_camera.VideoChannel, _imageProvider); _camera = null; } void InvokeThread(Action action) { BeginInvoke(action); } private void btn_Start_Click(object sender, EventArgs e) { if (_camera != null) { _recorder.Start(); recordlabel.Text = "Recording"; btn_Start.Enabled = false; btn_Stop.Enabled = true; } } private void btn_Stop_Click(object sender, EventArgs e) { if (_camera != null) { _recorder.Stop(); recordlabel.Text = "Stop Recording"; btn_Start.Enabled = true; btn_Stop.Enabled = false; } } } }
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
Below you can find the code that belongs to the interface of the previously presented application. Clicking the "Start" button begins the recording process and when you push the "Stop" button the process ends. The interval of the recorded audio will be the interval between the two button hits.
Mainform.Designer.Cs
namespace AudioStreamRecorder { partial class Mainform { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.btn_Disconnect = new System.Windows.Forms.Button(); this.btn_Connect = new System.Windows.Forms.Button(); this.tb_cameraUrl = new System.Windows.Forms.TextBox(); this.CameraURL = new System.Windows.Forms.Label(); this.connectBtn = new System.Windows.Forms.Button(); this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF(); this.btn_Start = new System.Windows.Forms.Button(); this.btn_Stop = new System.Windows.Forms.Button(); this.statelabel = new System.Windows.Forms.Label(); this.audiorecord = new System.Windows.Forms.Label(); this.recordlabel = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btn_Disconnect // this.btn_Disconnect.Enabled = false; this.btn_Disconnect.Location = new System.Drawing.Point(177, 38); this.btn_Disconnect.Name = "btn_Disconnect"; this.btn_Disconnect.Size = new System.Drawing.Size(75, 23); this.btn_Disconnect.TabIndex = 12; this.btn_Disconnect.Text = "Disconnect"; this.btn_Disconnect.UseVisualStyleBackColor = true; this.btn_Disconnect.Click += new System.EventHandler(this.btn_Disconnect_Click); // // btn_Connect // this.btn_Connect.Enabled = false; this.btn_Connect.Location = new System.Drawing.Point(87, 38); this.btn_Connect.Name = "btn_Connect"; this.btn_Connect.Size = new System.Drawing.Size(75, 23); this.btn_Connect.TabIndex = 11; this.btn_Connect.Text = "Connect"; this.btn_Connect.UseVisualStyleBackColor = true; this.btn_Connect.Click += new System.EventHandler(this.btn_Connect_Click); // // tb_cameraUrl // this.tb_cameraUrl.Location = new System.Drawing.Point(87, 12); this.tb_cameraUrl.Name = "tb_cameraUrl"; this.tb_cameraUrl.ReadOnly = true; this.tb_cameraUrl.Size = new System.Drawing.Size(165, 20); this.tb_cameraUrl.TabIndex = 10; // // CameraURL // this.CameraURL.AutoSize = true; this.CameraURL.Location = new System.Drawing.Point(10, 15); this.CameraURL.Name = "CameraURL"; this.CameraURL.Size = new System.Drawing.Size(71, 13); this.CameraURL.TabIndex = 9; this.CameraURL.Text = "Camera URL:"; // // connectBtn // this.connectBtn.Location = new System.Drawing.Point(258, 10); this.connectBtn.Name = "connectBtn"; this.connectBtn.Size = new System.Drawing.Size(75, 23); this.connectBtn.TabIndex = 8; this.connectBtn.Text = "Compose"; this.connectBtn.UseVisualStyleBackColor = true; this.connectBtn.Click += new System.EventHandler(this.connectBtn_Click); // // 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(12, 89); this.videoViewerWF1.Name = "videoViewerWF1"; this.videoViewerWF1.RotateAngle = 0; this.videoViewerWF1.Size = new System.Drawing.Size(321, 232); this.videoViewerWF1.TabIndex = 13; this.videoViewerWF1.Text = "videoViewerWF1"; // // btn_Start // this.btn_Start.Enabled = false; this.btn_Start.Location = new System.Drawing.Point(12, 355); this.btn_Start.Name = "btn_Start"; this.btn_Start.Size = new System.Drawing.Size(75, 23); this.btn_Start.TabIndex = 14; this.btn_Start.Text = "Start"; this.btn_Start.UseVisualStyleBackColor = true; this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click); // // btn_Stop // this.btn_Stop.Enabled = false; this.btn_Stop.Location = new System.Drawing.Point(93, 355); this.btn_Stop.Name = "btn_Stop"; this.btn_Stop.Size = new System.Drawing.Size(75, 23); this.btn_Stop.TabIndex = 15; this.btn_Stop.Text = "Stop"; this.btn_Stop.UseVisualStyleBackColor = true; this.btn_Stop.Click += new System.EventHandler(this.btn_Stop_Click); // // statelabel // this.statelabel.AutoSize = true; this.statelabel.Location = new System.Drawing.Point(12, 66); this.statelabel.Name = "lb_State"; this.statelabel.Size = new System.Drawing.Size(0, 13); this.statelabel.TabIndex = 16; // // audiorecord // this.audiorecord.AutoSize = true; this.audiorecord.Location = new System.Drawing.Point(12, 324); this.audiorecord.Name = "audiorecord"; this.audiorecord.Size = new System.Drawing.Size(64, 13); this.audiorecord.TabIndex = 18; this.audiorecord.Text = "Audiorecord"; // // recordlabel // this.recordlabel.AutoSize = true; this.recordlabel.Location = new System.Drawing.Point(187, 364); this.recordlabel.Name = "lb_state2"; this.recordlabel.Size = new System.Drawing.Size(0, 13); this.recordlabel.TabIndex = 19; // // Mainform // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(346, 387); this.Controls.Add(this.recordlabel); this.Controls.Add(this.audiorecord); this.Controls.Add(this.statelabel); this.Controls.Add(this.btn_Stop); this.Controls.Add(this.btn_Start); this.Controls.Add(this.videoViewerWF1); this.Controls.Add(this.btn_Disconnect); this.Controls.Add(this.btn_Connect); this.Controls.Add(this.tb_cameraUrl); this.Controls.Add(this.CameraURL); this.Controls.Add(this.connectBtn); this.Name = "Mainform"; this.Text = "AudioStreamRecorder"; this.ResumeLayout(false); this.PerformLayout(); } #endregion 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 CameraURL; private System.Windows.Forms.Button connectBtn; private Ozeki.Media.VideoViewerWF videoViewerWF1; private System.Windows.Forms.Button btn_Start; private System.Windows.Forms.Button btn_Stop; private System.Windows.Forms.Label statelabel; private System.Windows.Forms.Label audiorecord; private System.Windows.Forms.Label recordlabel; } }
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 to the references of the solution.
- Please import the missing classes.
-
At recording I got an error. Why?
Make sure the you have enough place recording.
More information
- How to record audio and video stream into MPEG-4 in C#
- How to take a picture snapshot and save it as .jpg in C#
- How to implement circular buffer video recording in C#
- How to set frame rate, codec, and picture size for video recording (resample video stream) in Cx
- How to record from multiple cameras simultaneously in C#
- How to record audio stream into .WAV in C#
- Screen capture function - ScreenCapture