2019-05-24 14:27:19 +00:00
|
|
|
|
using System;
|
2019-11-04 06:43:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-07-05 18:17:09 +00:00
|
|
|
|
using Aurora.Models.Media;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
using LibVLCSharp.Shared;
|
|
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
|
namespace Aurora.Services.PlayerService
|
2019-05-24 14:27:19 +00:00
|
|
|
|
{
|
|
|
|
|
public class PlayerService : BaseService<PlayerService>
|
|
|
|
|
{
|
|
|
|
|
private BaseMedia _currentMedia;
|
|
|
|
|
private MediaPlayer _mediaPlayer;
|
|
|
|
|
private LibVLC _libvlc;
|
|
|
|
|
private PlaybackState _state;
|
|
|
|
|
|
|
|
|
|
public PlayerService()
|
|
|
|
|
{
|
|
|
|
|
_libvlc = new LibVLC();
|
|
|
|
|
_state = PlaybackState.Stopped;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event handler for changing playback states.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event PlaybackStateChangedEventHandler PlaybackStateChanged;
|
|
|
|
|
|
2019-05-24 21:17:14 +00:00
|
|
|
|
public event MediaChangedEventHandler MediaChanged;
|
|
|
|
|
|
2019-05-24 14:27:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The state of playback
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
|
|
|
|
public PlaybackState PlaybackState
|
|
|
|
|
{
|
|
|
|
|
get { return _state; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load media into the media player.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="media">Media to load</param>
|
2019-11-04 06:43:26 +00:00
|
|
|
|
public async Task LoadMedia(BaseMedia media)
|
2019-05-24 14:27:19 +00:00
|
|
|
|
{
|
|
|
|
|
if (_state == PlaybackState.Playing || _state == PlaybackState.Buffering)
|
|
|
|
|
{
|
|
|
|
|
Unload();
|
|
|
|
|
}
|
|
|
|
|
_currentMedia = media;
|
2019-11-04 06:43:26 +00:00
|
|
|
|
await _currentMedia.Load();
|
2019-05-24 14:27:19 +00:00
|
|
|
|
var md = new Media(_libvlc, _currentMedia.DataStream);
|
|
|
|
|
_mediaPlayer = new MediaPlayer(md);
|
|
|
|
|
_mediaPlayer.Stopped += OnStopped;
|
|
|
|
|
md.Dispose();
|
2019-05-24 21:17:14 +00:00
|
|
|
|
|
|
|
|
|
MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Metadata));
|
2019-05-24 14:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Play currently loaded media.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Play()
|
|
|
|
|
{
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackState oldState = _state;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
_state = PlaybackState.Playing;
|
|
|
|
|
_mediaPlayer.Play();
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
2019-05-24 14:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pause currently loaded media.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Pause()
|
|
|
|
|
{
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackState oldState = _state;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
_state = PlaybackState.Buffering;
|
|
|
|
|
_mediaPlayer.Pause();
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
2019-05-24 14:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stop currently loaded media.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackState oldState = _state;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
_state = PlaybackState.Stopped;
|
|
|
|
|
_mediaPlayer.Stop();
|
2019-05-24 19:59:26 +00:00
|
|
|
|
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
2019-05-24 14:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Enqueue(BaseMedia song)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dequeue(BaseMedia song)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Unload currently loaded media.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Unload()
|
|
|
|
|
{
|
|
|
|
|
_currentMedia.Unload();
|
|
|
|
|
_mediaPlayer.Media = null;
|
|
|
|
|
_mediaPlayer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event fired when currently loaded media player stops.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
private void OnStopped(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped));
|
|
|
|
|
_state = PlaybackState.Stopped;
|
|
|
|
|
this.Unload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|