Commit f47fbbbc authored by Martin Finkel's avatar Martin Finkel

update stuff

parent 67014c3b
......@@ -4,8 +4,12 @@
{
public const string App = "PulseMusic";
public const string Play = "Play";
public const string Progress = "Progress";
public const string Time = "Time";
public const string Position = "Position";
public const string Length = "Length";
public const string EndReached = "EndReached";
public const string Rewind = "Rewind";
public const string Forward = "Forward";
public const string Playing = "Playing";
}
}
}
\ No newline at end of file
using System;
using Xamarin.Forms;
namespace PulseMusic.Models
{
public class Countdown : BindableObject
{
TimeSpan _remainTime;
public event Action Completed;
public event Action Ticked;
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public bool IsRunning { get; set; }
public TimeSpan RemainTime
{
get { return _remainTime; }
private set
{
_remainTime = value;
OnPropertyChanged();
}
}
public void Start(int seconds = 1)
{
Device.StartTimer(TimeSpan.FromSeconds(seconds), () =>
{
if (IsRunning)
{
StartTime += TimeSpan.FromSeconds(1);
RemainTime = (EndTime - StartTime);
var ticked = RemainTime.TotalSeconds >= 0;
if (ticked)
{
Ticked?.Invoke();
}
else
{
Completed?.Invoke();
}
return ticked;
}
return true;
});
}
}
}
\ No newline at end of file
using LibVLCSharp.Shared;
using System.Diagnostics;
using Xamarin.Forms;
namespace PulseMusic
......@@ -7,6 +8,7 @@ namespace PulseMusic
{
LibVLC _libVLC;
MediaPlayer _mp;
const long OFFSET = 5000;
public PlaybackService()
{
......@@ -14,34 +16,63 @@ namespace PulseMusic
_libVLC = new LibVLC();
_mp = new MediaPlayer(_libVLC);
_mp = new MediaPlayer(_libVLC);
}
public void Init()
{
_mp.Media = new Media(_libVLC, "https://archive.org/download/ImagineDragons_201410/imagine%20dragons.mp4", Media.FromType.FromLocation);
_mp.Media = new Media(_libVLC, "https://streams.videolan.org/streams/mp3/05-Mr.%20Zebra.mp3", Media.FromType.FromLocation);
_mp.Media.AddOption(":no-video");
_mp.Media.Parse();
var artist = _mp.Media.Meta(Media.MetadataType.Artist);
_mp.TimeChanged += TimeChanged;
_mp.PositionChanged += PositionChanged;
_mp.LengthChanged += LengthChanged;
_mp.EndReached += EndReached;
_mp.Playing += Playing;
_mp.Paused += Paused;
MessagingCenter.Subscribe<string, bool>(MessengerKeys.App, MessengerKeys.Play, (vm, play) =>
MessagingCenter.Subscribe<string>(MessengerKeys.App, MessengerKeys.Rewind, vm =>
{
if (play)
_mp.Play();
else _mp.Pause();
Debug.WriteLine("Rewind");
_mp.Time -= OFFSET;
});
MessagingCenter.Subscribe<string>(MessengerKeys.App, MessengerKeys.Forward, vm =>
{
Debug.WriteLine("Forward");
_mp.Time += OFFSET;
});
}
public void Play(bool play)
{
if (play)
_mp.Play();
else _mp.Pause();
}
private void PositionChanged(object sender, MediaPlayerPositionChangedEventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Position, e.Position);
private void Paused(object sender, System.EventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, false);
private void Playing(object sender, System.EventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, true);
private void EndReached(object sender, System.EventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.EndReached);
private void LengthChanged(object sender, MediaPlayerLengthChangedEventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Length, e.Length);
private void PositionChanged(object sender, MediaPlayerPositionChangedEventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Progress, e.Position);
private void TimeChanged(object sender, MediaPlayerTimeChangedEventArgs e) =>
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Time, e.Time);
}
}
}
\ No newline at end of file
......@@ -8,7 +8,6 @@
<PackageReference Include="LibVLCSharp.Forms" Version="0.1.3" />
<PackageReference Include="Plugins.Forms.ButtonCircle" Version="2.0.3" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.60.2" />
<PackageReference Include="Xamarin.Essentials" Version="1.0.0" />
<PackageReference Include="Xamarin.FFImageLoading.Forms" Version="2.4.3.840" />
<PackageReference Include="Xamarin.FFImageLoading.Transformations" Version="2.4.3.840" />
<PackageReference Include="Xamarin.Forms" Version="3.2.0.871581" />
......
......@@ -12,7 +12,7 @@ namespace PulseMusic.ViewModels
{
private Song _song;
private string _title;
private Countdown _countdown;
//private Countdown _countdown;
private TimeSpan _startTime;
private TimeSpan _remainTime;
private bool _isPlaying;
......@@ -22,7 +22,7 @@ namespace PulseMusic.ViewModels
public PlayerViewModel()
{
_countdown = new Countdown();
//_countdown = new Countdown();
_playbackService = DependencyService.Get<PlaybackService>();
IsPlaying = true;
......@@ -77,31 +77,28 @@ namespace PulseMusic.ViewModels
set => SetProperty(ref _icon, value);
}
private long _length;
public override Task LoadAsync()
{
_playbackService.Init();
MessagingCenter.Subscribe<string, float>(MessengerKeys.App, MessengerKeys.Progress, (app, progress) =>
MessagingCenter.Subscribe<string, float>(MessengerKeys.App, MessengerKeys.Position, (app, position) => Progress = position);
MessagingCenter.Subscribe<string, long>(MessengerKeys.App, MessengerKeys.Time, (app, time) =>
{
Progress = progress;
});
MessagingCenter.Subscribe<string, long>(MessengerKeys.App, MessengerKeys.Length, (app, length) =>
{
_countdown.EndTime = TimeSpan.FromTicks(length);
RemainTime = TimeSpan.FromMilliseconds((double)new decimal(_length - time));
StartTime = TimeSpan.FromMilliseconds((double)new decimal(time));
});
MessagingCenter.Subscribe<string, long>(MessengerKeys.App, MessengerKeys.Length, (app, length) => _length = length);
MessagingCenter.Subscribe<string>(MessengerKeys.App, MessengerKeys.EndReached, app => EndReached());
LoadSong();
_countdown.StartTime = TimeSpan.Zero;
_countdown.IsRunning = true;
_countdown.Start();
_playbackService.Play(true);
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, IsPlaying);
return base.LoadAsync();
}
......@@ -128,10 +125,9 @@ namespace PulseMusic.ViewModels
void Play()
{
_countdown.IsRunning = !_countdown.IsRunning;
IsPlaying = _countdown.IsRunning;
IsPlaying = !IsPlaying;
if (_countdown.IsRunning)
if (IsPlaying)
{
Icon = "pause";
}
......@@ -140,12 +136,12 @@ namespace PulseMusic.ViewModels
Icon = "play";
}
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, IsPlaying);
_playbackService.Play(IsPlaying);
}
void Rewind()
{
Debug.WriteLine("Rewind");
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Rewind);
}
void Previous()
......@@ -160,7 +156,7 @@ namespace PulseMusic.ViewModels
void Forward()
{
Debug.WriteLine("Rewind");
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Forward);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment