:
using System;
using System.Windows.Forms;
using NAudio.Wave;
using System.Runtime.InteropServices;
using System.Data.Odbc;
namespace PaGa
{
public partial class StreamingForm : Form
{
int device;
string fileName;
WaveOut wave = null;
private NAudio.Wave.WaveFileReader waveReader = null;
private NAudio.Wave.DirectSoundOut output = null;
public StreamingForm(int device)
{
InitializeComponent();
this.device = device;
}
public static int DeviceCount
{
get { return WaveNative.waveOutGetNumDevs(); }
}
private const string mmdll = "winmm.dll";
[DllImport(mmdll)]
public static extern int waveOutGetNumDevs();
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <summary>
/// The main entry point for the application.
/// </summary>
//[STAThread]
private PaGa.WaveOutPlayer m_Player;
private PaGa.WaveInRecorder m_Recorder;
private PaGa.FifoStream m_Fifo = new PaGa.FifoStream();
private byte[] m_PlayBuffer;
private byte[] m_RecBuffer;
private void Filler(IntPtr data, int size)
{
if (m_PlayBuffer == null || m_PlayBuffer.Length < size)
m_PlayBuffer = new byte[size];
if (m_Fifo.Length >= size)
m_Fifo.Read(m_PlayBuffer, 0, size);
else
for (int i = 0; i < m_PlayBuffer.Length; i++)
m_PlayBuffer[i] = 0;
System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size);
}
private void DataArrived(IntPtr data, int size)
{
if (m_RecBuffer == null || m_RecBuffer.Length < size)
m_RecBuffer = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);
m_Fifo.Write(m_RecBuffer, 0, m_RecBuffer.Length);
}
private void Stop()
{
if (m_Player != null)
try
{
m_Player.Dispose();
}
finally
{
m_Player = null;
}
if (m_Recorder != null)
try
{
m_Recorder.Dispose();
}
finally
{
m_Recorder = null;
}
m_Fifo.Flush(); // clear all pending data
}
private void Start()
{
//Stop();
//try
//{
PaGa.WaveFormatStream fmt = new PaGa.WaveFormatStream(44100, 16, 2);
m_Player = new PaGa.WaveOutPlayer(device, fmt, 16384, 3, new PaGa.BufferFillEventHandler(Filler));
m_Player = new PaGa.WaveOutPlayer(device, fmt, 16384, 3, new PaGa.BufferFillEventHandler(Filler));
m_Recorder = new PaGa.WaveInRecorder(-1, fmt, 16384, 3, new PaGa.BufferDoneEventHandler(DataArrived));
// wave = new WaveOut();
// stop previous sounds before starting
//}
//catch
//{
//Stop();
//throw;
//}
}
public void playChime(int device)
{
OdbcConnection cn = new OdbcConnection("DSN=cp1");
cn.Open();
OdbcCommand cmd1 = new OdbcCommand("select chemin from alarme where code_alarme=41", cn);
cmd1.Connection = cn;
fileName = cmd1.ExecuteScalar().ToString();
wave = new WaveOut();
disposeWave();
// DisposeAll(); // stop previous sounds before starting
waveReader = new NAudio.Wave.WaveFileReader(fileName);
var waveOut = new NAudio.Wave.WaveOut();
waveOut.DeviceNumber = device;
var output = waveOut;
output.Init(waveReader);
output.Play();
}
public void disposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
output.Stop();
output.Dispose();
output = null;
}
}
if (wave != null)
{
wave.Dispose();
wave = null;
}
}
private void StreamingForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Stop();
}
private void StartButton_Click(object sender, System.EventArgs e)
{
playChime(device);
Start();
}
private void StopButton_Click(object sender, System.EventArgs e)
{
Stop();
}
private void bttnexit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
And thanks very much for your help.