简体   繁体   English

在Windows Phone 8.1背景中播放声音

[英]Play sound in windows phone 8.1 background

I am writing an app which require background music. 我正在编写需要背景音乐的应用程序。 Below is my code which i made after searching different forums. 以下是我在搜索不同论坛后制作的代码。

class AudioPlayer : IBackgroundTask
{
    private BackgroundTaskDeferral _deferral;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        _deferral = taskInstance.GetDeferral();
        taskInstance.Canceled += TaskInstance_Canceled;
    }
    private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        _deferral.Complete();
    }

    //===========================================================================================================
    public void startTick()
    {
        BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Audio/clock.wav"));
        BackgroundMediaPlayer.Current.Play();
    }
}  

Problem is it doesnt work. 问题是它不起作用。 No sound from emulator. 模拟器没有声音。 Please Help! 请帮忙!

There are few things that might have gone wrong (I guess it's not all your code). 几乎没有什么地方出错(我想这不是您的全部代码)。 The best would be here if you would follow Bacground Audio Overview . 如果您要遵循Bacground Audio Overview,那将是最好的选择。

Please also check if you had added needed declarations (I suspect that this may be a problem here): go to your Package.appxmanifest file, open Declarations then you should add Background Tasks with an Audio (as suppoerted task type) and specify its entry point (probably yournamespace.AudioPlayer ). 另请检查您是否添加了所需的声明(我怀疑这可能是这里的问题):转到Package.appxmanifest文件,打开“声明”,然后应添加带有Audio Background Tasks (作为支持的任务类型)并指定其条目点(可能是yournamespace.AudioPlayer )。

Also remember that, as said at MSDN (the link above): 还要记住,正如MSDN(上面的链接)所说:

Your background task starts the first time your app accesses BackgroundMediaPlayer.Current in foreground app code. 您的应用程序首次在您的应用程序访问BackgroundMediaPlayer时启动后台任务。当前在前台应用程序代码中。

I advise to set a breakpoint at Run method and you should see if AudioPlayer had started. 我建议在Run方法中设置一个断点,您应该查看AudioPlayer是否已启动。 Also remember to provide communication between UI and AudioPalyer - ToUI and ToPlayer (of course if you need it). 还记得在UI和AudioPalyer之间提供通信-ToUIToPlayer (当然,如果需要的话)。

You may also want to configure your SMTP , so that your AudioPlayer would react to those buttons that show when you hit VolumeControl. 您可能还需要配置SMTP ,以便您的AudioPlayer对单击VolumeControl时显示的按钮做出反应。

为了帮助您理解BackgroundMediaPlayer的正确用法,请看一下示例,您的想法将变得清晰。您必须集成另一个项目的引用以使用背景Audio。只需看一下即可。希望对您有所帮助。

In case someone else stumbles across this, any IBackgroundTask on Win Phone 8.1 needs to be contained in a WinRT component project type and not in a class library or PCL. 万一有人偶然发现,Win Phone 8.1上的任何IBackgroundTask都必须包含在WinRT组件项目类型中,而不是包含在类库或PCL中。

The MediaPlayer exe will just exit and you'll be left pulling out your hair if you give it a dll. MediaPlayer exe即将退出,如果您给它提供dll,您将被拔出头发。

That and make sure you entry point path is correct in your appxmanifest file and you should be good to go. 这样,并确保您的appxmanifest文件中的入口点路径正确,并且您应该一切顺利。 Just put a breakpoint in your Run() method to verify. 只需在您的Run()方法中放置一个断点来进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM