3
votes

I am working on a Windows Phone application for Windows 8.1. I need to use features like Motion classes, Isolated Storage etc. As these features were not supported in Windows Phone 8.1 I went for Windows Phone Silverlight 8.1. Now I have to use BackgroundMediaPlayer in my project and Windows.Media.Playback is not supported in Windows Phone Silverlight 8.1. Is there any possible way by which i can use all the basic API's like:-

-Microsoft.Devices.Sensors

-Microsoft.Xna.Framework

-System.IO.IsolatedStorage-System.Windows.Media.Imaging

and use BackgroundMediaPlayer, Motion classes?? any help will be really useful.

Thanks,

Ekta

2

2 Answers

7
votes

Playing Background audio differs in Windows Runtime and Silverlight (Overview). You were trying to use MediaPlayer which:

Minimum supported phone Windows Phone 8.1 [Windows Runtime apps only]

is only for runtime.

As for working with BackgroundAudioPlayer Class in Silverlight 8.1, there is a problem - it won't work. This is a limitation in Silverlight 8.1:

The AudioPlayerAgent and AudioStreamingAgent classes, which supported background audio playback for Windows Phone 8 apps, are not supported in Silverlight 8.1. If you want to support background audio playback, you can continue to use a Windows Phone 8 app or create a Windows Phone Store app, which supports new background audio APIs.

So in this case you will have to write app that target WP8.0 Silverlight or or WP8.1 Store app.

The similar question was here at MSDN forum.

0
votes

You can achieve that by Creating a new Windows Runtime Component. This is where your entry point for the Audio task will be. The entry point is a class that implements IBackgroundTask. This gives you one method that will be invoked on startup of the Audio task named Run.

In order to keep the Task alive indefinitely you need to keep track of the deferral that exists on the IBackgroundTaskInstance. Placing the deferral in a field for safe keeping will be enough. When the operating system cancels your task you will need to call Complete on this said deferral.

An example of a bare bone implementation of your task should look something like the following

public sealed 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();
    }
}

for more Refrence you can go here