Playing an mp3 sound in a c# console Application - c#

I have a connect four game which is fully completed, but to learn new stuff im trying to add sounds for when the counter drops in the hole, I cant understand why i keep getting FileNotFound exceptions when i have imported my sounds file to be both in the solution and in the debug file.
this keep crashing:
(new SoundPlayer("Clonk.mp3")).Play();
I have also tried .PlaySync() which gave me the same result
thanks guys!

The SoundPlayer only supports certain .wav files. Try converting your .mp3 file into a .wav and see if this fixes the problem for you.
System.Media.SoundPlayer(string wav).Play();

Related

File is in use while deleting a video which is being played in html via video tag

I have a quick question.
I am using the "video" tag on my web page to display video from my server say "/videos/myvideo.mp4
Video is playing fine.
The issues is when I try to delete the video from some other code (server side)
via File.Delete(physical path) I am getting "File in use" error.
Is this a known issue?
How can I delete the video physically if someone is playing that video on his page at the same time?
I have had a similar problem when trying to rename a file, using C# on Windows 7. I do have a feeling that the problem is not as severe now as it was when I wrote that code. I don't know if some Windows 7 patch has fixed it.
Some things to try:
Ensure your OS is fully patched.
Ensure the file is Close'd before attempting the delete.
Set any variables that use the file to null.
Dispose any variables that use the file.
If you are absolutely sure nothing is holding the file open, you
might need to write a loop to keep trying the Delete, say every few
seconds. (Horrible fudge, it was the only thing that worked for me.)

Why is my .wav file not playing?

I want to create an application where i can play a .wav file after the input matches a certain number.
Now I've only come up across threads on how to check if the input is only numbers etc. but i couldn't find any solution yet for my problem.
Here's some code I'm currently using:
if(fortwenty_Tb.Text.Equals("420"))
{
player.Play();
}
The application works, but the sound isn't playing.
I've already checked if the .wav file was faulty, but it isn't since it does work after i tried playing it with a button controller.
First of all, you have to use debugger and breakpoints to ensure that Play() is called. Then you have to check volume of player, volume of your application in windows mixer, and, additionaly, file integrity and codec.

Windows Form Application : Publish issue

I'm very new to Visual Studio 2010. I decided to create a simple WFA with C#.
Everything work fine with Images and Audio playback. My intention is to create a standalone application and send it to my friend as a gift. Problem I facing now is that when I tried to publish the application, the Images / Audio is still using the same location in my PC. So it won't play nor display any images.
Any comment on this or guide for me? I did try search for solution but doesn't seems to have any luck.
This is the code that I used to play Audio :
SoundPlayer ply = new SoundPlayer(#"C:\Users\Liam619\Documents\Visual Studio 2010\Projects\BirthdayApp\BirthdayApp\Resources\BirthdaySong.wav");
If I remove or change the path, the Application will hit error for locating the Audio file.
There may be several solutions to your problem.
a) embed the sound into a resource. As a beginner, resources may be a bit tricky to get it right the first time. But I want to encourage you reading something about it. You'll need resources when you want to translate your first program.
b) create an installer which copies the sound file to the installation directory. Try InnoSetup. If you're a programmer, sooner or later, you'll need to create a Setup anyway. Always worth knowing how to do that.
In that case, you still need the path to the sound file, but if you install your EXE into the same path as the sound file, see getting the application's executable directory.
everything in the database whether images or audio refers to your own server database.you have to send the database too with the app and the correct version .NET framework needs to be installed on the target PC.

Play media file, while it is still downloading (Windows Phone)

I would like to know, if there's a way to play a media-file in Windows Phone, while this file is still being downloaded?
At the current time, I have an application with feature, that allows to download and play media files, when they are fully downloaded.
(I've created this feature, using article)
So, in brief, first, the media-file is downloaded from url, then I'm using mediaElement to play it:
mediaElement.source = isolatedStorageFileStream;
After that, I can use this file via my player UI.
However, I prefer to make this feature better - allow to start playing file, while the download is still in progress. In the end, this should look like that: start downloading file, contemporaneously start decrypting this file, contemporaneously allow to play decrypted part of file. Let's forget about decryption so far(I've already written a method for a downloaded file, it should be slightly changed then), the main problem is how to allow two parallel tasks: downloading and playing.
The first question is: "Does MediaElement only allow to set "source" property to only solid(finally downloaded) files or it can use this Stream, during downloading, as well"?
Because, I've found some info, that:
"The MediaElement does not supporting streaming at the moment. It
loads the entire file first before it actually plays it."
, though I'm not sure, that this one refers to Windows Phone, it sounds rather anxiously.
If this cannot be done, what else can I use to work with media-files? I'm not interested in downloading video, let's limit only with mp3 audio files.
Right now, the download process is implemented in this way:
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(link));
Then in OpenReadCompleted the download file stream is set as source of mediaFile.
mediaFile.SetSource(isolatedStorageFileStream);
After this I can use my media file.
I there a solution for my problem? I'm rather new at Windows Phone threading and streaming and don't know, how to implement such feature.
P.S. Of course, I tried to find a similar question, but failed. If you know one, give me a link, please.
P.P.S. Sorry for my English.
You can only stream it using BackgroundAudioPlayer.
you are trying to read a file that is in use. I don't think is possible.
However:
VideoRecorder works by using filesink and managed to capture, write and display at the same time. would suggest you have a look in that area
How to: Record Video in a Camera Application for Windows Phone
http://msdn.microsoft.com/en-us/library/hh394041(v=vs.92).aspx
FileSink
http://msdn.microsoft.com/en-us/library/system.windows.media.filesink(VS.95).aspx

How can I play compressed sound files in C# in a portable way?

Is there a portable, not patent-restricted way to play compressed sound files in C# / .Net? I want to play short "jingle" sounds on various events occuring in the program.
System.Media.SoundPlayer can handle only WAV, but those are typically to big to embed in a downloadable apllication. MP3 is protected with patents, so even if there was a fully managed decoder/player it wouldn't be free to redistribute. The best format available would seem to be OGG Vorbis, but I had no luck getting any C# Vorbis libraries to work (I managed to extract a raw PCM with csvorbis but I don't know how to play it afterwards).
I neither want to distribute any binaries with my application nor depend on P/Invoke, as the project should run at least on Windows and Linux. I'm fine with bundling .Net assemblies as long as they are license-compatible with GPL.
[this question is a follow up to a mailing list discussion on mono-dev mailing list a year ago]
I finally revisited this topic, and, using help from BrokenGlass on writing WAVE header, updated csvorbis. I've added an OggDecodeStream that can be passed to System.Media.SoundPlayer to simply play any (compatible) Ogg Vorbis stream. Example usage:
using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))
{
var player = new SoundPlayer(new OggDecodeStream(file));
player.PlaySync();
}
'Compatible' in this case means 'it worked when I tried it out'. The decoder is fully managed, works fine on Microsoft .Net - at the moment, there seems to be a regression in Mono's SoundPlayer that causes distortion.
Outdated:
System.Diagnostics.Process.Start("fullPath.mp3");
I am surprised but the method Dinah mentioned actually works. However, I was thinking about playing short "jingle" sounds on various events occurring in the program, I don't want to launch user's media player each time I need to do a 'ping!' sound.
As for the code project link - this is unfortunately only a P/Invoke wrapper.
I neither want to distribute any
binaries with my application nor
depend on P/Invoke, as the project
should run at least on Windows and
Linux. I'm fine with bundling .Net
assemblies as long as they are
license-compatible with GPL.
Unfortunatly its going to be impossible to avoid distributing binaries, or avoid P/Invoke. The .net class libraries use P/Invoke underneath anyway, the managed code has to communicate with the unmanage operating system API at some point, in order to do anything.
Converting the OGG file to PCM should be possible in Managed code, but because there is no Native Support for Audio in .net, you really have 3 options:
Call an external program to play the sound (as suggested earlier)
P/Invoke a C module to play the sound
P/Invoke the OS APIs to play the sound.
(4.) If you're only running this code on windows you could probably just use DirectShow.
P/Invoke can be used in a cross platform way
http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names
Once you have your PCM data (using a OGG C Lib or Managed Code, something like this http://www.robburke.net/mle/mp3sharp/ of course there are licencing issues with MP3), you will need a way to play it, unfortunatly .net does not provide any direct assess to your sound card or methods to play streaming audio. You could convert the ogg files to PCM at startup, and then use System.Media.SoundPlayer, to play the wav files generated. The current method Microsoft suggests uses P/Invoke to access Sound playing API in the OS http://msdn.microsoft.com/en-us/library/ms229685.aspx
A cross platform API to play PCM sound is OpenAL and you should be able to play (PCM) sound using the c# bindings for OpenAL at www.taoframework.com, you will unfortunatly need to copy a number of DLL and .so files with your application in order for it to work when distributed, but this is, as i've explained earlier unavoidable.
Calling something which is located in 'System.Diagnostics' to play a sound looks like a pretty bad idea to me. Here is what that function is meant for:
//
// Summary:
// Starts a process resource by specifying the name of a document or application
// file and associates the resource with a new System.Diagnostics.Process component.
//
// Parameters:
// fileName:
// The name of a document or application file to run in the process.
//
// Returns:
// A new System.Diagnostics.Process component that is associated with the process
// resource, or null, if no process resource is started (for example, if an
// existing process is reused).
//
// Exceptions:
// System.ComponentModel.Win32Exception:
// There was an error in opening the associated file.
//
// System.ObjectDisposedException:
// The process object has already been disposed.
//
// System.IO.FileNotFoundException:
// The PATH environment variable has a string containing quotes.
i think you should have a look a fmod, which is the mother of all audio api
please feel free to dream about http://www.fmod.org/index.php/download#FMODExProgrammersAPI
The XNA Audio APIs work well in .net/c# applications, and work beautifully for this application. Event-based triggering, along with concurent playback of multiple sounds. Exactly what you want. Oh, and compression as well.
Well, it depends on a patent-related laws in a given country, but there is no way to write a mp3 decoder without violating patents, as far as i know. I think the best cross-platform, open source solution for your problem is GStreamer. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. Here is a good starting point. Banshee project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). FMOD is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.
There is a pure C# vorbis decoder available that is open source:
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/
Not sure if this is still relevant. Simplest solution would be to use NAudio, which is a managed open source audio API written in C#. Another thing to try would be utilizing ffmpeg, and creating a process to ffplay.exe (the right binaries are under shared builds).
There is no way for you to do this without using something else for your play handling.
Using the System.Diagnostic will launch an external software and I doubt you want that, right? You just want X sound file to play in the background when Y happens in your program, right?
Voted up because it looks like an interesting question. :D

Categories