Kasun Kodagoda
Kasun Kodagoda

Reputation: 4024

UnauthorizedAccessException thrown when accessing MediaLibrary and MediaPlayer

I'm trying to create a Media Player for windows phone. I have a earlier project i created for Windows Phone 7 but when i use the same code for windows phone 8 the app crashes whit a UnhandledException. I'm using Lumia 925 device. The previous app is in the store and it is used in Windows Phone 8 devices. I dont know what the problem is. I tried to create a new project with a button when clicked plays a song from the library. But same thing happens.. the app crashes with UnhandledException. Here is the code

    private void btnPlay_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            MediaLibrary lib = new MediaLibrary();
            var songsLib = lib.Songs;
            MediaPlayer.Play(songsLib[0]);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

I have added all the permissions in the manifest. I have searched and can't find a solution for this. My emulator don't work right now and i can't check it using the emulator. But i think both apps are working in the emulator. The problems comes when running in the device.

P.S. Something i noticed, When i downloaded my original app from the store and run it, it runs well when there are no songs in the library. as soon as there are songs in the phone the app crashes.

Upvotes: 0

Views: 199

Answers (1)

Mikko Viitala
Mikko Viitala

Reputation: 8404

From MSDN http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.frameworkdispatcher.update.aspx

If you use the XNA Framework in an application that does not implement the Game class—for example, a Windows Phone application that uses the Silverlight application model—you must call the FrameworkDispatcher.Update method yourself to dispatch messages that are in the XNA Framework message queue.

So just add one line to your existing code and you are done.

MediaLibrary lib = new MediaLibrary();
var songsLib = lib.Songs;
Microsoft.Xna.Framework.FrameworkDispatcher.Update(); // <-- this one
MediaPlayer.Play(songsLib[0]);

And just to be sure, check that you have enabled following capabilities.

<Capability Name="ID_CAP_MEDIALIB_AUDIO"/>
<Capability Name="ID_CAP_MEDIALIB_PLAYBACK"/>

Upvotes: 4

Related Questions