3

Hello I'm building a UWP radio app where the user can select different radio stations and podcasts.

I have made my design so i have implemented a splitview where when a user starts playing audio a MediaElement will show up in the buttom of the screen, and then bacause of the splitview stay there no matter what page of the app the user navigates to.

In order to have the audio play in the background i have set

<MediaElement x:Name="BGMediaElement"
              AudioCategory="BackgroundCapableMedia"

and then i have the following code to control it.

systemControls = SystemMediaTransportControls.GetForCurrentView();
        systemControls.ButtonPressed += SystemControls_ButtonPressed;
        systemControls.IsEnabled = true;
        systemControls.IsPlayEnabled = true;
        systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
        systemControls.IsPauseEnabled = true;

private void BGMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
    {
        switch (BGMediaElement.CurrentState)
        {
            case MediaElementState.Playing:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
                //playButton.Icon = new SymbolIcon(Symbol.Stop);
                break;

            case MediaElementState.Paused:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
                break;

            case MediaElementState.Stopped:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
                //playButton.Icon = new SymbolIcon(Symbol.Play);
                break;

            case MediaElementState.Closed:
                systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
                break;

            default:
                break;
        }
    }

void SystemControls_ButtonPressed(SystemMediaTransportControls sender,
SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Play:
                PlayMedia();
                //playButton.Icon = new SymbolIcon(Symbol.Pause);
                break;
            case SystemMediaTransportControlsButton.Pause:
                PauseMedia();
                //playButton.Icon = new SymbolIcon(Symbol.Play);
                break;
            default:
                break;
        }
    }

    async void PlayMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            BGMediaElement.Play();
        });
    }

    async void PauseMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            BGMediaElement.Pause();
        });
    }

Now my problem is that this works flawless on the pc and when debugging on mobile, but as soon as I actually run the app on the phone without debugging it the background audio stops working.

I have set the declaration for background task audio in the appxmanifest.

I had a look at the UWP samples for background audio and they implement a separate BackgroundAudioTask. That is just alot of extra work, and the sample has alot of things like playlists and stuff that i dont need for a radio/podcast app, especially when the code I have works flawless on PC, and I want the splitview mediaelemant at the same time.

Hope someone has an easy fix. :-)

Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72
Kasper S Mathiesen
  • 669
  • 1
  • 7
  • 17
  • take a look at [Guidelines for app suspend and resume](https://msdn.microsoft.com/en-us/library/windows/apps/hh465088.aspx) and [App lifecycle](https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle) – Nikola.Lukovic Apr 04 '16 at 10:57
  • I'm not sure how you implement your background audio. But an app performing background playback consists of two processes. The first process is the main app, which contains the app UI and client logic, running in the foreground. The second process is the background playback task, which implements IBackgroundTask like all UWP app background tasks. For more info, see [Background audio architecture](https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/background-audio#background-audio-architecture). However I didn't see the background playback task in your code. – Jay Zuo Apr 05 '16 at 03:13
  • You may refer to [The Basics of Background Audio](https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/) to implement the background audio. – Jay Zuo Apr 05 '16 at 03:14

0 Answers0