BackgroundTask not firing in C# Application - c#

I am working with Windows Phone 8.1 and C#.
By following Microsoft's MSDN sample (available at: http://code.msdn.microsoft.com/windowsapps/BackgroundAudio-63bbc319) to create an audio player, I tried to integrate it into my project. However, while the code works fine in Microsoft's sample, it does not work as intended after I integrate it into my project.
I'll copy and paste the files are likely to contain the issue. Constants.cs, ApplicationSettingsHelper.cs and MyPlaylistManager.cs remain the same as in the sample (which you can look at by clicking the link above and hitting the "Browse code" tab).
When I run the following project, it throws the following error: "Background Audio Task didn't start in expected time". If I change the bool result = SererInitialized.WaitOne(2000); block of code (in StartBackgroundAudio() to bool result = true;, it simply prints "Background Audio Task initialized" in the console and then stops.
Page.xaml.cs
http://pastebin.com/L2zxzJKv
BackgroundAudio.cs
http://pastebin.com/zR6uzBf3
I don't understand what I am doing wrong. I ended up undoing all my changes and simply use the exact same code from the sample in the project. I added the extensions in the appmanifest.
I have created a sample project to help someone solve the problem and answer this question. You can find it at https://onedrive.live.com/redir?resid=ED2ED8732D8D457E!132&authkey=!AFBrZi5oI3k2zmU&ithint=file%2czip

The ZIP file you provided only has one project in the solution. The background audio task needs to be its own separate Windows Runtime Component project in the solution. Please look at the sample solution again and make sure that you have set up your solution correctly.
The reason why your background audio task needs to be in a separate project is because all background tasks need to be in a separate project, and background tasks will run in a separate process than your foreground app. This is so that if the OS suspends or terminates your app (i.e. you switched to another app) then your background audio task will continue to run and play audio even though your app has been suspended. You will then need to communicate between the background audio task and foreground process by passing messages between the processes (see SendMessageToForeground and SendMessageToBackground).
Apps will always be suspended by the OS when put in the background, therefore you need a separate lightweight background task process that has special permission to run in the background for the purpose of playing music.
These pages are quite helpful at explaining all of the concepts:
Overview: Background audio (Windows Phone Store apps)
How to play audio in the background (XAML)

Related

Xamarin iOS - SignalR stops receiving messages in background mode

Currently i am developing an app which needs to be capable of receiving and interacting based on realtime event notifications from all platforms. We are using SignalR to achieve so. I am using Xamarin.Forms as this is a shared project.
Right now everything is working fine especially android is perfect. No matter application is in background or foreground its receiving messages from SignalR and playing sound and vibrates as expected. In iOS 10.3 simulator also its working as expected. Untill yesterday when i realized in real device when application goes in background mode it pauses everything and when its back in foreground it resumes as its supposed to be doing and as if the notification was sent just now. So i did some research and thought i would perform a long running task (in my case a forever loop with a 10seconds interval) in the background thread and in that way i was able to receive signalr messages (which i can see on the debugger) and vibration was playing while being in background. But after a certain time i am guessing within 10 mins iOS is clearing up its memory and when i open the app it seems reloaded because it shows the login screen instead of where i left the app.
Now question is:
1. How can i keep receiving SignalR events while being in background?
2. How can i stop iOS to reload my app due to being in background for a long time?
3. The app has to be Appstore publishable as in appstore must not reject it due to some dodgy works being done in the background like playing silent audio forever or using VoIP for background since this is not a VoIP app.
4. Also soon i am gonna be implementing location update in background mode so is this gonna keep the whole app alive in background? And behave like i am expecting?
As i am currently travelling i can not provide any code snippet but any questions please feel free to ask.
Thanks for your help and reading this boring article.
Just in case if someone is facing similar issue.
Apple doesnt allow to execute your app unless it falls in certain category Look at table 3-1. Fortunately, I had a valid reason to use continuous location update in foreground and background therefore as soon as I implemented location update event all my problems were solved immediately.
How to implement location update? Location DependencyService example for Xamarin.Forms
Hope that will help.

Background audio stops playing when minimize windows 10 universal

I created an radio app for windows 10 using mediaElement for audio output with AudioCategory = BackgroundCapableMedia but whenever I minimize the app on desktop or mobile the audio stops playing. So how do I keep the audio alive after I minimize? And all examples I find is for 8.1 silverlight applications
You need to implement a Background Task to keep audio alive when your application is minimized.
You can find a sample which targets Windows 10 on github. The project is splitted in 3 parts (a/ your main application b/ the background task and c/ code shared between the two first projects)
Background audio sample
In addition, you can find documentation associated to this sample on MSDN.
Background Audio
Please let me know if you need additional information.

BackgroundAudioPlayer doesn't start playing

Windows Phone 8 app.
We're using AudioStreamingAgent/Mp3MediaStreamSource to play MP3 files which could be cached locally or loaded from web.
When file is downloaded we are still using AudioStreamingAgent /Mp3MediaStreamSource to ensure we will be able to precache next file while playing current.
Yes, we could use just WP8 built-in player for cached files, but we will not be able to precache next file in this case.
Problem:
After several tracks, BackgroundAudioPlayer doesn't start playing anymore.
AudioStreamingAgent.OnBeginStreaming is executed
Сode to create MediaStreamSource is executed
AudioStreamer.SetSource method is executed with new MediaStreamSource instance
PlayStateChanged is fired in background process with PlayState.TrackReady and player.Play() is called in this case.
After all, I see the track is shown in UVC, but it's in state "Paused". Pressing "Play" in UVC calls OnUserAction in background which calls player.Play(). Nothing changes.
Picture of "stuck state"
Sample with problem reproduction, VS 2012 (sample is based on Background Audio Streamer Sample)
Steps to reproduce with sample code:
Install app
Press button "Preload tracks". It copies mp3 files from XAP to IsolatedStorage.
Press "Track 1" button. After track starts playing, press button "Track 2". Then, after track 2 starts playing, press button "Track 3".
Keep doing step 3 until after pressing "Track *" button track doesn't start playing.
Last track selected will be shown as an active track both in UVC and above progress bar on the page (see picture)
Pressing other track buttons in most cases doesn't make any difference.
The problem is reproduced both on device and on emulator, both on WP8 and WP8.1 developer preview.
Update: Running out of memory is not the case. Reproducing the problem using sample code and looking at the backgroundlog.xml file shows that sample uses only 6 Mb out of 20 Mb allowed Last column on this picture is "Memory usage|Max allowed memory usage"
In my experience with this, my suggestion would be to port and start working with WPA8.1 where the limitations with playing background audio go away.
In developing a music player app for WP and trying to cache music while using a mediastreamsource, I finally came to the conclusion that it might not be possible. It seems that you have a set amount of time or some other variable before the OS stomps on any tasks or background type work being done.
I wrote this up for a MS evangelist:
https://drive.google.com/file/d/0By_e5C-QWr9Ka3RmbVBlWVF3R1E/edit?usp=sharing
Here are some other SO's I favored when trying to solve this:
BackgroundAudioPlayer is 'Playing' but not calling GetSampleAsync()
WP7 background audio resources are no longer available
HttpWebRequest from AudioPlayerAgent
AudioPlayerAgent, timer and webservice
WP7 Mp3MediaStreamSource demo not working

MediaPlayer vs BackgroundAudioPlayer

I'm developing an Windows Phone application, that plays/streams music in background.
To accomplish that, I tried to use both Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer and Microsoft.Xna.Framework.Media.MediaPlayer classes.
In both cases everything worked just fine, so I was wondering, what are advantages/disadvantages of these methods, because now I can't decide which one is better.
The only thing that I don't like in BackgroundAudioPlayer, is that I need to create separate project for audio agent, and worry about transferring track info via IsolatedStorage. But since I've done that already. It's not a problem.
So are there any other differences between those two classes?
It depends on your program. If you want to play your background music only while using your program, you should use MediaPlayer. Otherwise you have to take care of the BackgroundAudioPlayer in any situation. Also this will annoy users, who also want to listen to their own music while using your application.
Otherwise, if your background music is the main purpose of the program, the user would like to start your music and switch to other programs. Then it keeps playing and you can control the music in the volume bar. That's why you have to create a background task for BackgroundAudioPlayer. The main application will stop in both ways when switching to other programs.
In short: game background music should never play after leaving the game (use MediaPlayer) - but I never want to watch my music player counting the time, while listening to my music library (use BackgroundAudioPlayer).
I believe MediaPlayer requires the application to be in the foreground. BackgroundAudioPlayer will let the user start a track, exit your application, and the music will continue to play and the agent can respond to the UVC.
Make sure you use a Mutex when you communicate between the foreground app and the background audio agent.

How to Play Background Music using MonoTouch While App Running

What would be the easiest way to have a music file included in the App as looped background music while the app is running and the music pauses if the app is suspended and starts up again when the app is brought back to foreground. Don't care about playing for the devices iTunes/Music catalog just one or more included music files within the App bundle.
I don't know if it's the easiest way, but there is some sample code in TweetStation to play audio in your app.

Categories

Resources