Reorganization
This commit is contained in:
17
Aurora/Services/PlayerService/MediaChangedEvent.cs
Normal file
17
Aurora/Services/PlayerService/MediaChangedEvent.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
public delegate void MediaChangedEventHandler(object source, MediaChangedEventArgs e);
|
||||
|
||||
public class MediaChangedEventArgs : EventArgs
|
||||
{
|
||||
public BaseMetadata NewMetadata { get; private set; }
|
||||
|
||||
public MediaChangedEventArgs(BaseMetadata metadata)
|
||||
{
|
||||
NewMetadata = metadata;
|
||||
}
|
||||
}
|
||||
}
|
9
Aurora/Services/PlayerService/PlaybackState.cs
Normal file
9
Aurora/Services/PlayerService/PlaybackState.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
public enum PlaybackState
|
||||
{
|
||||
Playing,
|
||||
Stopped,
|
||||
Buffering,
|
||||
|
||||
}
|
19
Aurora/Services/PlayerService/PlaybackStateChangedEvent.cs
Normal file
19
Aurora/Services/PlayerService/PlaybackStateChangedEvent.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
public delegate void PlaybackStateChangedEventHandler(object source, PlaybackStateChangedEventArgs e);
|
||||
|
||||
public class PlaybackStateChangedEventArgs : EventArgs
|
||||
{
|
||||
public PlaybackState OldState { get; }
|
||||
public PlaybackState NewState { get; }
|
||||
|
||||
public PlaybackStateChangedEventArgs(PlaybackState oldState, PlaybackState newState)
|
||||
{
|
||||
OldState = oldState;
|
||||
NewState = newState;
|
||||
}
|
||||
}
|
||||
}
|
122
Aurora/Services/PlayerService/PlayerService.cs
Normal file
122
Aurora/Services/PlayerService/PlayerService.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
using LibVLCSharp.Shared;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
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;
|
||||
|
||||
public event MediaChangedEventHandler MediaChanged;
|
||||
|
||||
/// <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>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play currently loaded media.
|
||||
/// </summary>
|
||||
public void Play()
|
||||
{
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Playing;
|
||||
_mediaPlayer.Play();
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pause currently loaded media.
|
||||
/// </summary>
|
||||
public void Pause()
|
||||
{
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Buffering;
|
||||
_mediaPlayer.Pause();
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop currently loaded media.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user