I'm a beginer in Csharp and I'm trying to developpe using NAudio.I found in NAudio site a code which gives me the possibility to playback sound.
In my case I will create three buttons and each button is related to an external sound card,when I click in a button I want to hear sound from the speaker which is related to my sound card( button 1 is related to sound card 1,...).So each function "play back" should play sound in a specific device.For performance issue it's not very pratic to create a "playback" function for each button (so for each sound card)and my sound allows me to play sound in only a sound card,Please can you help me to correct the code??????? It's very important for me.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.CoreAudioApi;
namespace PaGa
{
public partial class PlaybackForm : Form
{
IWavePlayer waveOut;
string fileName = null;
WaveStream mainOutputStream;
WaveChannel32 volumeStream;
public PlaybackForm()
{
InitializeComponent();
}
private void buttonPlay_Click(object sender, EventArgs e)
{
if (waveOut != null)
{
if (waveOut.PlaybackState == PlaybackState.Playing)
{
return;
}
else if (waveOut.PlaybackState == PlaybackState.Paused)
{
waveOut.Play();
return;
}
}
// we are in a stopped state
// TODO: only re-initialise if necessary
if (String.IsNullOrEmpty(fileName))
{
toolStripButtonOpenFile_Click(sender, e);
}
if (String.IsNullOrEmpty(fileName))
{
return;
}
try
{
CreateWaveOut();
}
catch (Exception driverCreateException)
{
MessageBox.Show(String.Format("{0}", driverCreateException.Message));
return;
}
mainOutputStream = CreateInputStream(fileName);
trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;
labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,
mainOutputStream.TotalTime.Seconds);
trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;
try
{
waveOut.Init(mainOutputStream);
}
catch (Exception initException)
{
MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");
return;
}
// not doing Volume on IWavePlayer any more
volumeStream.Volume = volumeSlider1.Volume;
waveOut.Play();
}
private WaveStream CreateInputStream(string fileName)
{
WaveChannel32 inputStream;
if (fileName.EndsWith(".wav"))
{
WaveStream readerStream = new WaveFileReader(fileName);
if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
{
readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
readerStream = new BlockAlignReductionStream(readerStream);
}
if (readerStream.WaveFormat.BitsPerSample != 16)
{
var format = new WaveFormat(readerStream.WaveFormat.SampleRate,
16, readerStream.WaveFormat.Channels);
readerStream = new WaveFormatConversionStream(format, readerStream);
}
inputStream = new WaveChannel32(readerStream);
}
else if (fileName.EndsWith(".mp3"))
{
WaveStream mp3Reader = new Mp3FileReader(fileName);
WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
WaveStream blockAlignedStream = new BlockAlignReductionStream(pcmStream);
inputStream = new WaveChannel32(blockAlignedStream);
}
else
{
throw new InvalidOperationException("Unsupported extension");
}
// we are not going into a mixer so we don't need to zero pad
//((WaveChannel32)inputStream).PadWithZeroes = false;
volumeStream = inputStream;
var meteringStream = new MeteringStream(inputStream, inputStream.WaveFormat.SampleRate / 10);
meteringStream.StreamVolume += new EventHandler
return meteringStream;
}
void meteringStream_StreamVolume(object sender, StreamVolumeEventArgs e)
{
volumeMeter1.Amplitude = e.MaxSampleValues[0];
waveformPainter1.AddMax(e.MaxSampleValues[0]);
if (e.MaxSampleValues.Length > 1)
{
volumeMeter2.Amplitude = e.MaxSampleValues[1];
waveformPainter2.AddMax(e.MaxSampleValues[1]);
}
}
private void CreateWaveOut()
{
CloseWaveOut();
int latency = (int)comboBoxLatency.SelectedItem;
//if (radioButtonWaveOut.Checked)
{
//WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?
WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
// WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
// WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();
WaveOut outputDevice = new WaveOut(callbackInfo);
outputDevice.DesiredLatency = latency;
waveOut = outputDevice;
}
}
private void CloseWaveOut()
{
if (waveOut != null)
{
waveOut.Stop();
}
if (mainOutputStream != null)
{
// this one really closes the file and ACM conversion
volumeStream.Close();
volumeStream = null;
// this one does the metering stream
mainOutputStream.Close();
mainOutputStream = null;
}
if (waveOut != null)
{
waveOut.Dispose();
waveOut = null;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CloseWaveOut();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBoxLatency.Items.Add(25);
comboBoxLatency.Items.Add(50);
comboBoxLatency.Items.Add(100);
comboBoxLatency.Items.Add(150);
comboBoxLatency.Items.Add(200);
comboBoxLatency.Items.Add(300);
comboBoxLatency.Items.Add(400);
comboBoxLatency.Items.Add(500);
comboBoxLatency.SelectedIndex = 5;
}
private void buttonPause_Click(object sender, EventArgs e)
{
if (waveOut != null)
{
if (waveOut.PlaybackState == PlaybackState.Playing)
{
waveOut.Pause();
}
}
}
private void volumeSlider1_VolumeChanged(object sender, EventArgs e)
{
if (mainOutputStream != null)
{
volumeStream.Volume = volumeSlider1.Volume;
}
}
private void buttonControlPanel_Click(object sender, EventArgs e)
{
AsioOut asio = waveOut as AsioOut;
if (asio != null)
{
asio.ShowControlPanel();
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (waveOut != null)
{
waveOut.Stop();
trackBarPosition.Value = 0;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (waveOut != null)
{
if (mainOutputStream.Position >= mainOutputStream.Length)
{
buttonStop_Click(sender, e);
}
else
{
TimeSpan currentTime = mainOutputStream.CurrentTime;
trackBarPosition.Value = (int)currentTime.TotalSeconds;
labelCurrentTime.Text = String.Format("{0:00}:{1:00}", (int)currentTime.TotalMinutes,
currentTime.Seconds);
}
}
}
private void trackBarPosition_Scroll(object sender, EventArgs e)
{
if (waveOut != null)
{
mainOutputStream.CurrentTime = TimeSpan.FromSeconds(trackBarPosition.Value);
}
}
private void toolStripButtonOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "All Supported Files (*.wav, *.mp3)|*.wav;*.mp3|All Files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog.FileName;
}
}
}
}
Thank you in advance.
Good Day.
nicole castelPosted May 19, 2011, 5:27 PM
Sorry another time for bothering and thnk you very very much for what you have done for me Frogleg.
FroglegPosted May 19, 2011, 4:59 PM
It's no good if I do everyting for you
nicole castelPosted May 18, 2011, 10:01 AM
Concernig th "Stream message" button it allows m to send a direct vocal message to one or more areas,I find this code in TheCodeProject.com but it works only for one sound card (the default one ).I would guess that the -1 parameters are the device number.I changed it but I have always the same problem,when I change -1 to 1 I can hear my voice from the default speakers,but when I write 2 or 3 I cat hear anything.I also tried to Use NAudio.I used WaveOut instead of WaveOutPlayer and WaveIn instead of WaveInReader but this changes caused many exceptions .
FroglegPosted May 18, 2011, 3:46 AM
I had to remove background image because of too much flickering
I think you should do some coding as well
You need to save recorded messages to a folder, naming them by date and time
When you open message log, it will look in that folder and list all messages in the listbox
So roll your sleeves up and give it a go
Suthish NairPosted May 18, 2011, 12:13 AM
nicole castelPosted May 18, 2011, 12:01 AM
I think that I'm not your only fun ;) hey Suthish Nair dont run the Sample project,it contains my name ( :p I'm kidding)
Suthish NairPosted May 17, 2011, 11:52 PM
FroglegPosted May 17, 2011, 11:52 PM
nicole castelPosted May 17, 2011, 11:30 PM
And please tell me if you have new ideas for my project.
Thank you in advance Frogleg :)
FroglegPosted May 17, 2011, 11:20 PM
You need to tell me what you want
nicole castelPosted May 17, 2011, 6:53 PM
Now tell me please how can I add your ideas to the other project?????? Or do you have another idea ???????? :)
FroglegPosted May 17, 2011, 6:04 PM
nicole castelPosted May 17, 2011, 3:32 PM
FroglegPosted May 17, 2011, 2:52 PM
nicole castelPosted May 17, 2011, 9:00 AM
nicole castelPosted May 17, 2011, 8:46 AM
Thank you Frogleg,I saw your forms and I think that this is awesome but when I run run the proect I cant see anything,did you try it in your pc???????
FroglegPosted May 17, 2011, 4:43 AM
FroglegPosted May 17, 2011, 1:41 AM
When there are multiple areas - there are multiple playbackforms - so we need to playback with out creating a playbackform
nicole castelPosted May 16, 2011, 5:39 PM
FroglegPosted May 16, 2011, 5:26 PM
I'll post back later
nicole castelPosted May 16, 2011, 5:14 PM
FroglegPosted May 16, 2011, 5:02 PM
1: To record - 3 areas means max 3 recordings at one time
2: To play message - the check boxes will allow user multiple areas
ie
1, 2, 3, 1+2+3,1+2,1+3, 2+3
which equals 7 options
Is that right ?
nicole castelPosted May 16, 2011, 4:41 PM
Concerning the button,how can I use the switch??????? Can you explain more your idea please??????
FroglegPosted May 16, 2011, 4:24 PM
So you want to record messages from different locations ? = area 1,area 2 and area 3
And then be able to play back
nicole castelPosted May 16, 2011, 4:05 PM
FroglegPosted May 16, 2011, 4:00 PM
5 for stream and 5 for playback - I don't understand
nicole castelPosted May 16, 2011, 4:00 PM
FroglegPosted May 16, 2011, 3:50 PM
Are the other forms going to be MDI children ?
In Connection.cs you have
this.MdiParent.Menu.MenuItems[0].MenuItems[0].Text = "Disconnection";
this.MdiParent.Text = this.MdiParent.Text + " ------- Welcome " + rs.GetString(5) + " " + rs.GetString(3) + " " + rs.GetString(4) + " ---------";
this.Close();
Form1 form1 = new Form1();
form1.Show();
This will load form1 - do you want it to be a child form ?
nicole castelPosted May 16, 2011, 3:08 PM
I have a start window "ReceptionForm" in which I put a BackgroundImage that contains the name of my application, it also contains a Menu "File" and in it there is "connection" when I click on it a login window appears to allow me to enter my login and password, and I have another window "Form1 " with which I can perform the functionality of my application.
My problem is that I have not found a solution that allows me to display "Form1" instead of bachgroundimage in "ReceptionForm" when a user authenticates.You can use "hammami" as a login and "aa" as a password
Do you have an idea?
FroglegPosted May 16, 2011, 2:53 PM
FroglegPosted May 16, 2011, 2:44 PM
Use regions in your code
#region ?
#endregion
eg
#region Record sound
//all recording code in here
//you will be able to collapse and expand your code to make it easier to read
#endregion
nicole castelPosted May 16, 2011, 2:42 PM
If you dont mind can I ask you another questions please???????
FroglegPosted May 16, 2011, 2:34 PM
private void btnStop_Click(object sender, EventArgs e)
{
foreach (var playbackSession in outputDevices.Values)
{
playbackSession.WaveOut.Stop();
}
DisposeAll();
}
nicole castelPosted May 16, 2011, 10:27 AM
Mark Heath gave me a good code and the function works very well now but I still having the same problem with the button "Stop",Can you help me please?
nicole castelPosted May 16, 2011, 8:29 AM
Mark Heath gave me this code:
private void PlaySoundInDevice(int deviceNumber, string fileName)
{
WaveOut waveOut = new WaveOut();
waveOut.DeviceNumber = deviceNumber;
WaveFileReader waveReader = new WaveFileReader(fileName);
waveOut.Init(waveReader);
waveOut.Play();
}
public void playAllAvailableDevices()
{
int waveOutDevices = WaveOut.DeviceCount;
for (int n = 0; n < waveOutDevices; n++)
{
PlaySoundInDevice(n, fileName);
}
}
But it dont play sound in the three devices simultaneously,it's always waiting for only one device and I dont know why,the button "Stop" dont work when I use this code too.
FroglegPosted May 16, 2011, 2:53 AM
If your project is updated with buttons - upload new code rar
nicole castelPosted May 15, 2011, 5:19 PM
Thank you Frogleg,the code works very well,but I have another problem,did you remember the function "playAllAvailableDevices" which you sent to me? And this is its code:
public void playAllAvailableDevices()
{
//create a new class for each wav file & output etc.
WaveOut waveOut1 = new WaveOut();
WaveFileReader waveReader1 = new WaveFileReader(fileName);
WaveOut waveOut2 = new WaveOut();
WaveFileReader waveReader2 = new WaveFileReader(fileName);
WaveOut waveOut3 = new WaveOut();
WaveFileReader waveReader3 = new WaveFileReader(fileName);
switch (waveOutDevices)
{
case 1:
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();
break;
case 2:
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();
waveOut2.Init(waveReader2);
waveOut2.DeviceNumber = 1;
waveOut2.Play();
break;
case 3:
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();
waveOut2.Init(waveReader2);
waveOut2.DeviceNumber = 1;
waveOut2.Play();
waveOut3.Init(waveReader3);
waveOut3.DeviceNumber = 2;
waveOut3.Play();
break;
}}
Itplays sound only in one device,the first one it finds,I mean in this code the first device is number 0,so it play sound in it,but if you write device number 1 at first,it well play sound in it,as a conclusion it play sound only in one device,it dont works for all the devices at the same time.
FroglegPosted May 12, 2011, 8:00 PM
In form1.cs line 212
private void btnPlayBack1_Click(object sender, EventArgs e)
{
PlaybackForm playbackform1 = new PlaybackForm(1);
playbackform1.ShowDialog();
}
private void btnPlayBack2_Click(object sender, EventArgs e)
{
PlaybackForm playbackform2 = new PlaybackForm(2);
playbackform2.ShowDialog();
}
private void btnPlayBack3_Click(object sender, EventArgs e)
{
PlaybackForm playbackform3 = new PlaybackForm(3);
playbackform3.ShowDialog();
}
in playbackForm
int _deviceNum;
public PlaybackForm(int deviceNum)
{
InitializeComponent();
_deviceNum = deviceNum;
}
also in playbackForm line 459
private void CreateWaveOut()
{
CloseWaveOut();
int latency = (int)comboBoxLatency.SelectedItem;
//if (radioButtonWaveOut.Checked)
{
//WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?
WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
// WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
// WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();
WaveOut outputDevice = new WaveOut(callbackInfo);
outputDevice.DesiredLatency = latency;
outputDevice.DeviceNumber = _deviceNum;// add this<<<<<<<<<<<<<<<
waveOut = outputDevice;
}
}
nicole castelPosted May 12, 2011, 7:39 PM
FroglegPosted May 12, 2011, 7:21 PM
ie button 1 = sound card 1, button2 = sound card 2 and button3 = sound card 3
What sound will you be playing
nicole castelPosted May 12, 2011, 7:17 PM
FroglegPosted May 12, 2011, 7:11 PM
I don't understand what you want
nicole castelPosted May 12, 2011, 6:55 PM
FroglegPosted May 12, 2011, 6:03 PM
nicole castelPosted May 12, 2011, 5:31 PM
FroglegPosted May 12, 2011, 5:08 PM
nicole castelPosted May 12, 2011, 5:01 PM
Suthish NairPosted May 12, 2011, 1:11 PM