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.
I want to load a song with the Song.FromUri() method, it works with some files, doesn't work with others. All i could find out is that if i try to play a song from a downloaded album, it won't play, but if i try to play one randomly google'd and downloaded, it plays well. I tried to place the "wrong" file to the Content folder and load it with Content.Load<Song>("filename") method, it perfectly plays, but i can't do it that way since it would be a player application, and the user has to choose own files. Any advice?
No, there is no exception, it simply does nothing. Anyway, i made another way.
My project is to build a simple game and for now I only made the Main Menu Form1 with 5 buttons. The buttons have a MouseClick and MouseEnter on them, and I have a background music track using a WMP method (using WMPLib and axWMPLib).
My problem is when I'm taking the bin/debug putting it on a rar file and giving it to my friends, they say they don't hear the sound. I made the project trough WMP version 11, so I asked them if their WMP version is 11 and they said yes. I have no idea why I hear the sounds on my computer and they don't.
I tried to give them the folders:
bin\Release
bin\Debug
x86\Release
x86\Debug
but they still said that they can't hear any sound from all of them.
EDIT
All my sounds are in a folder called "Sounds". I found some details and found out that you need to embeding those WMP sounds to "Resources".
So how can I do that, and how I call them when Form1 loads up. And no, this following code doesn't work:
BackGround.URL = Properties.Resources.Invincible;
It says I can't convert System.IO.UnmanagedMemoryStream to String.
Sincerely I don't recommend you to use WMP, so give a look to the SoundPlayer class.
However I think the problem is that you don't give the correct location to your files. So what you can do is to locate your files in your application folder, get its location and create the location of the music files.
So try:
string musicName = Application.StartupPath + "music.mp3";
Or if you have a Sounds folder in the application path use:
string musicName = Application.StartupPath + "\\Sounds\\music.mp3";
Else if you insert your music file in the application resources this:
BackGround.URL = Properties.Resources.Invincible;
didn't work because BackGround.URL is of type string while Properties.Resources.Invincible is the music file stream.
I don't know if using WMP you can set the stream from where it can play the file. Although the SoundPlayer class I linked previously contains a property from where you can set the input stream. You can do it in this way:
SoundPlayer mySoundPlayer = new SoundPlayer();
mySoundPlayer.Stream = Properties.Resources.Invincible;
mySoundPlayer.Load();
mySounPlayer.Play(); //plays the Properties.Resources.Invincible sound
I'm trying to index every music file in a folder and play them at random (without repeating) with the default application on my system. For example:
ogg - itunes
mp3 - WMP
mp4 - quicktime
Are there tutorials that will help me with this? Thanks!
Well, once you have a list of files, if you want to come up with a (pseudo)-random permutation of them, what you want is a shuffle algorithm, like Knuth-Fisher-Yates.
If you have a single filename and you want to open it with the default application, you can probably use:
System.Diagnostics.Process.Start(mp3FileName);
See also the MSDN documentation on Process.Start.
This will behave equivalently to just double-clicking on the files in whatever order the shuffle comes up with. Depending on your media player, that may be good enough. However, unless you somehow know how long each media file is, you won't know how long to wait before starting the next one, and I don't think there's any way to wait for the music player to finish playing the media file without having special-case code specific to each player.
You may be able to use the Verb property of the ProcessStartInfo object to select "Enqueue" or something similar, which may do exactly what you want. Again, MSDN has some additional information on ProcessStartInfo.Verb that may be helpful to you.
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