Soundplayer for compiled application - c#

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....

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.

Related

Trouble pathing an audio file using WMPLib

I'm new to C# and pathing files have troubled me for a while.
The file structure to the audio files is as follows:
C:\Users\Username\Documents\Program\Form\WindowsFormApp\Audio Files\name.mp3
My visual studio forms are in:
C:\Users\Username\Documents\Program\Form\WindowsFormApp
I tried referencing audio files by using the following URL:
soundplayer.URL = #"Audio Files\name.mp3";
However, that did not work.
I then placed the folder Audio Files inside practically every other folder. It still did not work.
I tried:
soundplayer.URL = #"..\\Audio Files\name.mp3";
That did not work as well. I cannot simply do the entire path of the audio because it is different on each computer.
How do I path this correctly?
Well, in your case, it would be as follows
using System.IO;
using WMPLib;
WindowsMediaPlayer player;
private void button1_Click(object sender, EventArgs e)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"Audio Files\name.mp3");
player = new WindowsMediaPlayer();
FileInfo fileInfo = new FileInfo(path);
player.URL = fileInfo.Name;
player.controls.play();
}
Where
//This function gets the current route of your project and combines it with the subfolder path where your music file is
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"Audio Files\name.mp3");

How to play a sound that was imported into C# WPF project?

I have an issue with trying to play sound in my WPF application. When I reference the sound from its actual file location, like this,
private void playSound()
{
//location on the C: drive
SoundPlayer myNewSound = new SoundPlayer(#"C:\Users\...\sound.wav");
myNewSound.Load();
myNewSound.Play();
}
it works fine. However, I recently imported the same sound into my project, and when I try to do this,
private void playSound()
{
//location imported in the project
SoundPlayer myNewSound = new SoundPlayer(#"pack://application:,,,/sound.wav");
myNewSound.Load();
myNewSound.Play();
}
it produces an error and the sound won't play. How can I play the sound file imported into my project?
Easiest/shortest way for me is to change Build Action of added file to Resource, and then just do this:
SoundPlayer player = new SoundPlayer(Properties.Resources.sound_file);//sound_file is name of your actual file
player.Play();
You are using pack Uri as argument, but it needs either a Stream or a filepath .
As you have added the file to your project, so change its Build Action to Content , and Copy To Output Directory to Always.
using (FileStream stream = File.Open(#"bird.wav", FileMode.Open))
{
SoundPlayer myNewSound = new SoundPlayer(stream);
myNewSound.Load();
myNewSound.Play();
}
You can do it with reflection.
Set the property Build Action of the file to Embedded Resource.
You can then read it with:
var assembly = Assembly.GetExcetutingAssembly();
string name = "Namespace.Sound.wav";
using (Stream stream = assembly.GetManifestResourceStream(name))
{
SoundPlayer myNewSound = new SoundPlayer(stream);
myNewSound.Load();
myNewSound.Play();
}

AXWindowsMediaPlayer not finding embedded resource MP3

I have a embedded resource file (MP3 to be exact) that plays a short boop. I wanted it for easy transport of the file since I have a lot more of them that I'm looking to add in.
When I try to play it, WMP just says it cannot find the file.
I'm using axWindowsMediaPlayer1.URL = #"ultraelecguitar.Properties.Resources.pitchedbeep"; to access it. It is added in the resource manager, and marked as a embedded resource. When I run my program with the file in the directory, it works just fine. When I don't, it doesn't work at all.
If you save resource as temporary file then you could provide it's path as url.
static void Main(string[] args)
{
var wmp = new WMPLib.WindowsMediaPlayer();
wmp.URL = CreateTempFileFromResource("ConsoleApplication1.mp3.somefile.mp3");
Console.ReadKey();
}
private static string CreateTempFileFromResource(string resourceName)
{
var tempFilePath = Path.GetTempFileName() + Path.GetExtension(resourceName);
using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (var tempFileStream = new FileStream(tempFilePath, FileMode.Create))
{
resourceStream.CopyTo(tempFileStream);
}
return tempFilePath;
}

Getting a media file from resources

I have added a notification sound for some text message as a reference of the Main file of my project and try to make it work as follows
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("SignInSound.wav");
System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
player.Play();
I have the sound played, but it is not absolutely the one I added. Instead standard windows sound is played.
Any ideas?
Update
The issue is in getting the file from resources
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("SignInSound.wav");
Judging by the documentation, your resource stream is bad.
The Play method plays the sound using a new thread. If you call Play
before the .wav file has been loaded into memory, the .wav file will
be loaded before playback starts. You can use the LoadAsync or Load
method to load the .wav file to memory in advance. After a .wav file
is successfully loaded from a Stream or URL, future calls to playback
methods for the SoundPlayer will not need to reload the .wav file
until the path for the sound changes.
If the .wav file has not been
specified or it fails to load, the Play method will play the default
beep sound.
So the problem is that GetManifestResourceStream() is not doing what you think it's doing.
Solution (based on ResourceManager)
var thisType = this.GetType();
var assembly = thisType.Assembly;
var resourcePath = string.Format("{0}.{1}", assembly.GetName().Name, thisType.Name);
var resourceManager = new ResourceManager(resourcePath, assembly);
var resourceName = "SignInSound";
using ( Stream resourceStream = resourceManager.GetStream(resourceName) )
{
using ( SoundPlayer player = new SoundPlayer(resourceStream) )
{
player.PlaySync();
}
}
It seems that the System.Media.SoundPlayer class has a very limited amount of WAV formats it supports.
I have tried using the string path constructor, and it works with some .wav files, while it fails with others.
Here is some sample code. If you're using Windows 7, you can check it for yourself, just make a default new Windows Forms Application and add one button to it.
Notice how the code works for the "success" string, and throws an InvalidOperationException for the "fail" string.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Media.SoundPlayer player;
public Form1()
{
InitializeComponent();
string success = #"C:\Windows\Media\Windows Battery Critical.wav";
string fail = #"C:\Windows\Media\Sonata\Windows Battery Critical.wav";
player = new System.Media.SoundPlayer(success);
}
private void button1_Click(object sender, EventArgs e)
{
player.Play();
}
}
}
Notice that the file under "success" has a bit rate of 1411 kbps, while the other one has 160 kbps.
Try your code with a WAV file with a bit rate of 1411 kbps and let us know how it works.

How can I play a SWF file placed into Resources folder

I need to make a Windows application in which, at loading time, I need to play a Flash (.swf) file in WebBrowser. But I can play the Flash file directly from hard disk to WebBrowser control. Here I need to play the .swf file in the Resources folder and load it in WebBrowser control. Please help.
Thanks in advance.
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream("Namespace.Filename");
byte[] data = Properties.Resources.Filename;
file.Read(data, 0, data.Length);
Add the flash(.swf ) file as embedded resource.
use this method it works as u expect.
private void Form1_Shown(object sender, EventArgs e)
{
Stream sr = Assembly.GetExecutingAssembly().GetManifestResourceStream("namespace.file.swf");
if (sr == null) return;
var reader = new BufferedStream(sr);
string tempfile = Path.GetTempFileName() + ".swf";
var data = new byte[reader.Length];
reader.Read(data, 0, (int)reader.Length);
File.WriteAllBytes(tempfile, data);
webBrowser1.Url = new Uri(tempfile);
}
Hope it helps.

Categories