Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# audio
Hello,
 
I'm a beginner in C# and I want to read a sound file. I found this code in the net and I tried to correct it:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;
using System.Runtime.InteropServices;
using System.Media;
namespace alarmee
{
    public partial class Form1 : Form
    {
        public Audio Lecteur;
        public bool lecture = false;
       public Form1()
        {
            InitializeComponent();
        }
       [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]
       private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);
       [System.Flags]
       public enum PlaySoundFlags : int
       {
           SND_SYNC = 0x0000,
           SND_ASYNC = 0x0001,
           SND_NODEFAULT = 0x0002,
           SND_LOOP = 0x0008,
           SND_NOSTOP = 0x0010,
           SND_NOWAIT = 0x00002000,
           SND_FILENAME = 0x00020000,
           SND_RESOURCE = 0x00040004
       }
        private void button4_Click(object sender, EventArgs e)
        {
        }
        System.Media.SoundPlayer sp = new SoundPlayer();
        private void button1_Click(object sender, EventArgs e)
        {
//Dans ton constructeur :
            sp.SoundLocation = "C:\\Sample\\alarme.wav";
sp.LoadCompleted += new AsyncCompletedEventHandler(sp_LoadCompleted);
sp.LoadAsync();
        }
    
//Puis
private void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
   sp.PlayLooping();
}
}
}
 
Now I want to read the sound file without specifying its exact location because it's not practical to demand to a person who wants to use my application to have the same file.wav in the same location.
 
Hope you can understand what I wrote. Thank you in advance. Good day.
Posted 22-Apr-11 4:39am
Edited 22-Apr-11 7:06am
thatraja242.2K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can either distribute the wave file with your application setup. Byt this I mean, when you give the exe to your end-users, include the wave files too. This may be in a zip file, an msi, or some other kind of setup archive.
 
Or you can embed it as a resource in the exe. For more info, see:
How to embed and access resources by using Visual C#[^]
  Permalink  
v2
Comments
Nicole castel at 22-Apr-11 9:48am
   
How can I do that please,as I said I'm a beginner and I didn't understand what you mean by "distribute the wave file with your application setup" and by "embed it as a resource in the exe"
Nishant Sivakumar at 22-Apr-11 9:51am
   
Check my updated answer.
Nicole castel at 22-Apr-11 10:03am
   
Thank you for your help but in here I want to insert a sound file not an image,what should I change in the Sample code?
Nishant Sivakumar at 22-Apr-11 10:05am
   
Just replace the image file with the sound file. Everything else is the same. Of course you play the wave file using code that is different from the code that is used to render an image. But you already have code that shows how to play the wave file.
Nicole castel at 22-Apr-11 10:09am
   
Thank you very much Nishant :) I'll try the code.
Have a good day :)
Nishant Sivakumar at 22-Apr-11 10:20am
   
You're welcome. If you have related questions, feel free to start a new thread for those.
SAKryukov at 22-Apr-11 22:50pm
   
That is correct (my 5) but resource may (or may not, it depends on the purpose) be better.
So, please see my answer as well.
--SA
Nishant Sivakumar at 23-Apr-11 9:18am
   
Thank you, SA.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can also add a sound file in resource. It will be embedded with a file.
Create an new .resx resource file and use "add existing file". Look at the auto-generated C# file (created under your resource file node). Open it, locate a static class and a static property created; it will have a name close the the name of your sound file. To read the resource using this property, you can use System.Resources.ResourceReader.
 
[EDIT]
 
In response to the follow-up Question, here is the explanation of .resx files:
 
The .resx resource is one of the ways to add any data to a .NET assembly, not in the form of separate files but embedded in the executable module of the assembly, same file where the code is. Suppose you have three media files: sound.wav, bitmap.png and photo.jpg; file formats do not matter — it can be anything. On can simply add them to the project using "Add" —> "Existing item..." and specify what to do with them, usually copy to output directory.
 
Not so with .resx file. You first create a new resource: "Add" —> "New item..." —> "Visual C# items" —> "General" —> "Resource file" —> [give it some name]. When new resource is created and opened, locate its menu on top: "Add Resource" —> "Add Existing File". Add your three media files here. Each of these files will be added to the project; the newly created resource will contain reference to them. When you compile your code, each media file will be embedded in the executable module (by default not copied to output directory) and used as described above.
 
—SA
  Permalink  
v2
Comments
Nicole castel at 23-Apr-11 7:02am
   
I tried the code given by Nishant.In my case I should add a sound file but in "add items" I only find a bitmap file and a text file and there is no soun file :( And I really didnt understand your answer SAKryukov can you give me steps please.How can I create an .resx file and where? please explain
SAKryukov at 23-Apr-11 21:25pm
   
Steps and the explanation added. Using resources is one of the important skills needed do development, you want to know how to do such steps. You could find it out yourself by looking and VS menus and other controls.
--SA
Nishant Sivakumar at 23-Apr-11 9:19am
   
Good answer SA. Voted 5.
SAKryukov at 23-Apr-11 21:25pm
   
Thank you, Nishant.
--SA
Nicole castel at 25-Apr-11 6:11am
   
Hello
now i can read my sound file in any pc but I didn't use System.Resource.ResourceReader
This is my code:
 
System.Media.SoundPlayer sp = new SoundPlayer();
private void button1_Click(object sender, EventArgs e)
{
 
//Dans ton constructeur :
 
string chemin = "..//..//Sequences//alarme.wav";
sp.SoundLocation = chemin;
sp.LoadCompleted += new AsyncCompletedEventHandler(sp_LoadCompleted);
sp.LoadAsync();
}
 
//Puis
private void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
sp.PlayLooping();
}
 

I created a new folder: "Add" -> "New Folder" -> my folder name is Sequenes ->than I clicked on Sequences -> "Add" -> "Existing Item"-> I added alarme.wav
 
Did you think that it's a good idea?
SAKryukov at 25-Apr-11 10:17am
   
Well, what you do is adding WAV file. It will work, but you're not adding a resource, you're adding a separate file again. If this is a file which should go with your application, embedded resource is better. On your request, I've shown the steps to add resource instead. You just open a resource reader, and it has a stream property. Your System.Media.SoundPlayer has another constructor, SoundPlayer(Stream), which you use instead "of string fileName", almost no difference.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can also allow the user to choose the location of the wave file by using the Open File dialog control[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 23 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100