如何使用 AudioGraph Api 在后台播放音频? [英] How to play audio in background using AudioGraph Api?

查看:45
本文介绍了如何使用 AudioGraph Api 在后台播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows 10 Audio Graphs APi 播放曲目.我使用这个 API 的原因是我需要在不同的播放设备中播放曲目.因此,使用此 API 我可以轻松选择输出播放设备.但我现在面临的问题是,每当应用程序进入后台或我最小化应用程序曲目时,就会停止播放.

如何在使用 AudioGraph Api 时继续在后台播放音频?

解决方案

如何在使用 AudioGraph Api 时继续在后台播放音频?

您需要按照指南在 UWP 应用中启用后台音频,如果您只需要使用 AudioGraph 而不是 MediaPlayer,这也可以正常工作.

文档已改编来自 UWP 背景音频示例.

后台媒体播放功能是我们需要启用的正确功能.

支持两种场景:

<块引用>

  1. 长时间运行的播放列表:用户短暂地打开一个前台应用来选择和启动一个播放列表,之后用户期望播放列表以继续在后台播放.

  2. 使用任务切换器:用户短暂地打开一个前台应用程序开始播放音频,然后切换到另一个打开的应用程序使用任务切换器.用户希望音频继续播放背景.

我只是创建了一个示例来实现第一个场景并使用AudioGraph API播放音频文件,我们需要检查一些点:

  1. 启用背景媒体播放功能

  2. MediaPlayer设置MediaPlaybackList,我按照官方示例使用PlaybackService:

    MediaPlayer Player =>PlaybackService.Instance.Player;媒体播放列表播放列表{得到 { 返回 Player.Source 作为 MediaPlaybackList;}设置 { Player.Source = value;}}公共主页(){this.InitializeComponent();//处理页面加载事件已加载 += Scenario1_Loaded;}私有无效Scenario1_Loaded(对象发送者,RoutedEventArgs e){//创建一个新的播放列表如果(播放列表 == 空)PlaybackList = new MediaPlaybackList();}

请检查我完成的示例: LINK

I am using windows 10 Audio Graphs APi to play tracks. The reason I am using this API is I need to play tracks in different Playback devices. So, using this API I can easily choose output playback device. But the problem I am facing right now is that whenever application goes in background or I minimize the app track stops playing.

How to keep playing audio in background while using AudioGraph Api?

解决方案

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:

  1. 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.

  2. 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:

  1. Enable the Background Media Playback capability

  2. 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

这篇关于如何使用 AudioGraph Api 在后台播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆