Question

I'm currently using AudioPlaybackAgent to play online audio stream (mp3). If device is connected to the internet audio is playing. But when there is no network connection and I press the play button in my app, crashes with

An unhandled exception of type 'System.SystemException' occurred in System.Windows.ni.dll

And it points me to this part of code

....
case UserAction.Play:
    if (player.PlayerState != PlayState.Playing)
    {
        player.Play();
    }
    break;  // << right here
....

I can paste my whole Audio Player but its standard generated background audio player. What is also wired here is that on background player initialization there is line of code

Application.Current.UnhandledException += AudioPlayer_UnhandledException;

which should handle unhandled exceptions...but it don't.

I can catch this exception inside audio player using simple try-catch but I can't display it using MessageBox right from audioplaybackagent (because MS is not allowing it if you try to submit app to store).

So my questions here are:

  1. How can i capture exception inside my app and then show for example MessageBox to user saying that there is no network connection
  2. Why is unhandled exception not handled by piece of code posted?

Thanks for your help!

Was it helpful?

Solution 2

I've figured it out using following code to check if network is available on tapping "play" button

if (NetworkInterface.GetIsNetworkAvailable() == false)
{
    // push some info to user...
}

OTHER TIPS

Did you check the AudioPlayer_UnhandledException method in your audio playback agent? Out of the box, it's logic only handles errors when the debugger is attached, if I recall correctly.

However, IMO, a better way to handle this situation would be to check if there is a network connection and disable the audio streaming/alert the user accordingly in the app itself, rather than relying on the agent to detect and notify if the network is available or not.

Windows Phone provides ways to look up connection status in Microsoft.Phone.Net.NetworkInformation, namely the DeviceNetworkInformation class.

DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(NetworkChange);

void NetworkChange(object sender, NetworkNotificationEventArgs e)
{
    // Network changed, disable audio streaming and alert user
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top