0

I use the XamarinMediaManager in a Xamarin.Forms project, to play audio from a web radio.

But I would like to manage error cases, like when the URL is no longer available, or if there is not internet access for example.

I launch the audio stream like this:

var mediaItem = await CrossMediaManager.Current.Play(audioUrl);

If I use a "wrong" URL, nothing happens, but I didn't found how to "identify" this case. It's the same thing if I activate the airplane mode.

I would like to display an error message to the user in both cases, so he could understand why the audio doesn't launch.

Is there a way to manage this?

Gold.strike
  • 1,269
  • 13
  • 47
  • https://github.com/Baseflow/XamarinMediaManager#hook-into-events – Jason Feb 09 '21 at 18:03
  • Good idea @Jason. I've tried but the `MediaItemFailed`event is not fired if I use a wrong URL or the airplane mode. I already use the `StateChanged`event that is well fired: but the state passed to "Pause" if I use a wrong URL or the airplane mode. So it's not possible to track an error like I would like to. – Gold.strike Feb 09 '21 at 19:15
  • 1
    @Gold.strike About internet available, you can use [Xamarin.Essentials: Connectivity](https://learn.microsoft.com/en-us/xamarin/essentials/connectivity?tabs=android) to check. – Cherry Bu - MSFT Feb 10 '21 at 03:31
  • Hi @CherryBu-MSFT you're right for this specific case. One also suggested me another solution for other cases by checking `MediaItem.Title`. – Gold.strike Feb 11 '21 at 09:04

1 Answers1

0

Finally, I've implemented a solution that one have suggested by using MediaItem.Title:

var mediaItem = await CrossMediaManager.Current.Play(radioUrl);
var currentTitle = mediaItem.Title;

This only works for iOS, on Android the use of mediaItem.Title raise an Exception...

So, to manage Android case, I use the MediaItemFailed:

CrossMediaManager.Current.MediaItemFailed += OnCurrentMediaItemFailed; 

The event is only raised after 1 minute on Android, and it's not raised at all on iOS.

This solution is not perfect and is no the same for 2 platforms, but I don't found a better one yet...

Gold.strike
  • 1,269
  • 13
  • 47