Commit 6b38ab89 authored by Martin Finkel's avatar Martin Finkel

Bind progress with circle, fix animation, play from internet stream

parent 0c4b88a3
......@@ -135,10 +135,5 @@
<ItemGroup>
<AndroidResource Include="Resources\drawable\shuffle.png" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\Imagine Dragons - Radioactive.mp3">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AndroidAsset>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -4,5 +4,8 @@
{
public const string App = "PulseMusic";
public const string Play = "Play";
public const string Progress = "Progress";
public const string Length = "Length";
public const string EndReached = "EndReached";
}
}
using LibVLCSharp.Shared;
using PulseMusic.ViewModels;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace PulseMusic
......@@ -13,7 +7,6 @@ namespace PulseMusic
{
LibVLC _libVLC;
MediaPlayer _mp;
const string SONG_NAME = "Imagine Dragons - Radioactive.mp3";
public PlaybackService()
{
......@@ -23,19 +16,32 @@ namespace PulseMusic
_mp = new MediaPlayer(_libVLC);
}
public void Init()
{
_mp.Media = new Media(_libVLC, "https://archive.org/download/ImagineDragons_201410/imagine%20dragons.mp4", Media.FromType.FromLocation);
async Task<Stream> SongStream() => await FileSystem.OpenAppPackageFileAsync(SONG_NAME);
_mp.Media.AddOption(":no-video");
public async void Init()
{
_mp.Media = new Media(_libVLC, await SongStream(), ":no-video");
_mp.PositionChanged += PositionChanged;
_mp.LengthChanged += LengthChanged;
_mp.EndReached += EndReached;
MessagingCenter.Subscribe<PlayerViewModel, bool>(this, MessengerKeys.Play, (vm, play) =>
MessagingCenter.Subscribe<string, bool>(MessengerKeys.App, MessengerKeys.Play, (vm, play) =>
{
if (play)
_mp.Play();
else _mp.Pause();
});
}
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);
}
}
......@@ -80,59 +80,50 @@ namespace PulseMusic.ViewModels
public override Task LoadAsync()
{
_playbackService.Init();
MessagingCenter.Subscribe<string, float>(MessengerKeys.App, MessengerKeys.Progress, (app, progress) =>
{
Progress = progress;
});
MessagingCenter.Subscribe<string, long>(MessengerKeys.App, MessengerKeys.Length, (app, length) =>
{
_countdown.EndTime = TimeSpan.FromTicks(length);
});
MessagingCenter.Subscribe<string>(MessengerKeys.App, MessengerKeys.EndReached, app => EndReached());
LoadSong();
_countdown.StartTime = TimeSpan.Zero;
_countdown.EndTime = Song.Duration;
_countdown.IsRunning = true;
_countdown.Start();
_countdown.Ticked += OnCountdownTicked;
_countdown.Completed += OnCountdownCompleted;
MessagingCenter.Send(this, MessengerKeys.Play, IsPlaying);
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, IsPlaying);
return base.LoadAsync();
}
public override Task UnloadAsync()
{
_countdown.Ticked -= OnCountdownTicked;
_countdown.Completed -= OnCountdownCompleted;
return base.UnloadAsync();
}
void LoadSong()
{
Song = new Song
{
Title = "Imagine Dragons",
Artist = "Radioactive",
Cover = "imagine_dragons",
Duration = new TimeSpan(0, 3, 5)
};
Title = string.Format("{0} - {1}", Song.Artist, Song.Title);
}
void OnCountdownTicked()
{
StartTime = _countdown.StartTime;
RemainTime = _countdown.RemainTime;
var totalSeconds = Song.Duration.TotalSeconds;
var remainSeconds = _countdown.RemainTime.TotalSeconds;
Progress = remainSeconds / totalSeconds;
}
void OnCountdownCompleted()
void EndReached()
{
Progress = 0;
IsPlaying = false;
MessagingCenter.Send(this, MessengerKeys.Play, IsPlaying);
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, IsPlaying);
}
void Play()
......@@ -149,7 +140,7 @@ namespace PulseMusic.ViewModels
Icon = "play";
}
MessagingCenter.Send(this, MessengerKeys.Play, IsPlaying);
MessagingCenter.Send(MessengerKeys.App, MessengerKeys.Play, IsPlaying);
}
void Rewind()
......
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