在 UWP 中在后台播放音频的最简单方法是什么? [英] What's the easiest way to play audio in background in UWP?

查看:28
本文介绍了在 UWP 中在后台播放音频的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的项目(一个 soundcloud 客户端),该应用程序可以很好地播放曲目,但在应用程序最小化时就不能播放了.我使用 MediaElement-Object 从 url 播放 mp3.当应用程序在后台时,我如何强制音乐继续播放音乐.或者什么是最简单的方法/最好的解释教程来实现这个.我搜索了很多好的答案,但我发现那些对我来说太好了 :D 什么意思,我不明白.

解决方案

要在后台播放音频,您必须在 Package.appxmanifest 中为后台任务做一个声明,启用音频并添加一个入口点,如 TestUWP.MainPage 页面.

此外,为了让用户能够轻松管理音频,您可以使用

如果您想启用更多控件,您可以使用可用的属性,例如.

systemControls.IsNextEnabled = true;

并且您必须在按钮开关中添加case.

case SystemMediaTransportControlsButton.Next://处理下一首歌曲休息;

I am working on my Project (a soundcloud client) and the app can play tracks just fine, but not when the app is minimized. I use the MediaElement-Object for playing the mp3 from the url. How can i force the music to continue playing the music, when the app is in the background. Or whats the easiest way/best explained tutorial to implement this. I searched alot for a good answer, but the ones, i found, was too good for me :D What means, that i didn't understand it.

解决方案

To play audio in the background you will have to do a Declaration in Package.appxmanifest for a Background Tasks, enable audio and add an entry point like TestUWP.MainPage page.

Also for the user to easily be able to manage the audio you can use SystemMediaTransportControls

Here is a basic setup with Play and Pause.

xaml

<MediaElement x:Name="mediaElement" Height="100" Width="100" AreTransportControlsEnabled="True"/>

C#

public MainPage()
{
    this.InitializeComponent();

    systemControls = SystemMediaTransportControls.GetForCurrentView();

    // Register to handle the following system transpot control buttons.
    systemControls.ButtonPressed += SystemControls_ButtonPressed;

    mediaElement.CurrentStateChanged += MediaElement_CurrentStateChanged;


    systemControls.IsPlayEnabled = true;
    systemControls.IsPauseEnabled = true;
}

private void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
    switch (mediaElement.CurrentState)
    {
        case MediaElementState.Playing:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
            break;
        case MediaElementState.Paused:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
            break;
        case MediaElementState.Stopped:
            systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
            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();
            break;
        case SystemMediaTransportControlsButton.Pause:
            PauseMedia();
            break;
        case SystemMediaTransportControlsButton.Stop:
            StopMedia();
            break;
        default:
            break;
    }
}

private async void StopMedia()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        mediaElement.Stop();
    });
}

async void PlayMedia()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        if (mediaElement.CurrentState == MediaElementState.Playing)
            mediaElement.Pause();
        else
            mediaElement.Play();
    });
}

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

Output

If you like to enable more controls you can do using the available properties for ex.

systemControls.IsNextEnabled = true;

and you have to add the case in the button switch.

case SystemMediaTransportControlsButton.Next:
                    //handle next song
                    break;

这篇关于在 UWP 中在后台播放音频的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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