using System; using Aurora.Models.Media; using LibVLCSharp.Shared; namespace Aurora.Services.PlayerService { public class PlayerService : BaseService { private BaseMedia _currentMedia; private MediaPlayer _mediaPlayer; private LibVLC _libvlc; private PlaybackState _state; public PlayerService() { _libvlc = new LibVLC(); _state = PlaybackState.Stopped; } /// /// Event handler for changing playback states. /// public event PlaybackStateChangedEventHandler PlaybackStateChanged; public event MediaChangedEventHandler MediaChanged; /// /// The state of playback /// /// public PlaybackState PlaybackState { get { return _state; } } /// /// Load media into the media player. /// /// Media to load public void LoadMedia(BaseMedia media) { if (_state == PlaybackState.Playing || _state == PlaybackState.Buffering) { Unload(); } _currentMedia = media; _currentMedia.Load(); var md = new Media(_libvlc, _currentMedia.DataStream); _mediaPlayer = new MediaPlayer(md); _mediaPlayer.Stopped += OnStopped; md.Dispose(); MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Metadata)); } /// /// Play currently loaded media. /// public void Play() { PlaybackState oldState = _state; _state = PlaybackState.Playing; _mediaPlayer.Play(); PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state)); } /// /// Pause currently loaded media. /// public void Pause() { PlaybackState oldState = _state; _state = PlaybackState.Buffering; _mediaPlayer.Pause(); PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state)); } /// /// Stop currently loaded media. /// public void Stop() { PlaybackState oldState = _state; _state = PlaybackState.Stopped; _mediaPlayer.Stop(); PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state)); } public void Enqueue(BaseMedia song) { throw new NotImplementedException(); } public void Dequeue(BaseMedia song) { throw new NotImplementedException(); } /// /// Unload currently loaded media. /// private void Unload() { _currentMedia.Unload(); _mediaPlayer.Media = null; _mediaPlayer = null; } /// /// Event fired when currently loaded media player stops. /// /// /// private void OnStopped(object sender, EventArgs args) { PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped)); _state = PlaybackState.Stopped; this.Unload(); } } }