Skip to content

Commit

Permalink
Android: New player status types: WaitingToStart and StartPaused. The…
Browse files Browse the repository at this point in the history
… first is the status used when launching a Player view. This allows MobileLibraryBrowserPresenter to pass the playlist directly to the player instead of using view details. This makes Android intents easier to manage. The second one, StartPaused, is used when the app launches and resumes the previous local playlist. We don't want to start the playback automatically.
  • Loading branch information
ycastonguay committed Nov 3, 2013
1 parent a9dee4c commit 6e44d92
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 60 deletions.
70 changes: 40 additions & 30 deletions MPfm/MPfm.MVP/Messages/PlayerStatusType.cs
@@ -1,30 +1,40 @@
// 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/>.

namespace MPfm.MVP.Messages
{
/// <summary>
/// Enumeration of player status types.
/// </summary>
public enum PlayerStatusType
{
Initialized = 0,
Stopped = 1,
Playing = 2,
Paused = 3
}
}
// 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/>.

namespace MPfm.MVP.Messages
{
/// <summary>
/// Enumeration of player status types.
/// </summary>
public enum PlayerStatusType
{
Initialized = 0,
Stopped = 1,
Playing = 2,
Paused = 3,
/// <summary>
/// Used when starting playback as paused.
/// This status tells the presenters to start playback when it will be ready to start updating the UI.
/// </summary>
WaitingToStart = 4,
/// <summary>
/// Used to start the player but pause the playback immediately.
/// Useful when the application starts and resumes the previous sessions; we don't want to force playback on the user.
/// </summary>
StartPaused = 5
}
}
2 changes: 1 addition & 1 deletion MPfm/MPfm.MVP/Navigation/MobileNavigationManager.cs
Expand Up @@ -154,7 +154,7 @@ private void ContinueAfterSplash()
{
Tracing.Log("MobileNavigationManager - Resume playback is available; launching Player activity...");
var audioFiles = audioFileCacheService.AudioFiles.Where(x => x.ArtistName == audioFile.ArtistName && x.AlbumTitle == audioFile.AlbumTitle).ToList();
playerService.Play(audioFiles, audioFile.FilePath);
playerService.Play(audioFiles, audioFile.FilePath, true, true);
// TO DO: Start paused; resume playback when player view is ready.
CreatePlayerView(MobileNavigationTabType.Playlists);
}
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.MVP/Presenters/MobileLibraryBrowserPresenter.cs
Expand Up @@ -332,7 +332,7 @@ private void ItemClick(int index)

// Start playback and start Player view
var audioFiles = _audioFileCacheService.SelectAudioFiles(_query);
_playerService.Play(audioFiles, _items[index].AudioFile != null ? _items[index].AudioFile.FilePath : string.Empty);
_playerService.Play(audioFiles, _items[index].AudioFile != null ? _items[index].AudioFile.FilePath : string.Empty, false, true);
_navigationManager.CreatePlayerView(_tabType);
}
catch(Exception ex)
Expand Down
14 changes: 11 additions & 3 deletions MPfm/MPfm.MVP/Presenters/PlayerPresenter.cs
Expand Up @@ -136,10 +136,18 @@ public override void BindView(IPlayerView view)
View.RefreshMarkers(markers);
}

if (_playerService.Status == PlayerStatusType.WaitingToStart)
{
_playerService.Status = PlayerStatusType.Playing;
//_playerService.Pause();
_timerRefreshSongPosition.Start();
}

View.RefreshPlayerVolume(new PlayerVolumeEntity() {
Volume = 100,
VolumeString = "100%"
});
View.RefreshPlayerStatus(_playerService.Status);
}

public override void ViewDestroyed()
Expand Down Expand Up @@ -211,11 +219,11 @@ private void Play()
/// Starts the playback of a new playlist.
/// </summary>
/// <param name='audioFiles'>List of audio files</param>
public void Play(IEnumerable<AudioFile> audioFiles)
private void Play(IEnumerable<AudioFile> audioFiles)
{
try
{
_playerService.Play(audioFiles);
_playerService.Play(audioFiles, string.Empty, false, false);
_timerRefreshSongPosition.Start();
}
catch(Exception ex)
Expand Down Expand Up @@ -250,7 +258,7 @@ private void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath)
{
try
{
_playerService.Play(audioFiles, startAudioFilePath);
_playerService.Play(audioFiles, startAudioFilePath, false, false);
_timerRefreshSongPosition.Start();
}
catch(Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.MVP/Presenters/StartResumePlaybackPresenter.cs
Expand Up @@ -118,7 +118,7 @@ private void ResumePlayback(CloudDeviceInfo device)
ArtistName = device.ArtistName,
AlbumTitle = device.AlbumTitle
});
_playerService.Play(audioFiles, audioFile != null ? audioFile.FilePath : string.Empty);
_playerService.Play(audioFiles, audioFile != null ? audioFile.FilePath : string.Empty, false, true);
}
}
}
5 changes: 2 additions & 3 deletions MPfm/MPfm.MVP/Services/Interfaces/IPlayerService.cs
Expand Up @@ -53,10 +53,9 @@ public interface IPlayerService
void Initialize(Device device, int sampleRate, int bufferSize, int updatePeriod);
void Dispose();

void Play();
void Play(IEnumerable<AudioFile> audioFiles);
void Play();
void Play(IEnumerable<string> filePaths);
void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath);
void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath, bool startPaused, bool waitingToStart);
void Stop();
void Pause();
void Next();
Expand Down
31 changes: 16 additions & 15 deletions MPfm/MPfm.MVP/Services/PlayerService.cs
Expand Up @@ -280,16 +280,7 @@ private void NotifyPlaylistUpdate()

public void Play()
{
_player.Play();
UpdatePlayerStatus(PlayerStatusType.Playing);
NotifyPlaylistUpdate();
}

public void Play(IEnumerable<AudioFile> audioFiles)
{
_player.Playlist.Clear();
_player.Playlist.AddItems(audioFiles.ToList());
_player.Play();
_player.Play(false);
UpdatePlayerStatus(PlayerStatusType.Playing);
NotifyPlaylistUpdate();
}
Expand All @@ -298,18 +289,28 @@ public void Play(IEnumerable<string> filePaths)
{
_player.Playlist.Clear();
_player.Playlist.AddItems(filePaths.ToList());
_player.Play();
_player.Play(false);
UpdatePlayerStatus(PlayerStatusType.Playing);
NotifyPlaylistUpdate();
}

public void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath)
public void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath, bool startPaused, bool waitingToStart)
{
_player.Playlist.Clear();
_player.Playlist.AddItems(audioFiles.ToList());
_player.Playlist.GoTo(startAudioFilePath);
_player.Play();
UpdatePlayerStatus(PlayerStatusType.Playing);

if(!string.IsNullOrEmpty(startAudioFilePath))
_player.Playlist.GoTo(startAudioFilePath);

_player.Play(startPaused);

if(startPaused)
UpdatePlayerStatus(PlayerStatusType.StartPaused);
else if(waitingToStart)
UpdatePlayerStatus(PlayerStatusType.WaitingToStart);
else
UpdatePlayerStatus(PlayerStatusType.Playing);

NotifyPlaylistUpdate();
}

Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Player/IPlayer.cs
Expand Up @@ -69,7 +69,7 @@ public interface IPlayer
void FreeDevice();
void FreePlugins();

void Play();
void Play(bool startPaused);
void PlayFiles(List<AudioFile> audioFiles);
void PlayFiles(List<string> filePaths);
void Pause();
Expand Down
13 changes: 8 additions & 5 deletions MPfm/MPfm.Player/Player.cs
Expand Up @@ -856,7 +856,7 @@ protected void timerPlayer_Elapsed(object sender, System.Timers.ElapsedEventArgs
/// <summary>
/// Plays the playlist from the current item index.
/// </summary>
public void Play()
public void Play(bool startPaused)
{
try
{
Expand Down Expand Up @@ -1047,7 +1047,7 @@ public void Play()
long length = _playlist.CurrentItem.Channel.GetLength();
SetSyncCallback(length);
_isPlaying = true;
_isPaused = false;
_isPaused = startPaused;

// Only the DirectSound mode needs to start the main channel since it's not in decode mode.
if (_device.DriverType == DriverType.DirectSound)
Expand All @@ -1058,6 +1058,9 @@ public void Play()
// Start playback
Tracing.Log("Player.Play -- Starting DirectSound playback...");
_mixerChannel.Play(false);

if (startPaused)
Base.Pause();
}

// Raise audio file finished event (if an event is subscribed)
Expand Down Expand Up @@ -1127,7 +1130,7 @@ public void PlayFiles(List<AudioFile> audioFiles)

// Start playback from first item
_playlist.First();
Play();
Play(false);
}

/// <summary>
Expand Down Expand Up @@ -1282,7 +1285,7 @@ public void GoTo(int index)
Stop();
Playlist.GoTo(index);
_currentMixPlaylistIndex = index;
Play();
Play(false);
}

/// <summary>
Expand All @@ -1294,7 +1297,7 @@ public void GoTo(Guid playlistItemId)
Stop();
Playlist.GoTo(playlistItemId);
_currentMixPlaylistIndex = Playlist.CurrentItemIndex;
Play();
Play(false);
}

/// <summary>
Expand Down

0 comments on commit 6e44d92

Please sign in to comment.