MediaChangedEvent now hooked up to player component.

This commit is contained in:
watsonb8 2019-05-24 17:17:14 -04:00
parent 80e9a4543d
commit 64c2285817
7 changed files with 83 additions and 4 deletions

View File

@ -31,6 +31,8 @@ namespace Aurora.Backend.Models.Media
public abstract MediaTypeEnum MediaType { get; } public abstract MediaTypeEnum MediaType { get; }
public abstract BaseMetadata Metadata { get; protected set; }
/// <summary> /// <summary>
/// Gets or sets the data stream that holds the song. /// Gets or sets the data stream that holds the song.

View File

@ -14,7 +14,7 @@ namespace Aurora.Backend.Models.Media
#region Properties #region Properties
public FileInfo File { get; private set; } public FileInfo File { get; private set; }
public AudioMetadata Metadata { get; private set; } public override BaseMetadata Metadata { get; protected set; }
public override MediaTypeEnum MediaType public override MediaTypeEnum MediaType
{ {

View File

@ -0,0 +1,17 @@
using System;
using Aurora.Backend.Models.Media;
namespace Aurora.Backend.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;
}
}
}

View File

@ -1,4 +1,5 @@
using System; using System;
using Aurora.Backend.Models.Media;
namespace Aurora.Backend.Services.PlayerService namespace Aurora.Backend.Services.PlayerService
{ {

View File

@ -22,6 +22,8 @@ namespace Aurora.Backend.Services.PlayerService
/// </summary> /// </summary>
public event PlaybackStateChangedEventHandler PlaybackStateChanged; public event PlaybackStateChangedEventHandler PlaybackStateChanged;
public event MediaChangedEventHandler MediaChanged;
/// <summary> /// <summary>
/// The state of playback /// The state of playback
/// </summary> /// </summary>
@ -47,6 +49,8 @@ namespace Aurora.Backend.Services.PlayerService
_mediaPlayer = new MediaPlayer(md); _mediaPlayer = new MediaPlayer(md);
_mediaPlayer.Stopped += OnStopped; _mediaPlayer.Stopped += OnStopped;
md.Dispose(); md.Dispose();
MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Metadata));
} }
/// <summary> /// <summary>

View File

@ -14,7 +14,7 @@
<StackLayout <StackLayout
Grid.Column="0"> Grid.Column="0">
<Label <Label
Text="{Binding SongName}"/> Text="{Binding SongTitle}"/>
<Label <Label
Text="{Binding ArtistName}"/> Text="{Binding ArtistName}"/>
</StackLayout> </StackLayout>

View File

@ -2,16 +2,20 @@
using Xamarin.Forms; using Xamarin.Forms;
using Aurora.Frontend.Views; using Aurora.Frontend.Views;
using Aurora.Backend.Services.PlayerService; using Aurora.Backend.Services.PlayerService;
using Aurora.Backend.Models.Media;
namespace Aurora.Frontend.Components.MediaPlayer namespace Aurora.Frontend.Components.MediaPlayer
{ {
public class PlayerViewModel : BaseViewModel public class PlayerViewModel : BaseViewModel
{ {
PlayerService _playerService; PlayerService _playerService;
BaseMetadata _metadata;
public PlayerViewModel() public PlayerViewModel()
{ {
_playerService = PlayerService.Instance; _playerService = PlayerService.Instance;
_playerService.PlaybackStateChanged += OnPlaybackStateChanged; _playerService.PlaybackStateChanged += OnPlaybackStateChanged;
_playerService.MediaChanged += OnMediaChanged;
PlayCommand = new Command(OnPlayExecute, CanPlayExecute); PlayCommand = new Command(OnPlayExecute, CanPlayExecute);
PreviousCommand = new Command(OnPreviousExecute, CanPreviousExecute); PreviousCommand = new Command(OnPreviousExecute, CanPreviousExecute);
@ -20,6 +24,7 @@ namespace Aurora.Frontend.Components.MediaPlayer
~PlayerViewModel() ~PlayerViewModel()
{ {
_playerService.PlaybackStateChanged -= OnPlaybackStateChanged;
} }
@ -28,10 +33,43 @@ namespace Aurora.Frontend.Components.MediaPlayer
public Command NextCommand { get; private set; } public Command NextCommand { get; private set; }
public Command PreviousCommand { get; private set; } public Command PreviousCommand { get; private set; }
public string PlayButtonText public string PlayButtonText
{ {
get { return PlayerService.Instance.PlaybackState == PlaybackState.Buffering ? "Play" : "Pause"; } get { return _playerService.PlaybackState == PlaybackState.Buffering ? "Play" : "Pause"; }
}
/// <summary>
/// TODO keep player view generic between audio and video.
/// </summary>
/// <value></value>
public string ArtistName
{
get
{
if (_metadata == null)
{
return "";
}
AudioMetadata metadata = _metadata as AudioMetadata;
return metadata.Artist;
}
}
/// <summary>
/// TODO keep player view generic between audio and video.
/// </summary>
/// <value></value>
public string SongTitle
{
get
{
if (_metadata == null)
{
return "";
}
AudioMetadata metadata = _metadata as AudioMetadata;
return metadata.Title;
}
} }
#endregion Public Properties #endregion Public Properties
@ -96,6 +134,11 @@ namespace Aurora.Frontend.Components.MediaPlayer
#endregion public Methods #endregion public Methods
#region EventHandlers #region EventHandlers
/// <summary>
/// PlayerService playback state changed event handler.
/// </summary>
/// <param name="sender">The sending object.</param>
/// <param name="args">Event arguments.</param>
public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args) public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args)
{ {
OnPropertyChanged("PlayButtonText"); OnPropertyChanged("PlayButtonText");
@ -103,6 +146,18 @@ namespace Aurora.Frontend.Components.MediaPlayer
NextCommand.ChangeCanExecute(); NextCommand.ChangeCanExecute();
PreviousCommand.ChangeCanExecute(); PreviousCommand.ChangeCanExecute();
} }
/// <summary>
/// PlayerService media changed event handler.
/// </summary>
/// <param name="sender">The sending object.</param>
/// <param name="args">Event arguments.</param>
public void OnMediaChanged(object sender, MediaChangedEventArgs args)
{
_metadata = args.NewMetadata;
OnPropertyChanged("ArtistName");
OnPropertyChanged("SongTitle");
}
#endregion EventHandlers #endregion EventHandlers
} }
} }