Skip to content

Instantly share code, notes, and snippets.

@ufukhawk
Created February 19, 2019 09:45
Show Gist options
  • Save ufukhawk/bee95cc6dc4636ac3b505185e7b559ee to your computer and use it in GitHub Desktop.
Save ufukhawk/bee95cc6dc4636ac3b505185e7b559ee to your computer and use it in GitHub Desktop.
public class AudioPlayerViewModel: INotifyPropertyChanged
{
private IAudioPlayerService _audioPlayer;
private bool _isStopped;
public event PropertyChangedEventHandler PropertyChanged;
public AudioPlayerViewModel(IAudioPlayerService audioPlayer)
{
_audioPlayer = audioPlayer;
_audioPlayer.OnFinishedPlaying = () => {
_isStopped = true;
CommandText = "Play";
};
CommandText = "Play";
_isStopped = true;
}
private string _commandText;
public string CommandText
{
get { return _commandText;}
set
{
_commandText = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CommandText"));
}
}
private ICommand _playPauseCommand;
public ICommand PlayPauseCommand
{
get
{
return _playPauseCommand ?? (_playPauseCommand = new Command(
(obj) =>
{
if (CommandText == "Play")
{
if (_isStopped)
{
_isStopped = false;
_audioPlayer.Play("Galway.mp3");
}
else
{
_audioPlayer.Play();
}
CommandText = "Pause";
}
else
{
_audioPlayer.Pause();
CommandText = "Play";
}
}));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment