See commit description for changes

Added PageContainer to dynamically load pages within the main content. Added player component to control music playback(Half functional). Added playback changed event not the player service.
This commit is contained in:
watsonb8 2019-05-24 10:27:19 -04:00
parent 2dbe9cead9
commit 93be6dc100
26 changed files with 357 additions and 86 deletions

2
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"type": "mono",
"request": "attach",
"address": "localhost",
"port": 55555
"port": 55555,
}
]
}

View File

@ -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();

View File

@ -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
/// <summary>
/// Override load method.
/// </summary>
public override void Load()
{
if (this.DataStream != null)
@ -26,6 +29,9 @@ namespace Aurora.Backend.Models
base.Load();
}
/// <summary>
/// Override unload method
/// </summary>
public override void Unload()
{
if (this.DataStream != null)

View File

@ -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<string, BaseSong> _library;
private Dictionary<string, BaseMedia> _library;
#endregion Fields
public LibraryService()
{
_library = new Dictionary<string, BaseSong>();
_library = new Dictionary<string, BaseMedia>();
LoadLibrary();
}
@ -27,10 +27,10 @@ namespace Aurora.Backend.Services
/// Gets the songs.
/// </summary>
/// <returns>The songs.</returns>
public ObservableCollection<BaseSong> GetLibrary()
public ObservableCollection<BaseMedia> GetLibrary()
{
ObservableCollection<BaseSong> collection = new ObservableCollection<BaseSong>();
foreach (KeyValuePair<string, BaseSong> pair in _library)
ObservableCollection<BaseMedia> collection = new ObservableCollection<BaseMedia>();
foreach (KeyValuePair<string, BaseMedia> 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,

View File

@ -1,33 +0,0 @@
using System;
using Aurora.Backend.Models;
using LibVLCSharp.Shared;
namespace Aurora.Backend.Services
{
public class PlayerService : BaseService<PlayerService>
{
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();
}
}
}
}

View File

@ -0,0 +1,9 @@
using System;
public enum PlaybackState
{
Playing,
Stopped,
Buffering,
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,116 @@
using System;
using Aurora.Backend.Models;
using LibVLCSharp.Shared;
namespace Aurora.Backend.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;
/// <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();
}
/// <summary>
/// Play currently loaded media.
/// </summary>
public void Play()
{
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Playing));
_state = PlaybackState.Playing;
_mediaPlayer.Play();
}
/// <summary>
/// Pause currently loaded media.
/// </summary>
public void Pause()
{
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Buffering));
_state = PlaybackState.Buffering;
_mediaPlayer.Pause();
}
/// <summary>
/// Stop currently loaded media.
/// </summary>
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();
}
/// <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();
}
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Components.MediaPlayer.Player">
<ContentView.Content>
<StackLayout Orientation="Horizontal">
<Button Text="Previous" Command="{Binding PreviousCommand}" WidthRequest="100" HeightRequest="50"/>
<Button Text="{Binding PlayButtonText}" Command="{Binding PlayCommand}" WidthRequest="100" HeightRequest="50"/>
<Button Text="Next" Command="{Binding NextCommand}" WidthRequest="100" HeightRequest="50"/>
</StackLayout>
</ContentView.Content>
</ContentView>

View File

@ -2,12 +2,13 @@
using System.Collections.Generic;
using Xamarin.Forms;
namespace Aurora.Frontend.Components.MusicPlayer
namespace Aurora.Frontend.Components.MediaPlayer
{
public partial class Player : ContentView
{
public Player()
{
BindingContext = new PlayerViewModel();
InitializeComponent();
}
}

View File

@ -0,0 +1,108 @@
using System;
using Xamarin.Forms;
using Aurora.Frontend.Views;
using Aurora.Backend.Services.PlayerService;
namespace Aurora.Frontend.Components.MediaPlayer
{
public class PlayerViewModel : BaseViewModel
{
PlayerService _playerService;
public PlayerViewModel()
{
_playerService = PlayerService.Instance;
_playerService.PlaybackStateChanged += OnPlaybackStateChanged;
PlayCommand = new Command(OnPlayExecute, CanPlayExecute);
PreviousCommand = new Command(OnPreviousExecute, CanPreviousExecute);
NextCommand = new Command(OnNextExecute, CanNextExecute);
}
~PlayerViewModel()
{
}
#region Public Properties
public Command PlayCommand { get; private set; }
public Command NextCommand { get; private set; }
public Command PreviousCommand { get; private set; }
public string PlayButtonText
{
get { return PlayerService.Instance.PlaybackState == PlaybackState.Buffering ? "Play" : "Pause"; }
}
#endregion Public Properties
#region Public Methods
public bool CanPreviousExecute()
{
return true;
}
public void OnPreviousExecute()
{
}
public bool CanPlayExecute()
{
switch (_playerService.PlaybackState)
{
case PlaybackState.Buffering:
{
return true;
}
case PlaybackState.Playing:
{
return true;
}
case PlaybackState.Stopped:
{
return false;
}
}
return false;
}
public void OnPlayExecute()
{
switch (_playerService.PlaybackState)
{
case PlaybackState.Buffering:
{
_playerService.Play();
break;
}
case PlaybackState.Playing:
{
_playerService.Pause();
break;
}
}
}
public bool CanNextExecute()
{
return true;
}
public void OnNextExecute()
{
}
#endregion public Methods
#region EventHandlers
public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args)
{
OnPropertyChanged("PlayButtonText");
PlayCommand.ChangeCanExecute();
NextCommand.ChangeCanExecute();
PreviousCommand.ChangeCanExecute();
}
#endregion EventHandlers
}
}

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Components.MusicPlayer.Player">
<ContentView.Content>
</ContentView.Content>
</ContentView>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Albums.AlbumsView">
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Albums.AlbumsView">
<ContentPage.Content>
<Label Text="Albums" />
</ContentPage.Content>
</ContentPage>
</ContentView>

View File

@ -5,7 +5,7 @@ using Xamarin.Forms;
namespace Aurora.Frontend.Views.Albums
{
public partial class AlbumsView : ContentPage
public partial class AlbumsView : ContentView
{
public AlbumsView()
{

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Artists.ArtistsView">
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Artists.ArtistsView">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
</ContentView>

View File

@ -5,7 +5,7 @@ using Xamarin.Forms;
namespace Aurora.Frontend.Views.Artists
{
public partial class ArtistsView : ContentPage
public partial class ArtistsView : ContentView
{
public ArtistsView()
{

View File

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:Aurora.Frontend.Views.Songs"
xmlns:views="clr-namespace:Aurora.Frontend.Views.MainView"
xmlns:navigation="clr-namespace:Aurora.Frontend.Components.NavigationMenu"
x:Class="Aurora.Frontend.Views.Main.MainView"
MasterBehavior="Split">
<MasterDetailPage.Master>
<navigation:NavigationMenu x:Name="MasterPage" Items="{Binding Pages}"/>
</MasterDetailPage.Master>
<MasterDetailPage.Master>
<navigation:NavigationMenu x:Name="MasterPage" Items="{Binding Pages}"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<views:SongsView />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<views:PageContainer x:Name="ContentPage" />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Aurora.Frontend.Components.NavigationMenu;
using Aurora.Frontend.Views.MainView;
using Xamarin.Forms;
@ -15,6 +16,13 @@ namespace Aurora.Frontend.Views.Main
InitializeComponent();
BindingContext = new MainViewModel();
MasterPage.ListView.ItemSelected += ListView_ItemSelected;
//Set initial view from first item in list
ObservableCollection<NavigationGroupItem> screenList = (ObservableCollection<NavigationGroupItem>)MasterPage.ListView.ItemsSource;
var view = (View)Activator.CreateInstance(screenList.FirstOrDefault().FirstOrDefault().TargetType);
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
viewContent.Content = view;
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
@ -23,10 +31,10 @@ namespace Aurora.Frontend.Views.Main
if (item == null)
return;
var page = (Page)Activator.CreateInstance(item.TargetType);
page.Title = item.Title;
var view = (View)Activator.CreateInstance(item.TargetType);
Detail = new NavigationPage(page);
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
viewContent.Content = view;
MasterPage.ListView.SelectedItem = null;
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:components="clr-namespace:Aurora.Frontend.Components" xmlns:mp="clr-namespace:Aurora.Frontend.Components.MediaPlayer" x:Class="Aurora.Frontend.Views.MainView.PageContainer">
<ContentPage.Content>
<StackLayout>
<ContentPresenter x:Name="ViewContent"/>
<mp:Player HorizontalOptions="CenterAndExpand" VerticalOptions="End" HeightRequest="200"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Aurora.Frontend.Views.MainView
{
public partial class PageContainer : ContentPage
{
public PageContainer()
{
InitializeComponent();
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:songs="clr-namespace:Aurora.Frontend.Views.Songs"
xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
@ -47,4 +47,4 @@
</dg:DataGrid>
</ContentPage.Content>
</ContentPage>
</ContentView>

View File

@ -5,7 +5,7 @@ using Xamarin.Forms;
namespace Aurora.Frontend.Views.Songs
{
public partial class SongsView : ContentPage
public partial class SongsView : ContentView
{
public SongsView()
{

View File

@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using Aurora.Backend.Models;
using Aurora.Backend.Services;
using Aurora.Backend.Services.PlayerService;
using Xamarin.Forms;
namespace Aurora.Frontend.Views.Songs
@ -8,15 +9,15 @@ namespace Aurora.Frontend.Views.Songs
public class SongsViewModel : BaseViewModel
{
#region Fields
private ObservableCollection<BaseSong> _songsList;
private BaseSong _selectedSong;
private ObservableCollection<BaseMedia> _songsList;
private BaseMedia _selectedSong;
#endregion Fields
#region Constructor
public SongsViewModel()
{
_songsList = new ObservableCollection<BaseSong>();
_songsList = new ObservableCollection<BaseMedia>();
PlayCommand = new Command(PlayExecute);
Initialize();
}
@ -24,13 +25,13 @@ namespace Aurora.Frontend.Views.Songs
#endregion Constructor
#region Properties
public ObservableCollection<BaseSong> SongsList
public ObservableCollection<BaseMedia> SongsList
{
get { return _songsList; }
set { SetProperty(ref _songsList, value); }
}
public BaseSong SelectedSong
public BaseMedia SelectedSong
{
get { return _selectedSong; }
set { SetProperty(ref _selectedSong, value); }
@ -49,7 +50,8 @@ namespace Aurora.Frontend.Views.Songs
public void PlayExecute()
{
PlayerService.Instance.Play(_selectedSong);
PlayerService.Instance.LoadMedia(_selectedSong);
PlayerService.Instance.Play();
}
#endregion Methods

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Stations.StationsView">
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Aurora.Frontend.Views.Stations.StationsView">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
</ContentView>

View File

@ -5,7 +5,7 @@ using Xamarin.Forms;
namespace Aurora.Frontend.Views.Stations
{
public partial class StationsView : ContentPage
public partial class StationsView : ContentView
{
public StationsView()
{

View File

@ -3,5 +3,12 @@
{
"path": "."
}
]
],
"settings": {
"files.exclude": {
"**/bin": true,
"**/obj": true,
"**/packages": true
}
}
}