Kyle Rickaby
Kyle Rickaby

Reputation: 117

Soundplayer for compiled application

How can I hardcode the code in order to pull the .wav file from the project?

As well as where should I put the .wav file?

The code I am currently using is:

        private void timer2_Tick(object sender, EventArgs e)
    {
        SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\dj.wav");
        simpleSound.Play();
    }

I just want the path @"c:\Windows\Media\dj.wav" to be for that content folder... So that when I deploy the application to another computer it comes with it....

Upvotes: 0

Views: 47

Answers (1)

user985916
user985916

Reputation:

Use GetManifestResourceStream.

var path = "MyApplicationNamespace.Content.dj.wav";
var assembly = Assembly.GetExecutingAssembly();

using( var soundStream = assembly.GetManifestResourceStream( path ) )
using( var soundPlayer = new SoundPlayer( soundStream ) )
{
    soundPlayer.Play();
}

The string passed to GetManifestResourceStream must be fully qualified with your application's root namespace and the directory tree the wave file resides in.

You also need to set the Build Action for the wave file to Embedded Resource in the properties window.

Upvotes: 1

Related Questions