Sound interaction between two applications - c#

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.

Related

How to handle the Keyboard music shortcuts (play/pause/previous/next) in UWP?

I'm developing a Music player app for UWP which can play music in background.
Note that I'm using old 2 layer music app pattern one for foreground and one for background music.
In the app i want to handle the Keyboard music controls like Play, Pause, Next and Previous. can anyone help me out with proper way of implementing the keyboard music controls?
The keyboard shortcuts such as Play, Pauseand so on you mean here are actually the System Media Transport Controls(SMTC).
can anyone help me out with proper way of implementing the keyboard music controls?
Actually, starting with Windows 10, version 1607, UWP apps that use the MediaPlayer class to play media are automatically integrated with the SMTC by default. Simply instantiate a new instance of MediaPlayer and assign a MediaSource, MediaPlaybackItem, or MediaPlaybackList to the player's Source property and the user will see your app name in the SMTC and can play, pause, and move through your playback lists by using the SMTC controls.
This is the recommended way of interacting with the SMTC for most scenarios.Details please reference Integrate with the System Media Transport Controls. There are a few scenarios where you may need to implement manual control of the SMTC. For this please reference Manual control of the System Media Transport Controls.
but i need it to work even in background.
According to the remark section of SystemMediaTransportControls class:
The system transport control allows a user to control a music application that is in the background as well as get and set the current information on which track is playing.
So that SMTC should also work for music in background.
For a sample please reference the official sample.

Audio stuttering while scrolling (C# WebBrowser)

I have a WinForms WebBrowser control in a WPF WindowsFormsHost, but I've verified the problem in straight WinForms as well as several different computers.
What happens is whenever a webpage is playing music and I start scrolling the page the audio freezes and repeats the same short sample over and over again. One of the most pronounced sites that exhibits this is Google Play Music.
Is there anything I can do to mitigate this problem?
Try enabling GPU rendering for your WebBrowser control via FEATURE_GPU_RENDERING feature. Here is how to do it, and there's a bunch of other features to play with.

Play two sounds parallel in background agent

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

Play Background Sound and Sound Effect in Windows Phone 8

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.

MediaElement seems to disable lock screen when playing a video

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.

Categories