Playing mp3 in WPF - c#

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;

Related

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.

Setting ContextMenu Style to 2013

I'm trying to change the default style of the contextmenu to 2013/2015 in my rehosted vs13 application.
The problem occurs in only one designer, everywhere else its the correct one. I've tried to override both the XAML code and the code behind, checked if something else was changing the style, but without anykind of result.
Is there even a way to change the default style? Am I overseeing something?
Okay, after some heavy research/try and error if finally found out what was wrong: I couldnt access the control I wanted to change the ordinary way, so I had to think outside of the box (and ask a collegue for help).
This is the code that works for me, its not pretty, but it deletes the 'standard'-style set by WPF.
var dv = wd.Context.Services.GetService<DesignerView>();
dv.MenuItemStyle = null;
dv.MenuSeparatorStyle = null;
dv.Resources[typeof(ContextMenu)] = new Style(typeof(ContextMenu));
Quick thanks to Glen Thomas for trying to help.

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 same sound many times C# WPF

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);

Categories

Resources