Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds some extension methods to BackgroundAudioPlayer to work with the exceptions it commonly throws.
/*
Copyright (C) 2013 Richard Szalay
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.Phone.BackgroundAudio
{
public static class BackgroundAudioPlayerDefensiveExtensions
{
public static void TrySetTrack(this BackgroundAudioPlayer source, AudioTrack track)
{
try
{
source.Track = track;
}
catch (Exception)
{
source.Close();
source.Track = track;
}
}
public static bool TryGetTrack(this BackgroundAudioPlayer source, out AudioTrack track)
{
try
{
track = source.Track;
return true;
}
catch (Exception)
{
track = null;
return false;
}
}
public static AudioTrack GetTrackOrDefault(this BackgroundAudioPlayer source)
{
AudioTrack track;
return source.TryGetTrack(out track) ? track : null;
}
public static bool TryGetPosition(this BackgroundAudioPlayer source, out TimeSpan position)
{
PlayState playState;
if (!TryGetPlayerState(source, out playState) ||
(playState == PlayState.Unknown))
{
position = TimeSpan.Zero;
return false;
}
try
{
position = source.Position;
return true;
}
catch (Exception)
{
position = TimeSpan.Zero;
return false;
}
}
public static bool TrySetPosition(this BackgroundAudioPlayer source, TimeSpan position)
{
try
{
source.Position = position;
return true;
}
catch (Exception)
{
return false;
}
}
public static bool TryGetPlayerState(this BackgroundAudioPlayer source, out PlayState playState)
{
try
{
playState = source.PlayerState;
return true;
}
catch (Exception)
{
playState = PlayState.Unknown;
return false;
}
}
public static PlayState GetPlayerStateOrDefault(this BackgroundAudioPlayer source)
{
PlayState state;
return source.TryGetPlayerState(out state) ? state : PlayState.Unknown;
}
public static bool TryFastForward(this BackgroundAudioPlayer source)
{
try
{
source.FastForward();
return true;
}
catch (Exception)
{
return false;
}
}
public static bool TryRewind(this BackgroundAudioPlayer source)
{
try
{
source.Rewind();
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
public static bool TryStop(this BackgroundAudioPlayer source)
{
try
{
source.Stop();
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
public static bool TryGetDuration(this AudioTrack track, out TimeSpan duration)
{
try
{
duration = track.Duration;
return true;
}
catch (InvalidOperationException)
{
duration = TimeSpan.Zero;
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment