如何在后台播放 MediaElement.? [英] How to play MediaElement in background.?

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

问题描述

我正在开发一个通过流媒体播放在线广播的应用程序.我使用过 MediaElement.但问题是播放器不在后台播放.我的意思是,只要我点击手机上的开始"或返回"按钮,流媒体和音频就会停止.我还没有在任何设备上测试过,所以如果它确实发生在模拟器而不是设备上,请通知我.这是我的代码..

I am developing an application to play online radio via streaming. I have used MediaElement. But the problem is the player doesn't play in background. I mean as soon as I click on "start" or "back" button on the phone, the streaming as well as the audio stops. I have not tested it on any device, so please inform me if it does happen in simulator but not device. Here is my code..

private void Play()
    {
        if (mediaElement == null || mediaElement.CurrentState != MediaElementState.Playing)
        {
            if (SystemTray.ProgressIndicator == null)
                SystemTray.ProgressIndicator = new ProgressIndicator();

            SystemTray.ProgressIndicator.IsIndeterminate = true;
            SystemTray.ProgressIndicator.IsVisible = true;
            SystemTray.ProgressIndicator.Text = "Connecting to *********...";

            mediaStream = new ********.RadioStream(uri);


            mediaStream.StreamSetupComplete += (o, e) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    if (mediaElement != null)
                    {
                        LayoutRoot.Children.Remove(mediaElement);
                    }
                    mediaElement = new MediaElement();
                    mediaElement.Volume = 1.0;
                    LayoutRoot.Children.Add(mediaElement);
                    mediaElement.SetSource(mediaStream);

                    SystemTray.ProgressIndicator.IsVisible = false;
                });
            };
        }
    }

我想知道启用它在后台播放的步骤.至少当用户按下开始"按钮时,音频流不应停止.

I want to know the steps to enable this to play in background. Atleast when the user presses "start" button, the audio streaming should not stop.

我还有一个问题是我添加了一个 ApplicationBarMenu,其中有一个退出"按钮.一旦用户点击此按钮,流媒体就会停止,应用程序应该自行关闭.我无法以编程方式关闭应用程序.代码如下..

Also one more problem I have is I have added an ApplicationBarMenu in which I have an "Exit" button. As soon as the user clicks this button the streaming should stop and application should close itself. I am unable to close the application programmatically. Code is give below..

 void exit_Click(object sender, EventArgs e)
    {
        if (playing)
        {
            MessageBoxResult Choice;
            Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel);
            if (Choice == MessageBoxResult.OK)
            {
                ImageBrush brush = new ImageBrush();
                brush.ImageSource = new BitmapImage(new Uri(@"Images/play.png", UriKind.Relative));
                play.Background = brush;
                Stop();
                playing = false;
                try
                {

                //    if (NavigationService.CanGoBack)
                //    {
                //        while (NavigationService.RemoveBackEntry() != null)
                //        {
                //            NavigationService.RemoveBackEntry();
                //        }
                //    }
                }
                catch
                {
                }
            }

            else
            {

            }
          }

        }

请帮我提供正确的代码.即使除了 MediaElement 之外,还有其他任何方式可以在后台流式传输媒体,也请提出建议.希望尽快回复.提前感谢大家.

Please help me with the proper code. Even if there is any other way to stream media in background other than MediaElement, please suggest that too.. Hoping a reply soon. Thanks to all in advance.

推荐答案

您必须使用 BackgroundAudioPlayer 用于此.

You must use BackgroundAudioPlayer for this.

你应该看看 Microsoft.Phone.BackgroundAudio 命名空间也是如此.

You should take a look at Microsoft.Phone.BackgroundAudio Namespace too.

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

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