From 9b6d80127f77421954721f7e843f59aa77c5d827 Mon Sep 17 00:00:00 2001 From: ycastonguay Date: Wed, 28 Aug 2013 01:11:39 -0400 Subject: [PATCH] Android: The player position can now be changed in the lock screen. Fixed bugs in lock screen. Related to issue #406. --- .../Classes/Activities/LockScreenActivity.cs | 33 +++++++++++++++-- MPfm/MPfm.MVP/MPfm.MVP.Android.csproj | 1 + .../Messages/PlayerSetPositionMessage.cs | 35 +++++++++++++++++++ MPfm/MPfm.MVP/Services/PlayerService.cs | 10 +++--- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 MPfm/MPfm.MVP/Messages/PlayerSetPositionMessage.cs diff --git a/MPfm/MPfm.Android/Classes/Activities/LockScreenActivity.cs b/MPfm/MPfm.Android/Classes/Activities/LockScreenActivity.cs index 54364515..0ee09c9e 100644 --- a/MPfm/MPfm.Android/Classes/Activities/LockScreenActivity.cs +++ b/MPfm/MPfm.Android/Classes/Activities/LockScreenActivity.cs @@ -26,12 +26,14 @@ using Android.Widget; using Java.Lang; using MPfm.Android.Classes.Cache; +using MPfm.Core; using MPfm.MVP.Bootstrap; using MPfm.MVP.Messages; using MPfm.MVP.Models; using MPfm.MVP.Services.Interfaces; using MPfm.Sound.AudioFiles; using TinyMessenger; +using Exception = System.Exception; namespace MPfm.Android { @@ -234,14 +236,41 @@ private void SeekBarOnStopTrackingTouch(object sender, SeekBar.StopTrackingTouch { Console.WriteLine("SeekBarOnStopTrackingTouch progress: {0}", _seekBar.Progress); //OnPlayerSetPosition(_seekBar.Progress); + _messengerHub.PublishAsync(new PlayerSetPositionMessage(this, _seekBar.Progress / 100f)); _isPositionChanging = false; } private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e) { Console.WriteLine("SeekBarOnProgressChanged"); - //PlayerPositionEntity entity = OnPlayerRequestPosition((float)_seekBar.Progress / 100f); - //_lblPosition.Text = entity.Position; + PlayerPositionEntity entity = RequestPosition((float)_seekBar.Progress / 10000f); + _lblPosition.Text = entity.Position; + } + + private PlayerPositionEntity RequestPosition(float positionPercentage) + { + try + { + Console.WriteLine("LockScreenActivity - RequestPosition - positionPercentage: {0}", positionPercentage); + // Calculate new position from 0.0f/1.0f scale + long lengthBytes = _playerService.CurrentPlaylistItem.LengthBytes; + var audioFile = _playerService.CurrentPlaylistItem.AudioFile; + long positionBytes = (long)(positionPercentage * lengthBytes); + //long positionBytes = (long)Math.Ceiling((double)Playlist.CurrentItem.LengthBytes * (percentage / 100)); + long positionSamples = ConvertAudio.ToPCM(positionBytes, (uint)audioFile.BitsPerSample, audioFile.AudioChannels); + int positionMS = (int)ConvertAudio.ToMS(positionSamples, (uint)audioFile.SampleRate); + string positionString = Conversion.MillisecondsToTimeString((ulong)positionMS); + PlayerPositionEntity entity = new PlayerPositionEntity(); + entity.Position = positionString; + entity.PositionBytes = positionBytes; + entity.PositionSamples = (uint)positionSamples; + return entity; + } + catch (Exception ex) + { + Console.WriteLine("LockScreenActivity - An error occured while calculating the player position: " + ex.Message); + } + return new PlayerPositionEntity(); } private void GetAlbumArt(AudioFile audioFile) diff --git a/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj b/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj index 008b34a3..2e4c2cc8 100644 --- a/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj +++ b/MPfm/MPfm.MVP/MPfm.MVP.Android.csproj @@ -129,6 +129,7 @@ + diff --git a/MPfm/MPfm.MVP/Messages/PlayerSetPositionMessage.cs b/MPfm/MPfm.MVP/Messages/PlayerSetPositionMessage.cs new file mode 100644 index 00000000..bbe3c7cb --- /dev/null +++ b/MPfm/MPfm.MVP/Messages/PlayerSetPositionMessage.cs @@ -0,0 +1,35 @@ +// Copyright © 2011-2013 Yanick Castonguay +// +// This file is part of MPfm. +// +// MPfm is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// MPfm is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with MPfm. If not, see . + +using TinyMessenger; + +namespace MPfm.MVP.Messages +{ + /// + /// Message used to change the player position. + /// + public class PlayerSetPositionMessage : TinyMessageBase + { + public float Position { get; set; } + + public PlayerSetPositionMessage(object sender, float position) + : base(sender) + { + this.Position = position; + } + } +} diff --git a/MPfm/MPfm.MVP/Services/PlayerService.cs b/MPfm/MPfm.MVP/Services/PlayerService.cs index bd9a4d83..c8d8e624 100644 --- a/MPfm/MPfm.MVP/Services/PlayerService.cs +++ b/MPfm/MPfm.MVP/Services/PlayerService.cs @@ -71,6 +71,7 @@ public void Initialize(Device device, int sampleRate, int bufferSize, int update _player.OnAudioInterrupted += HandleOnAudioInterrupted; _player.OnBPMDetected += HandleOnBPMDetected; _messengerHub.Subscribe(PlayerCommandMessageReceived); + _messengerHub.Subscribe(PlayerSetPositionMessageReceived); IsInitialized = true; } @@ -98,10 +99,11 @@ private void UpdatePlayerStatus(PlayerStatusType status) }); } - /// - /// Receives player commands via TinyMessenger. - /// - /// Message + public void PlayerSetPositionMessageReceived(PlayerSetPositionMessage m) + { + _player.SetPosition(m.Position); + } + public void PlayerCommandMessageReceived(PlayerCommandMessage m) { switch (m.Command)