【问题标题】:Play Audio(mp3) in every page on Windows Store Apps C# XAML在 Windows Store Apps C# XAML 的每个页面中播放音频(mp3)
【发布时间】:2014-01-22 18:50:13
【问题描述】:

我在 Windows Store App 项目上有多个页面。在 MainPage 背景音频开始播放,但是当我单击按钮并转到其他页面时,音乐已停止。有一个背景音频必须播放每一页,每一页都有自己的音频(mp3)。(我知道 MediaElement 一次只能播放一个音频)

我试过这个 http://www.c-sharpcorner.com/UploadFile/7e39ca/play-audion-on-background-in-windows-store-apps/

它在后台工作,但是当更改页面音频停止时。

【问题讨论】:

    标签: c# xaml windows-store-apps mp3


    【解决方案1】:

    不要使用 mediaelement,使用背景音频播放器,如果您已经在使用背景音频播放器,那么它应该可以按照您的意愿工作。如果没有,那么尽管它应该可以工作,因为它在后台运行,但你可以试试这个

    在每个页面上暂停背景播放器 onNavigatedFrom 事件并播放它 onNavigatedTo 事件

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if ((BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing) && (BackgroundAudioPlayer.Instance.CanSeek))
            {
                BackgroundAudioPlayer.Instance.Pause();
            }
        }
    
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Pause) 
                {
                    BackgroundAudioPlayer.Instance.PlayerState = BackgroundAudioPlayer.Instance.Play();
                }
        }
    

    【讨论】:

    • BackgroundAudioPlayer 给我错误。尝试使用 Microsoft.Phone.BackgroundAudio 添加;但它也给了我错误。
    • 错误是什么?您必须首先创建一个背景音频应用程序,您知道吗?
    • 是的,但我猜 BackgroundAudioPlayer 需要 Microsoft.Phone.BackgroundAudio 库,当我尝试添加项目时它会出错。我认为 Windows 商店应用程序项目没有那个库。
    【解决方案2】:

    您是否尝试将上述后台播放器代码放入您的 App.xaml/App.xaml.cs 中?

    【讨论】:

    • 当我将这些代码添加到 app.xaml 时,它会给出错误
    • 在resources块中添加是否也会报错?
    • 感谢您的回复,不,当我在 块中添加代码时它不会出错,但它不会开始播放音频。
    • 如果你愿意,把你的项目上传到skydrive,在这里留下一个链接,我去看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多