diff --git a/.vscode/launch.json b/.vscode/launch.json index 8a67338..600453d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "type": "mono", "request": "attach", "address": "localhost", - "port": 55555 + "port": 55555, } ] } \ No newline at end of file diff --git a/Aurora/Backend/Models/BaseSong.cs b/Aurora/Backend/Models/BaseMedia.cs similarity index 96% rename from Aurora/Backend/Models/BaseSong.cs rename to Aurora/Backend/Models/BaseMedia.cs index 935029a..1175bc6 100644 --- a/Aurora/Backend/Models/BaseSong.cs +++ b/Aurora/Backend/Models/BaseMedia.cs @@ -3,12 +3,12 @@ using System.IO; namespace Aurora.Backend.Models { - public abstract class BaseSong + public abstract class BaseMedia { private bool _loaded; private Stream _stream; - public BaseSong() + public BaseMedia() { _loaded = false; Id = Guid.NewGuid().ToString(); diff --git a/Aurora/Backend/Models/LocalSong.cs b/Aurora/Backend/Models/LocalAudio.cs similarity index 75% rename from Aurora/Backend/Models/LocalSong.cs rename to Aurora/Backend/Models/LocalAudio.cs index 502344c..4198e0f 100644 --- a/Aurora/Backend/Models/LocalSong.cs +++ b/Aurora/Backend/Models/LocalAudio.cs @@ -3,9 +3,9 @@ using System.IO; namespace Aurora.Backend.Models { - public class LocalSong : BaseSong + public class LocalAudio : BaseMedia { - public LocalSong(FileInfo fileInfo) + public LocalAudio(FileInfo fileInfo) { File = fileInfo; } @@ -15,6 +15,9 @@ namespace Aurora.Backend.Models #endregion Properties + /// + /// Override load method. + /// public override void Load() { if (this.DataStream != null) @@ -26,6 +29,9 @@ namespace Aurora.Backend.Models base.Load(); } + /// + /// Override unload method + /// public override void Unload() { if (this.DataStream != null) diff --git a/Aurora/Backend/Services/LibraryService.cs b/Aurora/Backend/Services/LibraryService.cs index 1d56f67..dbdbd95 100644 --- a/Aurora/Backend/Services/LibraryService.cs +++ b/Aurora/Backend/Services/LibraryService.cs @@ -12,14 +12,14 @@ namespace Aurora.Backend.Services #region Fields private string _pathName = "/Users/brandonwatson/Music/iTunes/iTunes Media/Music"; private string _extensions = ".wav,.mp3,.aiff,.flac,.m4a,.m4b,.wma"; - private Dictionary _library; + private Dictionary _library; #endregion Fields public LibraryService() { - _library = new Dictionary(); + _library = new Dictionary(); LoadLibrary(); } @@ -27,10 +27,10 @@ namespace Aurora.Backend.Services /// Gets the songs. /// /// The songs. - public ObservableCollection GetLibrary() + public ObservableCollection GetLibrary() { - ObservableCollection collection = new ObservableCollection(); - foreach (KeyValuePair pair in _library) + ObservableCollection collection = new ObservableCollection(); + foreach (KeyValuePair pair in _library) { collection.Add(pair.Value); } @@ -50,7 +50,7 @@ namespace Aurora.Backend.Services { TagLib.File tagFile = TagLib.File.Create(file.FullName); - BaseSong song = new LocalSong(file) + BaseMedia song = new LocalAudio(file) { Title = tagFile.Tag.Title, Album = tagFile.Tag.Album, diff --git a/Aurora/Backend/Services/PlayerService.cs b/Aurora/Backend/Services/PlayerService.cs deleted file mode 100644 index 8958158..0000000 --- a/Aurora/Backend/Services/PlayerService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using Aurora.Backend.Models; -using LibVLCSharp.Shared; - -namespace Aurora.Backend.Services -{ - public class PlayerService : BaseService - { - public PlayerService() - { - } - - public void Play(BaseSong song) - { - using (var libVLC = new LibVLC()) - { - song.Load(); - var media = new Media(libVLC, song.DataStream); - using (var mp = new MediaPlayer(media)) - { - media.Dispose(); - mp.Play(); - Console.ReadKey(); - } - - song.Unload(); - } - - - } - - } -} diff --git a/Aurora/Backend/Services/PlayerService/PlaybackState.cs b/Aurora/Backend/Services/PlayerService/PlaybackState.cs new file mode 100644 index 0000000..37c6773 --- /dev/null +++ b/Aurora/Backend/Services/PlayerService/PlaybackState.cs @@ -0,0 +1,9 @@ +using System; + +public enum PlaybackState +{ + Playing, + Stopped, + Buffering, + +} \ No newline at end of file diff --git a/Aurora/Backend/Services/PlayerService/PlaybackStateChangedEvent.cs b/Aurora/Backend/Services/PlayerService/PlaybackStateChangedEvent.cs new file mode 100644 index 0000000..461049a --- /dev/null +++ b/Aurora/Backend/Services/PlayerService/PlaybackStateChangedEvent.cs @@ -0,0 +1,18 @@ +using System; + +namespace Aurora.Backend.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; + } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Services/PlayerService/PlayerService.cs b/Aurora/Backend/Services/PlayerService/PlayerService.cs new file mode 100644 index 0000000..c92cd87 --- /dev/null +++ b/Aurora/Backend/Services/PlayerService/PlayerService.cs @@ -0,0 +1,116 @@ +using System; +using Aurora.Backend.Models; +using LibVLCSharp.Shared; + +namespace Aurora.Backend.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; + + /// + /// 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(); + } + + /// + /// Play currently loaded media. + /// + public void Play() + { + PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Playing)); + _state = PlaybackState.Playing; + _mediaPlayer.Play(); + + } + + /// + /// Pause currently loaded media. + /// + public void Pause() + { + PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Buffering)); + _state = PlaybackState.Buffering; + _mediaPlayer.Pause(); + } + + /// + /// Stop currently loaded media. + /// + public void Stop() + { + PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped)); + _state = PlaybackState.Stopped; + _mediaPlayer.Stop(); + } + + 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(); + } + + } +} diff --git a/Aurora/Frontend/Components/MediaPlayer/Player.xaml b/Aurora/Frontend/Components/MediaPlayer/Player.xaml new file mode 100644 index 0000000..f242330 --- /dev/null +++ b/Aurora/Frontend/Components/MediaPlayer/Player.xaml @@ -0,0 +1,10 @@ + + + + +