How to keep playing audio in background while using AudioGraph Api?
You need to follow guidance to enable Background audio in UWP app, if you only need to use AudioGraph
and not the MediaPlayer
, this will also work well.
This document was adapted from the UWP Background Audio sample.
The Background Media Playback capability is the right one we need to enable.
There are two scenarios have been supported:
Long-running playlists: The user briefly brings up a foreground app to select and start a playlist, after which the user expects the
playlist to continue playing in the background.
Using task switcher: The user briefly brings up a foreground app to start playing audio, then switches to another open app using
the task switcher. The user expects the audio to continue playing in
the background.
I just create a sample to implement the first scenario and use AudioGraph API to play audio file, some points we need to check:
Enable the Background Media Playback capability
Set MediaPlaybackList
for MediaPlayer
, I followed the official sample to use PlaybackService:
MediaPlayer Player => PlaybackService.Instance.Player;
MediaPlaybackList PlaybackList
{
get { return Player.Source as MediaPlaybackList; }
set { Player.Source = value; }
}
public MainPage()
{
this.InitializeComponent();
// Handle page load events
Loaded += Scenario1_Loaded;
}
private void Scenario1_Loaded(object sender, RoutedEventArgs e)
{
// Create a new playback list
if (PlaybackList == null)
PlaybackList = new MediaPlaybackList();
}
Please check my completed sample: LINK
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…