Please help with problem – multiple audio file playback(c#)
-
I am having a problem with trying to get a series of wav files to play, I just want a simple button that when you press it, it will play a list of wav files, so far all that happen when I try and do this is it will play the last audio file stated in the code several times (the amount of other files I have tried to play.) Any help that can be given will be much appreciated Thank you RoB
-
I am having a problem with trying to get a series of wav files to play, I just want a simple button that when you press it, it will play a list of wav files, so far all that happen when I try and do this is it will play the last audio file stated in the code several times (the amount of other files I have tried to play.) Any help that can be given will be much appreciated Thank you RoB
Since you didn't write a single line of code it's impossible to tell you what you did wrong. Which environment are you using? .NET 1.1? 2.0? What are you using to play your wave files? There's no built-in class to do this, so what are you doing? Could it be that you're P/invoking
sndPlaySound
and specified the wrong flags? mav -
Since you didn't write a single line of code it's impossible to tell you what you did wrong. Which environment are you using? .NET 1.1? 2.0? What are you using to play your wave files? There's no built-in class to do this, so what are you doing? Could it be that you're P/invoking
sndPlaySound
and specified the wrong flags? mavThanks for your reply I am using .net 1.1 The bits of code I have been using are shown below:- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; using System.Runtime.InteropServices; private string currentSoundFileName; [DllImport("winmm.dll")] //load the sound libary public static extern long PlaySound(String lpszName, long hModule, long dwFlags); private Thread soundThread = null; public void PlaySoundInThread(string wavefile) { currentSoundFileName = wavefile; soundThread = new Thread(new ThreadStart(PlayASound)); soundThread.Start(); } public void PlayASound() { if (currentSoundFileName.Length > 0) { PlaySound(Application.StartupPath + "\\" +currentSoundFileName, 0, 0); } currentSoundFileName = ""; soundThread.Abort(); } Then I use the line below to play a wave file (i have stored the wav file in the debug directory) PlaySoundInThread("1.wav"); This works with one wav file but when I have tried to play another wave file after this, I just replete the line of code with a different wav file entered, and that's where my problem is, it does not work. Thanks RoB
-
Thanks for your reply I am using .net 1.1 The bits of code I have been using are shown below:- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; using System.Runtime.InteropServices; private string currentSoundFileName; [DllImport("winmm.dll")] //load the sound libary public static extern long PlaySound(String lpszName, long hModule, long dwFlags); private Thread soundThread = null; public void PlaySoundInThread(string wavefile) { currentSoundFileName = wavefile; soundThread = new Thread(new ThreadStart(PlayASound)); soundThread.Start(); } public void PlayASound() { if (currentSoundFileName.Length > 0) { PlaySound(Application.StartupPath + "\\" +currentSoundFileName, 0, 0); } currentSoundFileName = ""; soundThread.Abort(); } Then I use the line below to play a wave file (i have stored the wav file in the debug directory) PlaySoundInThread("1.wav"); This works with one wav file but when I have tried to play another wave file after this, I just replete the line of code with a different wav file entered, and that's where my problem is, it does not work. Thanks RoB
First, you've got the signature wrong. It's:
[DllImport("winmm.dll", SetLastError=true)]
static extern bool PlaySound(string pszSound, System.UIntPtr hmod, uint fdwSound);your trying to pass two
long
's (64-bit values!) in where the function is looking for two 32-bit numbers. Second, you don't need to launch another thread to play the sound. Starting a thread is a VERY expensive process, taking forever and a day to start one. If you look at the docs for PlaySound on MSDN or PInvoke.net[^], you'll see that if you pass in thefdwSound
parameter with the value 1, the function will start playing the sound asychronously, meaning it plays the sound without your code waiting for it to complete. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -
I am having a problem with trying to get a series of wav files to play, I just want a simple button that when you press it, it will play a list of wav files, so far all that happen when I try and do this is it will play the last audio file stated in the code several times (the amount of other files I have tried to play.) Any help that can be given will be much appreciated Thank you RoB
GOT IT WORKING! Thank you mav & dave. the code i used is below:- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; // for PlaySound() using Microsoft.Win32; // RegistryKey namespace WindowsApplication2 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { [DllImport("winmm.dll", SetLastError=true)] static extern bool PlaySound(string pszSound, System.UIntPtr hmod, uint fdwSound); [Flags] public enum SoundFlags : int { SND_SYNC = 0x0000, // play synchronously (default) SND_ASYNC = 0x0001, // play asynchronously SND_NODEFAULT = 0x0002, // silence (!default) if sound not found SND_MEMORY = 0x0004, // pszSound points to a memory file SND_LOOP = 0x0008, // loop the sound until next sndPlaySound SND_NOSTOP = 0x0010, // don't stop any currently playing sound SND_NOWAIT = 0x00002000, // don't wait if the driver is busy SND_ALIAS = 0x00010000, // name is a registry alias SND_ALIAS_ID = 0x00110000, // alias is a predefined id SND_FILENAME = 0x00020000, // name is file name SND_RESOURCE = 0x00040004 // name is resource name or atom } private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (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.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(48, 56); this.button1.Name = "button1"; this.button1.Size = new