如何知道我的应用程序是否在后台播放音乐? [英] how to know to my app if the music is playing in background ?

查看:531
本文介绍了如何知道我的应用程序是否在后台播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BackgroundAudio类,我的应用程序要知道是否正在播放时,立即将我发送到播放器页面,但是无法正常工作,我有此代码,但是我不知道该位置是否在主页文件中或App.cs文件以识别是否在播放

I'm using BackgroundAudio class my App to make know if is playing send me imediatly to the player page but the doesn't work i have this code but i dont know where place if in the main page file or App.cs file to recognize if is playing or not

if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
            {
                NavigationService.Navigate(new Uri("/PlayerPage.xaml", UriKind.Relative));
            }
            else
            {
                // do nothing
            }


推荐答案

您可以将其放在默认启动页面的OnNavigatedTo()函数中.

You can put this in OnNavigatedTo() function of your default startup page.

提示:如果您使用的是SDK 8.0,请确保未连接调试器.我注意到,连接调试器后,重新启动应用程序时,后台播放器实例将停止.

tip: If you're using SDK 8.0 ... make sure the debugger is NOT attached.  I notice that when the debugger is attached it the background player instance stops when the app is re-launched.

例如:

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
            {
                if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        NavigationService.Navigate(new Uri("/PlayerPage.xaml", UriKind.Relative));
                    });
                }
                else
                {
                    //do nothing
                }
            }
        }


 


 


这篇关于如何知道我的应用程序是否在后台播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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