Skip to content

Commit

Permalink
iOS: Fixed player position change which was sometimes wrong because t…
Browse files Browse the repository at this point in the history
…he touches ended event sometimes returns a different value than touches moved.

Related to issue #405.
  • Loading branch information
ycastonguay committed Jul 12, 2013
1 parent f73655e commit ca8495b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
52 changes: 26 additions & 26 deletions MPfm/MPfm.MVP/Presenters/PlayerPresenter.cs
Expand Up @@ -133,32 +133,6 @@ void HandleTimerRefreshSongPositionElapsed(object sender, ElapsedEventArgs e)
// Send changes to view
View.RefreshPlayerPosition(entity);
}

private PlayerPositionEntity RequestPosition(float positionPercentage)
{
try
{
// 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 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("An error occured while calculating the player position: " + ex.Message);
View.PlayerError(ex);
}
return new PlayerPositionEntity();
}

/// <summary>
/// Starts playback.
Expand Down Expand Up @@ -307,6 +281,32 @@ public void Previous()
public void RepeatType()
{
}

private PlayerPositionEntity RequestPosition(float positionPercentage)
{
try
{
// 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("An error occured while calculating the player position: " + ex.Message);
View.PlayerError(ex);
}
return new PlayerPositionEntity();
}

public void SetPosition(float percentage)
{
Expand Down
4 changes: 3 additions & 1 deletion MPfm/MPfm.Player/Player.cs
Expand Up @@ -1380,7 +1380,9 @@ public void SetPosition(double percentage)
throw new Exception("Error: The playlist has no current item!");

// Calculate position in bytes
long positionBytes = (long)Math.Ceiling((double)Playlist.CurrentItem.LengthBytes * (percentage / 100));
//long positionBytes = (long)Math.Ceiling((double)Playlist.CurrentItem.LengthBytes * (percentage / 100));
//long positionBytes = (long)(positionPercentage * lengthBytes);
long positionBytes = (long)(Playlist.CurrentItem.LengthBytes * (percentage / 100f));
SetPosition(positionBytes);
}

Expand Down
15 changes: 9 additions & 6 deletions MPfm/MPfm.iOS/Classes/Controllers/PlayerViewController.cs
Expand Up @@ -49,6 +49,7 @@ public partial class PlayerViewController : BaseViewController, IPlayerView
MPVolumeView _volumeView;
UIBarButtonItem _btnBack;
PlayerMetadataViewController _playerMetadataViewController;
float _lastSliderPositionValue = 0;

public PlayerViewController(Action<IBaseView> onViewReady)
: base (onViewReady, UserInterfaceIdiomIsPhone ? "PlayerViewController_iPhone" : "PlayerViewController_iPad", null)
Expand Down Expand Up @@ -146,14 +147,12 @@ public override void ViewDidLoad()
};
sliderPosition.TouchesMovedEvent += (sender, e) => {
_isPositionChanging = true;
//Console.WriteLine("Position: Setting value to " + position.ToString());
_lastSliderPositionValue = sliderPosition.Value / 100;
PlayerPositionEntity entity = OnPlayerRequestPosition(sliderPosition.Value / 10000);
lblPosition.Text = entity.Position;
scrollViewWaveForm.SetSecondaryPosition(entity.PositionBytes);
};
sliderPosition.TouchesEndedEvent += (sender, e) => {
//Console.WriteLine("Position: Setting value to " + position.ToString());
UIView.Animate(0.2f, () => {
float offset = 42;
viewPosition.Frame = new RectangleF(viewPosition.Frame.X, viewPosition.Frame.Y, viewPosition.Frame.Width, viewPosition.Frame.Height - offset);
Expand All @@ -164,7 +163,10 @@ public override void ViewDidLoad()
lblSlideMessage.Alpha = 0;
lblScrubbingType.Alpha = 0;
});
OnPlayerSetPosition(sliderPosition.Value / 100);
// Sometimes the position from TouchesEnded is different than the last position returned by TouchesMoved.
// This gives the user the impression that the selected position is different.
OnPlayerSetPosition(_lastSliderPositionValue);
scrollViewWaveForm.ShowSecondaryPosition(false);
_isPositionChanging = false;
};
Expand Down Expand Up @@ -282,7 +284,8 @@ public void AddScrollView(UIViewController viewController)
{
viewController.View.Frame = new RectangleF(scrollSubviewsLength * scrollView.Frame.Width, 0, scrollView.Frame.Width, scrollView.Frame.Height);
scrollView.AddSubview(viewController.View);
pageControl.Pages = scrollSubviewsLength;
//pageControl.Pages = scrollSubviewsLength;
pageControl.Pages = 5;
scrollView.ContentSize = new SizeF(scrollSubviewsLength * scrollView.Frame.Width, scrollView.Frame.Height);
}
else
Expand All @@ -299,7 +302,7 @@ public void AddScrollView(UIViewController viewController)
viewController.View.Frame = new RectangleF(2 * scrollView.Frame.Width, scrollView.Frame.Height / 2, scrollView.Frame.Width, scrollView.Frame.Height / 2);

scrollView.AddSubview(viewController.View);
pageControl.Pages = 3;
//pageControl.Pages = 3;
scrollView.ContentSize = new SizeF(3 * scrollView.Frame.Width, scrollView.Frame.Height);
}
}
Expand Down
6 changes: 4 additions & 2 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormView.cs
Expand Up @@ -109,8 +109,9 @@ public long SecondaryPosition
if(_isLoading)
return;

// Invalidate cursor
RectangleF rectCursor = new RectangleF(_secondaryCursorX - 15, 0, 30, Frame.Height);
// Invalidate cursor. TODO: When the cursor is moving quickly, it dispappears because of the invalidation.
// Maybe the cursor shouldn't be rendered, but instead be a simple rect over this control?
RectangleF rectCursor = new RectangleF(_secondaryCursorX - 25, 0, 50, Frame.Height);
SetNeedsDisplayInRect(rectCursor);
}
}
Expand Down Expand Up @@ -328,6 +329,7 @@ private void DrawWaveFormBitmap(CGContext context)
{
float secondaryPositionPercentage = (float)SecondaryPosition / (float)Length;
_secondaryCursorX = secondaryPositionPercentage * Bounds.Width;
_secondaryCursorX = (float)Math.Round(_secondaryCursorX * 2) / 2; // Round to 0.5

// Draw cursor line
context.SetStrokeColor(new CGColor(1, 1, 1, 1));
Expand Down

0 comments on commit ca8495b

Please sign in to comment.