Playing same sound many times C# WPF - c#

I'm trying to play the same overlapping sound whenever a button is pressed.
I tried with MediaElement and SoundPlayer, but the music stops and starts again. I need to create a new instance, but creating new MediaElement() and adding to the Stage didnĀ“t work too :-/
Thanks for your help

Check here: http://www.interact-sw.co.uk/iangblog/2008/01/25/wpf-concurrent-audio
Create multiple MediaPlayers in the CodeBehind.

I had no succeed with MediaPlayers, but it works:
MediaElement mp = new MediaElement();
soundPanel.Children.Add(mp);
mp.Source = new Uri(key_sound, UriKind.RelativeOrAbsolute);

Related

Kinect Hovering event handler

I am creating a WPF application using Kinect SDK 1.8
I want to press a button with hand.
private void playTeeth1Sound(object sender,RoutedEventArgs e)
{
System.Media.SoundPlayer teeth1_Sound = new System.Media.SoundPlayer(#"../../soundForKinect/1.wav");
teeth1_Sound.Play();
}
I don't see any Kinect specific code in your question, and I'm not sure what the questions is, but one thing to consider in the code snippet in your question is:
Every time this method is called (presumably when the button is pressed), it has to:
instantiate a new System.Media.SoundPlayer
load the sound file into teeth1_Sound
play the sound file with the Play() method
You might find there is a delay each time as the code re-instantiates teeth1_Sound and reloads the sound file? It might be easier to load all the sound files when the app is starting up, have them ready to Play() as soon as you need them?

C# windows forms How to play audio on webbrowser controller?

I prefer to use wavesurfer.js player in my web browser controller in C#.
The problem is wavesurfer isn't loading file.
Sample player is http://mypublic.kissr.com/
I am on windows forms applicaton. I tried with default webbrowser controller it didn't work.
then I tried with CefSharp browser controller. It still didn't work.
Any help is greatly appreciated.
Data View Waves Timer and Pointer location all these are important to me. that's why I need to use wavesurfer.
The link below, gives a very good tutorial, about playing mp3 files from a windows form with c#:
http://www.daniweb.com/software-development/csharp/threads/292695/playing-mp3-in-c
This link will lead you to a topic, which contains a lot information about how to play an mp3 song, using Windows forms. It also contains a lot of other projects, trying to achieve the same thing:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3dbfb9a3-4e14-41d1-afbb-1790420706fe
For example use this code for .mp3:
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "My MP3 file.mp3";
wplayer.Controls.Play();
Then only put the wplayer.Controls.Play(); in the Button_Click event.
For example use this code for .wav:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "Sound.wav";
player.Play();
Put the player.Play(); in the Button_Click event, and it will work.

Adding sound effects to WPF Surface application

I am trying to play a sound whenever an object is touched/moved in my WPF Surface application, to depict it has been selected or its movement. Here is what I tried but it doesn't seem to work.
SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
soundPlayerAction.Source = new Uri(#"C:\Resources\Selection.wav", UriKind.RelativeOrAbsolute);
EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.
eventTrigger.Actions.Add(soundPlayerAction);
Any feedback or ideas will be much appreciated.
Create a new folder in your project and rename it to SOUNDS then insert your sound files in it and try this:
SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
soundPlayerAction.Source = new Uri(#"SOUNDS\Selection.wav", UriKind.RelativeOrAbsolute);
EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.
eventTrigger.Actions.Add(soundPlayerAction);
Triggers.Add(eventTrigger); // Add this event trigger to Window.Triggers collection.
C:\Resources\Selection.wav is obviously not the correct path of the sound file.
You should create a folder named "Resources" in your Visual Studio project, and add the sound file to that folder. Then go to the properties of the sound file and set its Build Action to Resource and also set Copy to Output Directory to Copy aways or Copy if newer.
Now you can access the file by a relative Uri like this:
soundPlayerAction.Source = new Uri("Resources/Selection.wav", UriKind.Relative);
I came across this link https://www.youtube.com/watch?v=nF-HYoTurc8 and somewhat changed my code and the sound worked :D
In my PreviewTouchDown method, I added:
SoundPlayer soundPlayerAction = new SoundPlayer(TangramsTool.Properties.Resources.Selection);
soundPlayerAction.Play();

Can't play mp3 with MediaElement on WP7 emulator

I have an app where I have some prerecorded text to speech (As there is no default support for text to speech) then I want to play them like this
var mediaElement = new MediaElement();
mediaElement.Source = new Uri("sound.mp3", UriKind.Relative);
mediaElement.Position = new TimeSpan(0);
mediaElement.Play();
But nothing happens, do I HAVE to create a "real" control in my UI? I just want to play this sound when an event happens, I get no errors or nothing, nomatter if the mp3 is in the default folder or not.
The reason it's not playing is because you haven't added it to the Visual Tree. When you create a MediaElement programmatically, it needs to be added somewhere in the tree. You'll have to create a 'real control' in your UI, but it doesn't have to be seen.
var mediaElement = new MediaElement();
mediaElement.Source = new Uri("sound.mp3", UriKind.Relative);
mediaElement.Position = new TimeSpan(0);
LayoutRoot.Children.Add(mediaElement); //Add to visual tree
mediaElement.Play();
This depends on what happens in your code after mediaElement is declared. Currently, as soon as the method you declare it in ends, mediaElement will fall out of scope and become eligible for garbage collection.
You need to either:
Parent mediaElement to something, perhaps your UI
Make mediaElement a static field on the class
All that said, is playing through MediaElement supported in the Emulator?

Playing mp3 in WPF

MediaElement doesnt work for me in my WPF application.
mediaElement1.LoadedBehavior = MediaState.Manual;
mediaElement1.Source = new Uri(#"C:\Music\MySong.mp3", UriKind.RelativeOrAbsolute);
mediaElement1.Play();
When I do this in my Window1.xaml.cs file. Nothing happens. Atleast I cant hear anything. I have tried all kind of different things, but no sound.
In winforms:
axWindowsMediaPlayer1.URL = #"C:\Music\MySong.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
Works without any problems. Any simple solution or things to try?
Ok I solved it. WPF only support MediaElement if you have Windows Media Player 10 or above. I was running WMP9.
Though I'm also new in wpf,One thing you should notice about media element is that providing source in the XAML tag is not worth working.
you need to provide the source with the urikind like this
media.Source = new Uri(#"E:\Pehli_Baar_Mohabbat.mp3",UriKind.RelativeOrAbsolute);
put this line in window constructor
and set loadedbehavious=manual and then check.
mediaElement1.LoadedBehavior = MediaState.Manual;
---- edit to-----
mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;

Categories

Resources