I have researched a lot on playing sounds for Windows Phone 8 devices and found multiple solutions but they don't quite match my case.
What I need : I'm writing an app (C#+XAML) that uses a file as background sound (must be active while navigating the whole app), and also to be able to play sound effects.
What are the issues :
For background sound I could use the BackgroundAudio Agent, but it doesn't meet my requirements because I want the sound to be played only in the background of my app, and to stop if my app closes or is not active.
For sound effects - I tried MediaElement which is okay, but I couldn't manage to make it somehow play while I am navigating the whole app. Media closes if I leave that page - I guess I could use this for the sound effects trick. Also, there's the SoundEffect which is not quite a good solution since it can play only .wav files... I could use it for sound effects only but not background sound (big sized files).
So, how should I proceed to play background sound (only inside my app) if I choose MediaElement/SoundEffect to play a sound effect in the app. I need a solution that would allow me to play 2 sounds at once (background and sound effect) and the background sound to be played only while the app runs (is active)...
So far I am confused and managed only to solve the sound effects issue.
Any suggestions are greatly appreciated.
The issue you are seeing with your MediaElement is that you are defining it to be part of the application page and it stops playing as soon as it disappears off of the Visual Tree (i.e. after OnNavigatedFrom).
If you define a MediaElement to be "visible" as part of the application frame, audio will keep playing while your app is active (you will need to handle deactivation events, naturally).
If you do this MediaElement should work for your "background audio".
Be aware you can only have one single active MediaElement playing media in your app, however you should be able to use SoundEffect for your sound effects.
Update:
To put your MediaElement in a frame, you will need to create a custom PhoneApplicationFrame class/XAML, add the MediaElement to that XAML, and refer to your custom frame in App.xaml.cs.
// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new MyCustomPhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
See this Dzone article for more about Frame/Page in Windows Phone.
In practice, MediaElement is has some gotchas like the visual tree requirement. There are ways to get around it, but they are not optimal. I would suggest scrapping using MediaElement and use XAudio2 instead. It is native so default usage would be in c++, but you can also use SharpDX to access this framework from C#.
The advantage of XAudio2 is that you would not need to worry about sound dropping out when navigating around since it is not dependent on the UI. Another advantage is you could have one SourceVoice for handling your background audio, and other SourceVoices for handling sound effect playback. This all fits well within the model of usage the framework was designed for.
Related
I have an application that uses MediElement to play videos and provides it with a custom video effects implemented in C++\CX via MediaElement.AddVideoEffect method.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.mediaelement.addvideoeffect
And it works just fine. But there is a bit of a problem, - When user hits the "Pause" button, MediaElement stops calling ProccessFrame method of the video effects I attach. Which leads to the situation, when user can tweak his effect whilst video is playing, but cannot do it on Pause.
So basically, I'm wondering if there is a workaround.
Thank you for the time and effort.
Kind regards.
I have built a Windows Phone application with a video player to show a logo animation at startup.
If I launch an external application (like Spotify) with background audio (for example a song) and then switch to my application, the song is stopped (probably because of my logo animation) even though my logo animation doesn't even have audio.
I used a MediaElement for the logo animation :
<MediaElement AutoPlay="False" Name="media" Source="Assets/video.mp4"/>
In the code behind I use media.play(); to start the logo animation.
Is there a way to avoid stopping the sound of other applications?
From the MSDN:
When a MediaElement control plays audio or video content, any
background sounds or media already playing are halted. The app
launches the playback experience when the user taps the control. Only
one MediaElement control can operate at a time.
What this means for you is that you need to redesign the logo to run via XAML animations or some other means besides MediaElement if you want background audio to function properly. Depending on where your animation is coming from, this might be simple for you or it might be outside your scope. You'll have to determine for yourself if the benefits of background audio (Pandora, Spotify, Podcasts, etc) outweighs the work required.
That being said, I've used a large number (probably 20% in my testing) of apps that cancel background audio every time you enter them, and it's extremely frustrating. I think most users would prefer you fixed your application so that background audio is not interrupted.
I have a mp3 file. I have a background sound file like rain sound or some fire sound. I want to play these two files parallel in the app as well as in background. So that it looks like a single music is playing a story with a background. Please provide any guidance to achieve this.
AFAIK there are four ways to play audio in WP: BackgroundAudioPlayer, MediaElement, SoundEffect, Microsoft Media Foundation. As for your question:
BackgroundAudioPlayer - it won't work, as there is only one Instance of BAP on the phone. So to play a different file, you have to change Track's Source, so it won't handle two at the same time.
MediaElement - it's also a bad idea (it won't also run with BAP simultanously), because as MSDN says:
When a MediaElement control plays audio or video content, any background sounds or media already playing are halted. The app launches the playback experience when the user taps the control. Only one MediaElement control can operate at a time.
you can try to play many sounds using SoundEffect (thougt as Documentation says it can be only a wave file), I won't post code here, because there are already many examples: one, two, three
as for Microsoft Media Foundation: Walkthrough, Programming Guide, Supported file formats
I am working on an application that plays a video using the MediaElement component. Now, I would like that if the user is idle, the lock screen appears, as configured by the user in the Settings of the device.
If I don't play a video, the lock screen indeed appears. But, when a video is playing no lock screen appears. I cannot find any information on this.
Currently I set the idle detection modes like this:
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled;
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
I am a bit lost now. The only solution I can think of is running a timer myself and stop the video playback after a certain time. (but there seems to be no API calls to receive the configured lock timeout.)
Any suggestions are welcome, thanks.
One workaround for you would be to enable running under the lockscreen. Then in the obscured even you could stop the mediaplayer. Not ideal, but it might serve your purposes.
I was wondering if anyone can give me some insight on what is the prefered best option for playing a video in a Silverlight MediaElement. Right now I am using a direct link to a .wmv file on an mms server but the location is inconsistent and occasionaly locks up my ui when I try to load the video.
When this happens the storyboard animations that take place that is supposed present the video freeze up until the video is loaded then the animation catches up and jumps to the end. Looks really bad.
I have the option for smooth streaming as well. Just curious if there is a prefered best way to present video?
At a minimum you should not be starting your storyboards until the MediaOpened event has fired. And yes, smooth streaming would probably present the best user experience.