Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Please help with problem – multiple audio file playback(c#)

Please help with problem – multiple audio file playback(c#)

Scheduled Pinned Locked Moved C#
helpcsharp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    truly_pringled
    wrote on last edited by
    #1

    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

    M T 2 Replies Last reply
    0
    • T truly_pringled

      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

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • M mav northwind

        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

        T Offline
        T Offline
        truly_pringled
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • T truly_pringled

          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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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 the fdwSound 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

          1 Reply Last reply
          0
          • T truly_pringled

            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

            T Offline
            T Offline
            truly_pringled
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups