Designing a game for any laptop:
I want to play 2 sounds at the same time! A background music and a sound when pressing on a button!
Using System.Media won't allow me to play 2 sounds together. For background music, I used the following:
SoundPlayer sp = new SoundPlayer(#"..\..\bin\debug\tribal dance.wav");
sp.Play;
If I played another SoundPlayer, the previous one stops!
I want the background music to keep playing until the form is closed! And at the same time, if a button is clicked, another sound will be played with the music!
I also tried added the Windows Media Player to the toolbox and used it (allowing me 2 play 2 sounds at same time). That works fine but the path will give an error if I put it (#""..\..\bin\debug\tribal dance.wav");
and I need to put it that way in order to be read by any computer.
Any suggestions?
You CANNOT play two sounds at once using SoundPlayer.
SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.
There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)
The simplest option would be to depend on windows media player to play the audio for you.
Add a reference to "C:\Windows\System32\wmp.dll"
then use
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"..\..\bin\debug\tribal dance.wav";
Note: play starts immediately after setting the URL property
The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.
Using (WMPLib) Windows Media Player Library you can do this easily. Keep Your file in Debug/Sound folder and follow the following code. This will run 2 file simultaneously. To Add WMPLib in your project add it by NuGet Package Manager.
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
WMPLib.WindowsMediaPlayer wplayer2 = new WMPLib.WindowsMediaPlayer();
string play_string = "be.mp3";
wplayer.URL = #"SOUND/" + play_string;
wplayer.controls.play();
string play_string2 = "ba.mp3";
wplayer2.URL = #"SOUND/" + play_string2;
wplayer2.controls.play();
http://msdn.microsoft.com/en-us/library/dd562851%28v=VS.85%29.aspx this article may help
If you need absolute path to file you can use next code
string soundFile = Path.Combine(Directory.GetCurrentDirectory() + "file.wav");
Related
I want to play .wav sound in my application. I know how to do it. For example with a SoundPlayer class:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\path\sample.wav";
player.Load();
player.Play();
At home I have multiple Applications which can play sounds. However at job, I can't do it. Normally the computer can make sounds (youtube, Windows Media Player, etc.).
I investigated that my application does not appear in Volume Mixer.
In the end it was faulty wav file. I converted it to mp3 and then back to wav and now it works.
Is there any way I can play audio in my c#.NET application by using a soundcloud stream URI? I found a method to do this with windows 8 phones:
new AudioTrack(new Uri("[your soundcloud uri]", UriKind.Absolute), "[Track Name]", "[Artist]", "[Album]", null)
But I can't find a method to do this in desktop .NET framework. Could someone shine some light on this?
Since SoundCloud uses mp3 as streaming format, you may use WMPLib. First you need to add reference of it (Project -> References -> Add Reference -> COM -> Windows Media Player)
var wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "http://somewebsite.com/somedir/somefile.mp3";
wplayer.controls.play();
But on computers without Windows Media Player (Like Windows N, KN), you may need third-party libraries to play soundcloud streams. For example, NAudio.
ps: btw, the play of WMPLib is async, so you need to register to StateChange event to know whether the music is ended. See the MSDN Article.
Reference: MSDN Article
I'm trying to get one .wav file to play as ambient music though when another sound is played, it halts the ambient music and I've tried doing play-loop and tried the various sync, async , load.
I think I'm missing something here. Thank you in advance.
Check this question on SO and the suggestions made there. The basic problem is that SoundPlayer can only play one sound stream at a time - if you want to play multiple sounds concurrently you need a different solution.
It is possible to use Windows Media Player for the ambient sound, and play another sound with SoundPlayer at the same time:
// example begin
WMPLib.WindowsMediaPlayer mediaPlayer = new WMPLib.WindowsMediaPlayer();
mediaPlayer.URL = "<theAudioFile>";
mediaPlayer.settings.setMode("loop", true);
mediaPlayer.controls.play();
// example end
A reference to Windows Media Player assembly needs to be added to the solution as well.
I want in the win Application (written in C#), the sound (Wav Format) play as Background Sound, and mouse over Control play small wav sound File,
and when Click on Button, Stop Background Sound and ... .
Thanks For your guidance.
You could try look around in the System.Media namespace. There is a SoundPlayer which is able to play Wave files.
To play a wav file in a loop, you can use the following code:
string filename = #"C:\WINDOWS\Media\notify.wav";
System.Media.SoundPlayer player = new System.Media.SoundPlayer(filename);
player.PlayLooping();
To stop playing, you simple call Stop():
player.Stop();
Play around a bit, there's more if you need it.
you can use Windows Media Player:
reference C:\Windows\System32\wmp.dll in your project
to launch a wav file:
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayerClass();
wmp.URL = "[wav file path]";
//then control the player with :
wmp.controls.play(), stop(), ...
for the second wav file, do the same thing with another instance of WindowsMediaPlayer etc...
you can also use Managed DirectX : Managed DirectX Tutorial Part 2
Did you try to play each of them in a separate thread ?
I am looking for a way to interact with a standalone full version of Windows Media Player.
Mostly I need to know the Path of the currently played track.
The iTunes SDK makes this really easy but unfortunately there really isn't any way to do it with Windows Media Player, at least not in .Net(C#) without any heavy use of pinvoke, which I am not really comfortable with.
Thanks
Just to clearify: I don't want to embedded a new instance of Windows Media Player in my app, but instead control/read the "real" full version of Windows Media Player, started seperatly by the user
Just add a reference to wmp.dll (\windows\system32\wmp.dll)
using WMPLib;
And then you can instantiate a media player
var Player = new WindowsMediaPlayer();
// Load a playlist or file and then get the title
var title = Player.controls.currentItem.name;
See Creating the Windows Media Player Control Programmatically for more information
For remoting the Windows Media Player, you can use the IWMPRemoteMediaServices interface to control the stand alone Windows Media Player. And you should be able to read all the informations you want like title or filename from your WMP player object. Unfortunately there is no C# smaple code in the SDK included. You can get the files from here: http://d.hatena.ne.jp/punidama/20080227 Look for the file WmpRemote.zip
Originally it's from here: http://blogs.msdn.com/ericgu/archive/2005/06/22/431783.aspx
Then you have to cast to the WindowsMediaPlayer object:
RemotedWindowsMediaPlayer rm = new RemotedWindowsMediaPlayer();
WMPLib.WindowsMediaPlayer myPlayer = this.GetOcx() as WMPLib.WindowsMediaPlayer;
and there you go..
I had this https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbd43d7e-f3a6-4087-be06-df17e76b635d/windows-media-player-remoting-in-c?forum=clr in my bookmarks but have NOT tested it in anyway. Just a pointer in the right direction. It's nothing official and will require a bit of digging, but you should get a fairly simple wrapper (which will still use PInvoke under the hood - but you won't see it) around Windows Media Player.
Hope that helps.
Oh, I misunderstood. I thought you were talking about controlling the currently running Windows Media Player instance. If you are hosting Windows Media Player yourself then WMPLib is certainly the better solution.
The best info I have seen on interacting with Windows Media Player is this article written by Stephen Toub.
He lists a whole load of different ways to play dvr-ms files (doesn't really matter what format they are for this though). The bit that is possibly of interest to you is about using a Media Player ActiveX Control, which you can add to the visual Studio toolbox by right-clicking and adding the Windows Media Player ActiveX COM Control. You can then embed the player into your app, and access various properties of Media Player, like the url:
WMPplayer.URL = stringPathToFile;
This solution is possibly not what you want because it's starting a new instance of Media Player (as far as I know), however it might point you in the right direction.