Skip to content

Commit

Permalink
Android: The player position can now be changed in the lock screen. F…
Browse files Browse the repository at this point in the history
…ixed bugs in lock screen.

Related to issue #406.
  • Loading branch information
ycastonguay committed Aug 28, 2013
1 parent d47b5e2 commit 9b6d801
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
33 changes: 31 additions & 2 deletions MPfm/MPfm.Android/Classes/Activities/LockScreenActivity.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -234,14 +236,41 @@ private void SeekBarOnStopTrackingTouch(object sender, SeekBar.StopTrackingTouch
{
Console.WriteLine("SeekBarOnStopTrackingTouch progress: {0}", _seekBar.Progress);
//OnPlayerSetPosition(_seekBar.Progress);
_messengerHub.PublishAsync<PlayerSetPositionMessage>(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)
Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.MVP/MPfm.MVP.Android.csproj
Expand Up @@ -129,6 +129,7 @@
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Messages\ConnectionStatusChangedMessage.cs" />
<Compile Include="Messages\ApplicationCloseMessage.cs" />
<Compile Include="Messages\PlayerSetPositionMessage.cs" />
<Compile Include="Messages\MobileLibraryBrowserItemClickedMessage.cs" />
<Compile Include="Navigation\MobileNavigationManager.cs" />
<Compile Include="Presenters\AudioPreferencesPresenter.cs" />
Expand Down
35 changes: 35 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.

using TinyMessenger;

namespace MPfm.MVP.Messages
{
/// <summary>
/// Message used to change the player position.
/// </summary>
public class PlayerSetPositionMessage : TinyMessageBase
{
public float Position { get; set; }

public PlayerSetPositionMessage(object sender, float position)
: base(sender)
{
this.Position = position;
}
}
}
10 changes: 6 additions & 4 deletions MPfm/MPfm.MVP/Services/PlayerService.cs
Expand Up @@ -71,6 +71,7 @@ public void Initialize(Device device, int sampleRate, int bufferSize, int update
_player.OnAudioInterrupted += HandleOnAudioInterrupted;
_player.OnBPMDetected += HandleOnBPMDetected;
_messengerHub.Subscribe<PlayerCommandMessage>(PlayerCommandMessageReceived);
_messengerHub.Subscribe<PlayerSetPositionMessage>(PlayerSetPositionMessageReceived);
IsInitialized = true;
}

Expand Down Expand Up @@ -98,10 +99,11 @@ private void UpdatePlayerStatus(PlayerStatusType status)
});
}

/// <summary>
/// Receives player commands via TinyMessenger.
/// </summary>
/// <param name="m">Message</param>
public void PlayerSetPositionMessageReceived(PlayerSetPositionMessage m)
{
_player.SetPosition(m.Position);
}

public void PlayerCommandMessageReceived(PlayerCommandMessage m)
{
switch (m.Command)
Expand Down

0 comments on commit 9b6d801

Please sign in to comment.