使用 MediaElement 播放背景音频的 Windows 应用商店应用 [英] Windows Store Apps playing Background Audio with MediaElement

查看:21
本文介绍了使用 MediaElement 播放背景音频的 Windows 应用商店应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Windows Phone 8.1 应用中,我使用的是 Media Element.即使用户离开应用程序,我也希望它继续播放音频.MediaElement 正在使用来自远程源 (.mp4) 文件的视频.我也在这里尝试了示例视频;http://go.microsoft.com/fwlink/p/?LinkID=272585

In my Windows Phone 8.1 App, I am using Media Element. I want it to continue to play Audio even if user navigated away from app. MediaElement is using video from remote source (.mp4) file. I also tried with sample video in here; http://go.microsoft.com/fwlink/p/?LinkID=272585

我按照 如何在背景(XAML),但无法使其工作.此示例特定于 Windows 8.1 而不是 Windows Phone.

I followed example in How to play audio in the background (XAML) but could not make it work. This example is specific to Windows 8.1 not Windows Phone.

当我按下 Windows 按钮时,MediaElement 正在播放视频剪辑,音频停止,当我回击时,它继续工作.

While MediaElement is playing Video Clip when I press Windows button audio stops and when I hit back it continues to work.

我的代码如下;

     <MediaElement x:Name="MediaElement"  VerticalAlignment="Top" 
        HorizontalAlignment="Stretch" 
            AudioCategory="BackgroundCapableMedia" 
            MediaEnded="MediaElement_MediaEnded" 
            MediaFailed="MediaElement_MediaFailed" 
    MediaOpened="MediaElement_MediaOpened" SeekCompleted="MediaElement_SeekCompleted"   
DownloadProgressChanged="MediaElement_OnDownloadProgressChanged"  BufferingProgressChanged="MediaElement_BufferingProgressChanged" 
AreTransportControlsEnabled="True" CurrentStateChanged="MediaElement_CurrentStateChanged"    />

我还在 Package.appxmanifest 中将音频定义为支持的任务类型

I have also defined Audio as Supported task Types in Package.appxmanifest

public VideoPlayer()
        {
            InitializeComponent();

            #region SystemMediaTransportControls
            // Hook up app to system transport controls.
            systemControls = SystemMediaTransportControls.GetForCurrentView();
            systemControls.ButtonPressed += SystemControls_ButtonPressed;
            // Register to handle the following system transpot control buttons.
            systemControls.IsPlayEnabled = true;
            systemControls.IsPauseEnabled = true;
            systemControls.IsStopEnabled = true;
            systemControls.IsEnabled = true;
            #endregion

            _navigationHelper = new NavigationHelper(this);
            _navigationHelper.LoadState += NavigationHelper_LoadState;
            _navigationHelper.SaveState += NavigationHelper_SaveState;
        }


void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
        {
            switch (args.Button)
            {
                case SystemMediaTransportControlsButton.Play:
                    PlayMedia();
                    break;
                case SystemMediaTransportControlsButton.Stop:
                    StopMedia();
                    break;
                case SystemMediaTransportControlsButton.Pause:
                    PauseMedia();
                    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, () =>
            {
                MediaElement.Play();
            });
        }

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



private void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debug.WriteLine("MediaElement.CurrentState: " + MediaElement.CurrentState);
            }
            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;
            }
        }

推荐答案

Windows Phone 不使用与 Windows 相同的背景音频机制,主要是因为低规格手机没有足够的资源来运行两个应用程序一次.

Windows Phone doesn't use the same mechanism for Background Audio as Windows does, primarily because low-spec phones don't have enough resources to run two apps at once.

相反,Windows Phone 使用专用的后台进程在后台播放音乐.请参阅BackgroundMediaPlayer 有关如何在 Windows 运行时手机应用中执行此操作的详细信息.

Instead, Windows Phone uses a dedicated background process to play music in the background. See the BackgroundMediaPlayer for more information on how to do this in a Windows Runtime phone app.

这篇关于使用 MediaElement 播放背景音频的 Windows 应用商店应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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