Question: How to use RecorderEx and PlayerEx in console application or Windows Service?
Answer: In order to use RecorderEx and PlayerEx in a console application or Windows Service need to create instances of these classes by using a parameterized constructor:
new RecorderEx(true) and new PlayerEx(true), respectively. The same is true for multi-threaded recorder and player.
Below is an example of full duplex Recorder and Player.
using System;
using Alvas.Audio;
namespace AudioConsCs
{
class Program
{
static void Main(string[] args)
{
rex.Data += new RecorderEx.DataEventHandler(rex_Data);
rex.Open += new EventHandler(rex_Open);
rex.Close += new EventHandler(rex_Close);
rex.Format = pcmFormat;
rex.StartRecord();
Console.WriteLine("Please press enter to exit!");
Console.ReadLine();
rex.StopRecord();
}
static RecorderEx rex = new RecorderEx(true);
static PlayerEx play = new PlayerEx(true);
static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);
static void rex_Open(object sender, EventArgs e)
{
play.OpenPlayer(pcmFormat);
play.StartPlay();
}
static void rex_Close(object sender, EventArgs e)
{
play.ClosePlayer();
}
static void rex_Data(object sender, DataEventArgs e)
{
byte[] data = e.Data;
play.AddData(data);
}
}
}
Below is an example of multithreaded full duplex Recorder and Player in Windows Service.
/*
* rem run cmd as administrator
* rem install service
* \\Windows\Microsoft.NET\Framework\v2.0.50727\installutil AudioWindowsServiceCs.exe
* rem uninstall service
* \\Windows\Microsoft.NET\Framework\v2.0.50727\installutil AudioWindowsServiceCs.exe /u
*/
using System;
using System.ServiceProcess;
using System.Threading;
using Alvas.Audio;
namespace AudioWindowsServiceCs
{
public partial class AudioService : ServiceBase
{
public AudioService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Thread t = new Thread(Start);
t.Start();
}
protected override void OnStop()
{
rex.StopRecord();
}
static void Start()
{
rex.Data += new RecorderEx.DataEventHandler(rex_Data);
rex.Open += new EventHandler(rex_Open);
rex.Close += new EventHandler(rex_Close);
rex.Format = pcmFormat;
rex.StartRecord();
}
static RecorderEx rex = new RecorderEx(true);
static PlayerEx play = new PlayerEx(true);
static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);
static void rex_Open(object sender, EventArgs e)
{
play.OpenPlayer(pcmFormat);
play.StartPlay();
}
static void rex_Close(object sender, EventArgs e)
{
play.ClosePlayer();
}
static void rex_Data(object sender, DataEventArgs e)
{
byte[] data = e.Data;
play.AddData(data);
}
}
}
Download examples and Alvas.Audio Trial