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?
Related
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();
Using the MediaElement control and MediaCapture class, I am trying to record microphone input and play audio at the same time.
As soon as I start recording, whatever track is playing mutes. I don't think it stops, because it continues playing after the recording has stopped. I have added hooks into several events on the MediaElement and none are being fired. For example, CurrentStateChanged, MediaEnded, MediaFailed etc.
Recording code:
public async void InitializeMediaCapture()
{
_mediaCaptureManager = new MediaCapture();
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Other;
await _mediaCaptureManager.InitializeAsync(settings);
}
private async void CaptureAudio()
{
_recordStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
var recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
await _mediaCaptureManager.StartRecordToStorageFileAsync(recordProfile, this._recordStorageFile);
//audio playback stops on preceding line of code
}
I use .Play() on the MediaElement to play the audio, with the control in my XAML and the audio source set there.
<MediaElement x:Name="playbackElement"
Source="ms-appx:///Audio/Song.mp3"
AutoPlay="False" />
I have also tried playing the audio as BackgroundAudio, but that didn't work either. Any ideas?
1.Never set a MediaElement's Source in XAML, unless that XAML is on a page that you navigate to after asking the user for consent.
2.Check to see if background music is playing and then set the source (in code).
Note: If you set the source and then immediately call Play(), the Play() will have no affect since the MediaElement will still be in the "Opening" state, instead set "AutoPlay = true" (works from code)
Here's a Reference
I am building a win8 app which requires to launch the webcam for taking photos only.
I have seen the sample codes given in MSDN for Camera captures but I only want is onclick of CAPTURE Button the webcam should launched, take a pic and save it.
While in the sample codes they made the user to select option from the list box and on selectionchanged, the required function has been called. My problem is that I don't required any Listbox. Also they have used a class called SuspensionManager which I didn't understand. I am really confused.
Can somebody show me a way out?
Try this:
using Windows.Media.Capture;
var ui = new CameraCaptureUI();
ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
var bitmap = new BitmapImage();
bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
Photo.Source = bitmap;
}
Took from here
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);
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;