GetManifestResource problem
-
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks -
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? ThanksJustRonald wrote:
But I can't get it to work properly.
JustRonald wrote:
but i'm just a beginner and can't figure it out.
If you are getting some error message you need to post that along with the relevant code. Read the first post in the forum titled "How to get an answer to your question".
led mike
-
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? ThanksThere's an example here[^] The only problem I've found is you need to specify the file's location as "yournamespace.Resources.filename.ext" e.g. GetManifestResourceStream("My_App.Resources.new.wav"); Edit: You needto use Winmm.dll not Coredll.dll Improved solution/example below
Dave
modified on Monday, March 3, 2008 2:51 PM
-
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? ThanksHere is an example: Convert mp3 to exe[^]
#region signature my articles #endregion
-
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? ThanksI thought this was something interesting that I could maybe use so I've had a play and the example below works (ringin.wav from C:\Windows\Media added to resources and BuildAction property set to Embedded Resource).
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += new EventHandler(Form1_Load);
}void Form1\_Load(object sender, EventArgs e) { Stream wave = Assembly.GetExecutingAssembly(). GetManifestResourceStream( MethodBase.GetCurrentMethod(). DeclaringType.Namespace + ".Resources." + "ringin.wav"); Byte\[\] waveData = new Byte\[wave.Length\]; wave.Read(waveData, 0, (int)wave.Length); PlaySound(waveData, IntPtr.Zero, SoundFlags.SND\_ASYNC | SoundFlags.SND\_MEMORY); } enum SoundFlags { SND\_SYNC = 0x0000, SND\_ASYNC = 0x0001, SND\_MEMORY = 0x0004, SND\_NODEFAULT = 0x0002, SND\_LOOP = 0x0008, SND\_NOSTOP = 0x0010, SND\_NOWAIT = 0x00002000, SND\_FILENAME = 0x00020000, SND\_RESOURCE = 0x00040004 } \[DllImport("winmm.dll")\] static extern bool PlaySound(byte\[\] pszSound, IntPtr hmod, SoundFlags fdwSound); }
}
I've used
SoundFlags.SND_ASYNC
as it's in the load event but if you want it in FormClosing for example useSoundFlags.SND_SYNC
Dave
-
Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working
string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1);
If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? ThanksHi Ronald, there are some ways to solve your problem: 1. you can distribute your wav file in a specific subfolder of your application, e.g. Sounds. Then you just set the filename variable to the following value:
string filename = Application.StartupPath + @"Sounds\attention.wav";
Don't forget to includeusing System.Windows.Forms.
or type the namespace with the Application.StartupPath 2. You add the audio file as manifest into your exe. Then you need to call the following function:System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Program), "attention.wav")
Note that you get aSystem.IO.Stream
, so maybe need to save it in a temporary file and then call your PlaySound method (I don't know if your function supports a stream object as a parameter) So, these are the ways, I retrieved quickly from my mind at 23:17 :) (German time) PS: In the second solution I wrote typeof(Program). Program is a class generated by VisualStudio when you create a new project, so I used this here :) I hope I could help you_____________________________ The force of .NET is with me!