i developed a simple app to play music.
When i run it on debug it works just fine. But after i published it as an application and install it on another machine, it won't run because the file paths is not recognized.
Here is my code:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\Users\chris\source\repos\Aplikasi Reminder\Aplikasi Reminder\Resources\mixkit-correct-answer-tone-2870.wav";
player.Play();
I tried to change the paths into:
player.SoundLocation = #"Aplikasi_reminder.Properties.Resources.mixkit-correct-answer-tone-2870.wav";
and it won't too..
Please can someone tell me how to fix that, thank you..
Include your files in the project`s resources folder by right-clicking on your project name>>properties>>Recources>>select audio>>drag your .wav file.
Then you can play the file from Memory Stream:
public void Play()
{
SoundPlayer player = new SoundPlayer();
player.Stream = YOUR_PROJECT_NAME.Properties.Resources.YOUR_FILE_NAME;
player.Play();
}
Related
I'm trying to save a mp3 file, into the device's music library folder.(I Want it to show up immediately as a playable song).
More clearly i'm looking for the path /storage/emulated/0/Music/, in each phone.Im not sure if this path changes, so i would rather not take the risk.
I have tried this paths:
System.Environment.SpecialFolder.CommonMusic -> Blank
System.Environment.SpecialFolder.MyMusic -> /data/user/0/App.App/files/Music
Android.OS.Environment.DirectoryMusic -> /Music
None of this offer, what i wanted.I tried writing to the /storage/emulated/0/Music/ which did the job, but i dont think this path is stable.
Anyone knows how can i get the music folder path programmatically ? Im coding in C# using xamarin.android.
I used this code to get access to my Music Folder
public void StartPlayer(String filePath)
{
string Mp3Path = Path.Combine("/sdcard/"+
Android.OS.Environment.DirectoryMusic,
filePath);
if (player == null)
{
player = new MediaPlayer();
}
player.Reset();
player.SetDataSource(Mp3Path);
player.Prepare();
player.Start();
}
The sdcard shortcut works great for me, hope it works for you too ^^
I want to create a SetupFile for my project. I have two sounds that I use... that's my current code (which works fine when I debug):
SoundPlayer clckSound = new SoundPlayer(#"C:\Users\My Name\Downloads\Button Push.wav");
SoundPlayer wonSound = new SoundPlayer(#"C:\Users\My Name\Downloads\Applause.wav");
Inside my Setup Project in the "Application Folder" I created another folder named "Assets" where I put the two sound files.
I tried it with the following code which didnt work.
SoundPlayer clckSound = new SoundPlayer(Application.StartupPath + "\\Assets\\Button Push.wav");
SoundPlayer wonSound = new SoundPlayer(Application.StartupPath + "\\Assets\\Applause.wav");
How do I specify the file path in my code so that the sounds get played properly after installation of the Setup file?
Thanks for your help!
Okay so i'm a noob, and spent 4 hours on Google already. So i would really appreciate some help. I'm trying to play a sound with a variable (got 100+ sounds) And i have done everything in this tutorial:
http://www.codeproject.com/Articles/17422/Embedding-and-Playing-WAV-Audio-Files-in-a-WinForm
This works:
System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer
(PROGRAM.Properties.Resources.audio);
soundPlayer.Play();
This Doesn't work:
string file = "PROGRAM.Properties.Resources.audio";
System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer
(file);
soundPlayer.Play();
ERROR: Please be sure a sound file exists at the specified location... System.Media:SoundPlayer.ValidateSoundFile(StringfileName)
How is it possible that the string doesn't work?
When you're using PROGRAM.Properties.Resources.audio in the first example, you're actually getting a reference to a stream (which gives you the embedded audio data). This is managed by the auto-generated code your .resx produces.
When you pass "PROGRAM.Properties.Resources.audio" as a string, the SoundPlayer interprets that as a file name, and then obviously can't find it.
If you want to manually get the audio stream from a resource file, try:
var stream = PROGRAM.Properties.Resources.ResourceManager.GetStream( "audio" );
var soundPlayer = new System.Media.SoundPlayer( stream );
soundPlayer.Play();
I have found a solution on here to play a sound file in WPF which I extracted into a method and call this method in another method. But the when the PlaySound() is called the sound doesn't play. Does anyone have any insight as to why this is happening? Also the sound files are marked as content, but changing to type resource didn't remedy the problem either?
My method to play a sound:
private void PlaySound()
{
Uri uri = new Uri(#"pack://application:,,,/Sounds/jabSound.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Then I call the method like this but its doesn't play the sound file, PlaySound();
You could also use a SoundPlayer
SoundPlayer player = new SoundPlayer(path);
player.Load();
player.Play();
Pretty self explanatory.
BONUS Here's how to have it loop through asynchronously.
bool soundFinished = true;
if (soundFinished)
{
soundFinished = false;
Task.Factory.StartNew(() => { player.PlaySync(); soundFinished = true; });
}
Opens a task to play the sound, waits until the sound is finished, then knows it is finished and plays again.
It turns out that MediaPlayer does not play the music files in embedded resources, quote from Matthew MacDonald book: Pro WPF 4.5 in C#. Chapter 26:
You supply the location of your file as a URI. Unfortunately, this URI doesn’t use the application pack syntax, so it’s not
possible to embed an audio file and play it using the MediaPlayer class. This
limitation is because the MediaPlayer class is built on functionality that’s not
native to WPF—instead, it’s provided by a distinct, unmanaged component of the
Windows Media Player.
Therefore, try setting the local path to the your music file:
private void PlaySound()
{
var uri = new Uri(#"your_local_path", UriKind.RelativeOrAbsolute);
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
For workaround, see this link:
Playing embedded audio files in WPF
In addition to #Anatoly's answer, I would suggest to listen to MediaFailed event to check for MediaPlayer failure (such as file not found due to wrong path to your .wav file). MediaPlayer doesn't throw exception if it fails to load the media file, it triggers MediaFailed event instead.
And if you're trying to use relative uri, remember that it means relative to your executable file location. In development phase, it is usually inside bin\debug folder. So path to your .wav file should be "../../Sounds/jabSound.wav".
Uri uri = new Uri("../../Sounds/jabSound.wav", UriKind.Relative);
var player = new MediaPlayer();
player.MediaFailed += (o, args) =>
{
//here you can get hint of what causes the failure
//from method parameter args
};
player.Open(uri);
player.Play();
I'm trying to play a .Wav file thats located in a folder inside my project.
The sound file is located on "Resource/Sounds/slot_roll_on.Wav"
The resource folder is a folder I created myself, in the root of the project.
This is the code I'm using to run the .wav file
Assembly a = Assembly.GetExecutingAssembly();
Stream s = a.GetManifestResourceStream("kisscam.g.resources.Resources.Sounds.slot_roll_on.wav");
SoundPlayer snd = new SoundPlayer(s);
snd.Play();
I couldn't get the sound to play, I keep getting the windows sound for sound not found.
Somewhere on stack overflow I found this code to find what the right assembly path should be.
Assembly a = Assembly.GetExecutingAssembly();
string[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
int i;
for (i = 0; i < resourceNames.Length; i++)
{
MessageBox.Show(resourceNames[i]);
}
It output these 2 paths
kisscam.Properties.Resources.resources
and
kissscam.g.resources
I tried using them both, but none of them works.
Anyone know what I'm doing wrong ?
Add your Wav file to resources by going to your Project Properties --> Resources Select Audio and Browse to the file. You will then be able to see it as part pf Propeties.Resources. It will add it to a Resources Folder where you can set it to embedded or leave it as is, which is set as content
Accessed like this
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer snd = new SoundPlayer( Properties.Resources.tada);
snd.Play();
}
If you want to add music in your program by playing your .wav file in projects. Then you have to add the .wav file like this.
using System.Media; // write down it at the top of the FORM
SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
Remember that you have to write the path of the file with forward slashes (/) format, don't use back slashes () during giving a path to the file, otherwise you will get an error
Currently I know two ways to do so, see below:
Use file path
First put the file in the root folder of the project, then no matter you run the program under Debug or Release mode, the file can both be accessed for sure. Next use the class SoundPlayer to paly it.
var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = Path.Combine(basePath, #"./../../Reminder.wav");
player.Load();
player.Play();
Use resource
Follow below animate, add "Exsiting file" to the project.
SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
player.Play();
The strength of this way compared to the other is:
Only the folder "Release" under the "bin" directory need to be copy when run the program.