Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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
Updated 22-Apr-11 6:06am
v3

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#[^]
 
Share this answer
 
v2
Comments
Nicole castel 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"
Nish Nishant 22-Apr-11 9:51am    
Check my updated answer.
Nicole castel 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?
Nish Nishant 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 22-Apr-11 10:09am    
Thank you very much Nishant :) I'll try the code.
Have a good day :)
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
 
Share this answer
 
v2
Comments
Nicole castel 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
Sergey Alexandrovich Kryukov 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
Nish Nishant 23-Apr-11 9:19am    
Good answer SA. Voted 5.
Sergey Alexandrovich Kryukov 23-Apr-11 21:25pm    
Thank you, Nishant.
--SA
Nicole castel 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?
You can also allow the user to choose the location of the wave file by using the Open File dialog control[^].
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900