I have stored an audio file in the resources folder of my application but when I use the below path I get a file no found exception.
Can someone explain if this is the correct way to reference a file in resources or if I need to set the path differently?
This is the code that take the audio file as a parameter:
SoundPlayer player = new SoundPlayer("Resources/Audio/punchSound.wav");
player.Load();
player.Play();
You can use resource string:
var music = Properties.Resources.punchSound;
And then use SoundPlayer like this:
var player = new SoundPlayer(new MemoryStream(music));
Related
I have a file named "Alarm.wav" and I want to play it with media player, media player can't open a file with getting path, it only gets URI. I rally don't know how to make this file location to URI. Furthermore, I need it because software location can change. Here is the code:
MediaPlayer alarm = new MediaPlayer();
var uri = new Uri("file://Alarm.wav");
alarm.Open(uri);
alarm.Play();
But works if code be like this:
MediaPlayer alarm = new MediaPlayer();
var uri = new Uri("file://D:/apps/repos/BorderRaduis/BorderRaduis/bin/Debug/net5.0-windows/Alarm.wav");
alarm.Open(uri);
alarm.Play();
Relative path is what you are looking for.
So if you have a folder named e.g. Videos your path should be “videos/alarms.wav”
I am trying to play a sound file in my WPF application. Currently I have the following call:
private void PlaySound(string uriPath)
{
Uri uri = new Uri(#"pack://application:,,,/Media/movepoint.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Now if I specify Media/movepoint.wav as build action Content and load it as a relative or absolute file path it works fine, so I suspect this has something to do with the Pack URI, but I cannot for the life of me figure out what.
The objective is to store the file as a resource so that its not available in the output directory. I can provide either the WAV copy or the MP3 copy.
I tried this with an image file, which works the same as a sound file as far as the uri is concerned because it's just another resource. I used the code below which essentially matches what you have.
new Uri(#"pack://application:,,,/Resources/logo.png")
Make sure that your 'Media' folder is not nested in any other folder. If it is, you need to include that folder as well.
Using .NET Framework 4.0, VS2012.
This link gives a pretty good description of the whole "pack" scheme of things.
EDIT
More research on this topic seems to indicate that what you want to do might not be possible with audio or video files. The excerpt below is taken from the remarks section of this MSDN page.
Although you can declare an instance of this class in Extensible
Application Markup Language (XAML), you cannot load and play its media
without using code. To play media in XAML only, use a MediaElement.
Also, if you declare an instance in XAML, the only practical use is to
fill property element syntax for the Player property.
When distributing media with your application, you cannot use a media
file as a project resource. In your project file, you must instead set
the media type to Content and set CopyToOutputDirectory to
PreserveNewest or Always.
MediaPlayer can be used in two different modes, depending on what is
driving the player: independent mode or clock mode. In independent
mode, the MediaPlayer is analogous to an image and the media opened
through the Open method drives playback. In Clock mode, the
MediaPlayer can be thought of as a target for an animation, and thus
it will have corresponding Timeline and Clock entries in the Timing
tree which controls playback. For more information on media modes, see
the Multimedia Overview.
MediaPlayer is different from a MediaElement in that it is not a
control that can be added directly to the user interface (UI) of an
application. To display media loaded using MediaPlayer, a VideoDrawing
or DrawingContext must be used.
The following seems to work in .NET Framework 4.5:
var sri = Application.GetResourceStream(new Uri("pack://application:,,,/MyAssemblyName;component/Resources/CameraShutter.wav"));
if ((sri != null))
{
using (s == sri.Stream)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Load();
player.Play();
}
}
CameraShutter.wav is embedded as Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).
You can also load a Stream into the SoundPlayer if the .wav file is an Embedded Resource. Note that in this example the resources are in a folder called Resources that is in the root of the project, that is why it is written {0}.Resources.{1}.
//the wav filename
string file = "emergency_alarm_002.wav";
//get the current assembly
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
//load the embedded resource as a stream
var stream = assembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", assembly.GetName().Name, file));
//load the stream into the player
var player = new System.Media.SoundPlayer(stream);
//play the sound
player.Play();
This also seems to work and is maybe simpler to remember than that long line of pack or assembly stuff..
I opened the Resources.resx and dragged a sound file in there called aa_pickup.wav Then set the properties of it to Copy to Output Dir = Copy Always.
System.IO.Stream str = Properties.Resources.aa_pickup;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();
Now.. if I could only work out how to change the volume.
I am trying to add a .wav/.mp3 file to a project. I've added some pictures and they are working well, but I can't add any music. When I type the Uri like new Uri("file.mp3") and I put the file in the project directory it works, but this code below isn't (but it reads pictures O.o).
Uri jezus_uri = new Uri(#"pack://application:,,,/Resources/jesusrender.png");
Uri devil_uri = new Uri(#"pack://application:,,,/Resources/devilrender.jpg");
BitmapImage jesus_obraz = new BitmapImage(jezus_uri);
BitmapImage devil_obraz = new BitmapImage(devil_uri);
Uri muzyka = new Uri(#"pack://application:,,,/Resources/plig.wav");
MediaPlayer punch = new MediaPlayer();
punch.Open(muzyka);
punch.Play();
Build action is set to Resource of course. There is no error. Music is just not playing.
You cannot play sounds from a resource in a Media Player. You can only play it from a file. You may want to go with the approach of having your sound file deployed in the project directory and playing the file from the Media Player.
Alternatively, you can use the SoundPlayer to play sounds from a resource file.
SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav");
sndPing.Play();
Ref: https://msdn.microsoft.com/en-us/library/3w5b27z4%28v=vs.80%29.aspx
I have some problems with relative paths and reproduction of wav files. I have this simple code which works perfectly:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav";
player.Play();
I want somehow to play that file with relative path but I didn't have success with this:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"Data\Sounds\car.wav";
player.Play();
Thank you!
Is Data directory in the root directory of your application? Are you copying the directory contents as output?
If so, did you mean, Data\Sounds\car.wav?
Which, if running from Visual Studio would be in [projectroot]\[release]\bin\Data\Sounds\car.wav
If you don't see this directory in your bin folder, you'll need to ensure you're selecting all of the files you want copied to your output directory (which will copy the directory structure). You can do this by clicking on the file in your project and selecting the file as output.
Get the full path of your file with Path.GetFullPath("relativ/path")
You might be better off using absolute path after all. You can get the root path from the exe file, then append your relative path to it.
Like this:
// getting root path
string rootLocation = typeof(Program).Assembly.Location;
// appending sound location
string fullPathToSound = Path.Combine(rootLocation, #"Data\Sounds\car.wav");
player.SoundLocation = fullPathToSound;
//WindowsFormsApplication4.exe is name of name space this file name found in Debug file
//you should copy your "sound.wav" into your Debug file
string x = (Assembly.GetEntryAssembly().Location + "");
x = x.Replace("WindowsFormsApplication4.exe", "sound.wav");
SoundPlayer player1 = new SoundPlayer(x);
player1.Play();
This is worked for me
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\1.wav");
I am (after some strangeness with my resources file) able to play a .wav file in my project:
SoundPlayer player = new SoundPlayer();
player.Stream = Sounds.L1;
Player.Play();
This works fine but what I want to do is be able to concatenate a string together and then call the file. The .wav filename would be of the form "L" + int, with int being anywhere between 1 - 99 i.e. L1, L2...L99. All the different sound files are in a resx file title Sounds.
How would I be able to call them?
I am trying to use the ResXResourceSet resxSet = new ResXResourceSet("btc.Sounds") ResXResourceSet to load the resources file and then use the .getobject() as suggested. However, how do you specify the location of the embedded resource file? If I use a path as above I get an error as it is looking in my bin/debug folder, which is where the .exe is placed. If I explicityly specify the path I get an access error and one thing I am really curious about is the fact that the 'Sounds.resx' file is an embedded resource so it should 'know' where it is and shouldn't need any path... I have tried pretty much every permutation including using Assembly.GetExecutingAssembly().GetManifestResourceStream("btc_I_Cap.Sounds"), #"....\Sounds" #"....\Resources", #"btc.Sounds" "Sounds.resx" and so on and so far nothing. Can someone please put me out of my misery so that I can go 'oh yeah, that was obvious why didn't I think of that!'
Happy halloween.
Thanks.
Use a ResXResrouceSet to call the sound file Something like this:
using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
{
for (int i = 1; i <= 99; i ++)
{
SoundPlayer simpleSound = (SoundPlayer)resxSet.GetObject("L" + i.ToString());
simpleSound.Play();
}
}