473,408 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to insert a sound file in a Csharp program

Hello

I'm a biginner in Csharp and I want to read a sound file I found this code in the net and I tried to correct it:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.DirectX.AudioVideoPlayback;
  10. using System.Runtime.InteropServices;
  11. using System.Media;
  12.  
  13. namespace alarmee
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Audio Lecteur;
  18.         public bool lecture = false;
  19.        public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true, CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]
  24.        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);
  25.        [System.Flags]
  26.        public enum PlaySoundFlags : int
  27.        {
  28.            SND_SYNC = 0x0000,
  29.            SND_ASYNC = 0x0001,
  30.            SND_NODEFAULT = 0x0002,
  31.            SND_LOOP = 0x0008,
  32.            SND_NOSTOP = 0x0010,
  33.            SND_NOWAIT = 0x00002000,
  34.            SND_FILENAME = 0x00020000,
  35.            SND_RESOURCE = 0x00040004
  36.        }
  37.  
  38.         private void button4_Click(object sender, EventArgs e)
  39.         {
  40.         }
  41.         System.Media.SoundPlayer sp = new SoundPlayer();
  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.  
  45. //Dans ton constructeur :
  46.             sp.SoundLocation = "C:\\Sample\\alarme.wav";
  47. sp.LoadCompleted += new AsyncCompletedEventHandler(sp_LoadCompleted);
  48. sp.LoadAsync();
  49.         }
  50.  
  51. //Puis
  52. private void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e)
  53. {
  54.    sp.PlayLooping();
  55. }}}
  56.  
  57.  
Now I want to read the sound file witout precising its location because it's not practical to demand to a person who want to use my application to have the same file.wasv in the same location.
Hope you can understand what I wrote.
Thank you in advance.
Good day
Apr 22 '11 #1
2 2423
Add a .wav file in resource of the project and then use the following code:

Expand|Select|Wrap|Line Numbers
  1. SoundPlayer sp = new SoundPlayer(Properties.Resources.Default.soundfile);
  2.  
  3. sp.play();
  4.  
Apr 23 '11 #2
Hello
now i can read my sound file in any pc but I didn't use System.Resource.ResourceReader
This is my code:
Expand|Select|Wrap|Line Numbers
  1.  System.Media.SoundPlayer sp = new SoundPlayer();
  2.         private void button1_Click(object sender, EventArgs e)
  3.         {
  4.  
  5.             //Dans ton constructeur :          
  6.  
  7.             string chemin = "..//..//Sequences//alarme.wav";
  8.             sp.SoundLocation = chemin;
  9.             sp.LoadCompleted += new AsyncCompletedEventHandler(sp_LoadCompleted);
  10.             sp.LoadAsync();
  11.         }
  12.  
  13.         //Puis
  14.         private void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e)
  15.         {
  16.             sp.PlayLooping();
  17.         }
  18.  
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?
Apr 25 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paul Moore | last post by:
Does anyone know of a Python library which handles sound file formats (MP3, Ogg, FLAC are the three I'm nost interested in). I'd like something that can manipulate "metadata" - stuff like artist,...
3
by: Franklin P Patchey | last post by:
I don't know anything about Javascript, but a little about standard HTML. I have created a website and have set the homepage to automatically play a sound file on opening. What I would like is...
1
by: Vanessa | last post by:
Anyone know how to embed sound file to vb.net project, then play the sound file? I have searched the web for example... and download the example and when run, it works perfectly. But when i...
1
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be"...
6
by: martin1 | last post by:
Hi, All, The app retrieve sound.wav file from my loacal C drive, is there anyway the sound file can be stored in the app? then when building, it goes with app rather than sending sound file...
0
by: Chia Lee Lee | last post by:
Hi... I have a problem when i try to insert a sound file (.wav) to my project but system prompt me an error message as follow: “An unhandled exception of type 'System.NullReferenceException'...
2
by: hayz | last post by:
Flash sound file looping problems hello there I'm definitely a newb so please bare some patience. I have a flash sound file on the index page of a site i'm working on. First off i need the...
1
by: sumitkumarsingh | last post by:
hi, i am sumit kumar singh. I have some problem related to sound file.I want to store a sound file by C programm and C programm ,how canbe connect to the database. Give me...
1
by: Sheena777 | last post by:
I want to Play a specific sound file in my code when a certain event happens, but i don't know how. I can play the system sounds in my program and I know how to start the media player to play an mp3...
2
by: sailesh060 | last post by:
i want to how to insert a sound file in c# .net
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.