This project has moved and is read-only. For the latest updates, please go here.

Windows 8.1 background audio

Dec 8, 2014 at 12:43 PM
Hello.

In your samples there is not a Windows 8.1 universal app background audio sample. Is this because it will not work or that you have not had the time to build one yet?

Thanks
Coordinator
Dec 9, 2014 at 11:46 AM
Background audio for WP8.1 uses BackgroundMediaPlayer, which is only available for the phone. There is an app in the source code, but it leaks memory. The profiler doesn't show the leak in either the managed heap or the non-managed heap, but the total memory usage goes up until playback is terminated..
Dec 18, 2014 at 12:02 PM
Thanks, this code is not in any download ? what release is it for ?

I'm part of the xbox music pilot program so I might get them to look at it
Coordinator
Dec 19, 2014 at 2:11 PM
No, I have not put it in any of the binary downloads. There are still some pieces missing (like being able to set the URL), but the memory leak really needs to get sorted before it belongs in a binary build.

There are some recent changes (6762447a8d26) that make it more apparent by having the background task report memory usage to the foreground app. The first number it shows is from GC.GetTotalMemory(); the next two are from MemoryManager.AppMemoryUsage and MemoryManager.AppMemoryUsageLimit. The AppMemoryUsage usually grows steadily (maybe ~500bytes/s) until the background task is killed, but sometimes it doesn't budge for hours. In either case, the GC.GetTotalMemory() stays around 2MB (plus or minus a bit, depending on the stream's bitrate).

If you have any ideas or know someone that might, then by all means, have a look. I'm running out of ideas.
Apr 18, 2015 at 1:50 PM
Hi, sorry to bring up this old thread, but did you figure out what was casing the memory leaks on WP8.1? I am writing a media app and I am experiencing them too - as soon as I start playback in background, memory usage jumps by 6 MB to 25 MB, holds there for a while and then crashes - game over.

Anyway, any ideas are appreciated.
Coordinator
Apr 18, 2015 at 5:22 PM
I'm getting the impression that Microsoft's WP8.1 background audio implementation is at fault (at least when used from managed code).

Even Microsoft's own sample leaks memory. Doing more RPC seems to aggravate the problem. Try putting a for-loop in MyBackgroundAudioTask.cs' playList_TrackChanged() like so:
        void playList_TrackChanged(MyPlaylist sender, object args) 
        { 
            UpdateUVCOnNewTrack(); 
            ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, sender.CurrentTrackName); 
 
            if (foregroundAppState == ForegroundAppStatus.Active)
            for(var i = 0; i < 15; ++i)
            { 
                //Message channel that can be used to send messages to foreground 
                ValueSet message = new ValueSet(); 
                message.Add(Constants.Trackchanged, sender.CurrentTrackName); 
                BackgroundMediaPlayer.SendMessageToForeground(message); 
            } 
        } 
The 512MB emulator runs out of memory after a while. The time it takes varies from, say, 20 minutes to 4 hours, which suggests this is some kind of race causing the leak (interestingly, the rate of leakage seems inversely correlated with memory consumption).

I have also had trouble getting the playback status to report properly with WP8.1's background audio. It seems to like staying in the MediaPlayerState.Playing state after an error.

And I've never gotten the MediaStreamSample.Processed event to fire reliably on Win8.1 or WP8.1, foreground or background. If one counts both the number of samples submitted and the number of calls to the processed handler, the gap between the two numbers grows and grows--recent versions of phonesm use WinRtBufferPool which keeps such counters when run in debug mode.

In your application, is there anything you can do to cut down on your application's own memory consumption? It may not solve the problem on low memory devices, but if you can free up some memory, it would leave more for the operating system to hog. Does the profiler give any hints as to where memory is getting consumed?

There are others who are having problems as well. For example, search for "512" on the "Gooroovster App Page".

If anyone who has some answers would like to chime in...?
Apr 20, 2015 at 8:13 AM
I tried optimizing the code a bit (removing unused libraries mostly), but I am getting nowhere - the problem is the task takes 15 MB (!!!) when started - even in EMPTY project, with no references. This however applies only to phone - when run in emulator, empty task takes around 5 MB on start. Can you test on an actual device how much an empty task takes? I cannot believe there is a 10 MB difference between emulator and device, that's just ridiculous.
Coordinator
Apr 20, 2015 at 12:41 PM
Which version of the firmware do you have on that phone? What exactly do you mean by an "empty" project? Do you mean no references to your own code or one with no references to Microsoft's media stuff? How are you measuring the memory consumption?

With a Lumia 920 running 8.10.14219.341, a release build without the debugger attached starts at about 12.5MB and then drifts up from there. Doing the same with "Emulator 8.1 U1 WVGA 4 inch 512MB" (8.10.14141.167) start around 10.5MB and then drifts up. "Emulator 8.1 720P 4.7 inch" (8.10.12358.1191) also starts around 10.5MB. The numbers come from the MemoryManager.AppMemoryUsage and are only polled every few seconds, so the temporal accuracy is fairly low. My guess would be that creating the media stream source pulls in all the DLLs related to playing media (both the app's and Microsoft's DLLs). At least, the memory usage for phonesm's WP8.1 background audio jumps by 5-6MB when _mediaStreamFacade.CreateMediaStreamSourceAsync() is first called. Roughly another megabyte is consumed between when the SetMediaSource() is called and the MediaOpened event fires.
Apr 20, 2015 at 8:54 PM
I mean a project with WindowsRuntimeComponent, which when receives a message from the foreground start playback (and notifies memory usage) - that's is, no other code.

For debugging I also added a timer which runs every few seconds or so and again reports memory usage, I use MemoryManager.AppMemoryUsage

I can see that when started, the task takes 15 MB. After starting playback, it jumps to 20 MB. I have Lumia 925, WP 8.1 latest updates (including insider programme)

I creted a sample project, if you are interested... http://bezysoftware.net/files/App13.zip
Coordinator
Apr 21, 2015 at 12:15 PM
On a Lumia 920 with "Windows Phone 8.1 Update"/8.10.14219.341 (per Settings -> about, then "More info") running a release build without the debugger attached, App13's first reported values are 2.96MB, 3.88MB, 8.26MB, 8.35MB, 8.37MB, and 8.38MB. Skipping ahead about a minute it is 8.51MB and another minute later 8.70MB.

On "Emulator 8.1 WVGA 4 inch 512MB" ("Windows Phone 8.1"/8.10.12358.1191), it starts with 3.29MB, 4.42MB, 4.42MB, 7.42MB, 7.45MB, 7.47MB, 7.50MB. It keeps drifting up from there.

Windows Phone Developer Power Tools' Performance Monitor reports the same (in Private Bytes, although it uses 1024 * 1024 "MB" instead of 1000 * 1000 "MB").

Exactly which version is on that Lumia 925? Perhaps Microsoft has made background audio worse...? Could you try one of the emulators to see if another version of the operating system behaves differently?