Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
The Player is not longer public in PlayerService.
Added a bunch of methods in PlayerService to front the IPlayer class.

Related to issue #396.
  • Loading branch information
ycastonguay committed Feb 24, 2013
1 parent 1e6c003 commit bc2ffad
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 131 deletions.
6 changes: 3 additions & 3 deletions MPfm/MPfm.MVP/Presenters/EffectsPresenter.cs
Expand Up @@ -49,13 +49,13 @@ public EffectsPresenter(IPlayerService playerService)
public void SetEQParam(int index, float value)
{
// Set EQ and update UI
playerService.Player.UpdateEQBand(index, value, true);
playerService.UpdateEQBand(index, value, true);
View.UpdateFader(index, value);
}

public void BypassEQ()
{
playerService.Player.BypassEQ();
playerService.BypassEQ();
}

public void AutoLevel()
Expand All @@ -64,7 +64,7 @@ public void AutoLevel()

public void Reset()
{
playerService.Player.ResetEQ();
playerService.ResetEQ();
for (int a = 0; a < 18; a++)
View.UpdateFader(a, 0);
}
Expand Down
11 changes: 9 additions & 2 deletions MPfm/MPfm.MVP/Presenters/PlayerMetadataPresenter.cs
Expand Up @@ -18,6 +18,8 @@
using MPfm.MVP.Navigation;
using MPfm.MVP.Presenters.Interfaces;
using MPfm.MVP.Views;
using MPfm.MVP.Services.Interfaces;
using TinyMessenger;

namespace MPfm.MVP.Presenters
{
Expand All @@ -26,9 +28,14 @@ namespace MPfm.MVP.Presenters
/// </summary>
public class PlayerMetadataPresenter : BasePresenter<IPlayerMetadataView>, IPlayerMetadataPresenter
{
public PlayerMetadataPresenter()
ITinyMessengerHub messageHub;
IPlayerService playerService;

public PlayerMetadataPresenter(ITinyMessengerHub messageHub, IPlayerService playerService)
{
}
this.playerService = playerService;
this.messageHub = messageHub;
}

public override void BindView(IPlayerMetadataView view)
{
Expand Down
151 changes: 39 additions & 112 deletions MPfm/MPfm.MVP/Presenters/PlayerPresenter.cs
Expand Up @@ -44,11 +44,6 @@ public class PlayerPresenter : BasePresenter<IPlayerView>, IPlayerPresenter
readonly IAudioFileCacheService audioFileCacheService;
readonly ITinyMessengerHub messageHub;

#region Constructor and Dispose

/// <summary>
/// Initializes a new instance of the <see cref="MPfm.UI.PlayerPresenter"/> class.
/// </summary>
public PlayerPresenter(ITinyMessengerHub messageHub, IPlayerService playerService, IAudioFileCacheService audioFileCacheService)
{
// Set properties
Expand All @@ -67,7 +62,7 @@ public PlayerPresenter(ITinyMessengerHub messageHub, IPlayerService playerServic
Id = -1
};
playerService.Initialize(device, 44100, 5000, 100);
playerService.Player.OnPlaylistIndexChanged += HandlePlayerOnPlaylistIndexChanged;
//playerService.OnPlaylistIndexChanged += HandlePlayerOnPlaylistIndexChanged;

// Subscribe to events
messageHub.Subscribe<LibraryBrowserItemDoubleClickedMessage>((LibraryBrowserItemDoubleClickedMessage m) => {
Expand Down Expand Up @@ -104,33 +99,21 @@ public override void BindView(IPlayerView view)
view.OnPlayerSetVolume = SetVolume;
}

#endregion

/// <summary>
/// Handles the timer update position elapsed.
/// </summary>
/// <param name='sender'>
/// Sender.
/// </param>
/// <param name='e'>
/// E.
/// </param>
void HandleTimerRefreshSongPositionElapsed(object sender, ElapsedEventArgs e)
{
// Check player
if(playerService.Player.IsSettingPosition)
if(playerService.IsSettingPosition)
return;

int available = playerService.Player.MixerChannel.GetDataAvailable();
int available = playerService.GetDataAvailable();

// Create entity
PlayerPositionEntity entity = new PlayerPositionEntity();
entity.PositionBytes = playerService.Player.GetPosition();
entity.PositionSamples = ConvertAudio.ToPCM(entity.PositionBytes, (uint)playerService.Player.Playlist.CurrentItem.AudioFile.BitsPerSample, 2);
entity.PositionMS = (int)ConvertAudio.ToMS(entity.PositionSamples, (uint)playerService.Player.Playlist.CurrentItem.AudioFile.SampleRate);
entity.PositionBytes = playerService.GetPosition();
entity.PositionSamples = ConvertAudio.ToPCM(entity.PositionBytes, (uint)playerService.CurrentPlaylistItem.AudioFile.BitsPerSample, 2);
entity.PositionMS = (int)ConvertAudio.ToMS(entity.PositionSamples, (uint)playerService.CurrentPlaylistItem.AudioFile.SampleRate);
entity.Position = available.ToString() + " " + Conversion.MillisecondsToTimeString((ulong)entity.PositionMS);
entity.PositionPercentage = ((float)playerService.Player.GetPosition() / (float)playerService.Player.Playlist.CurrentItem.LengthBytes) * 100;

entity.PositionPercentage = ((float)playerService.GetPosition() / (float)playerService.CurrentPlaylistItem.LengthBytes) * 100;

// Send changes to view
View.RefreshPlayerPosition(entity);
Expand All @@ -145,7 +128,7 @@ void HandleTimerRefreshSongPositionElapsed(object sender, ElapsedEventArgs e)
protected void HandlePlayerOnPlaylistIndexChanged(PlayerPlaylistIndexChangedData data)
{
// Refresh song information
RefreshSongInformation(playerService.Player.Playlist.CurrentItem.AudioFile);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
}

/// <summary>
Expand All @@ -155,16 +138,8 @@ public void Play()
{
try
{
// Start playback
Tracing.Log("PlayerPresenter.Play -- Starting playback...");
playerService.Player.Play();

// Refresh song information
Tracing.Log("PlayerPresenter.Play -- Refreshing song information...");
RefreshSongInformation(playerService.Player.Playlist.CurrentItem.AudioFile);

// Start timer
Tracing.Log("PlayerPresenter.Play -- Starting timer...");
playerService.Play();
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
timerRefreshSongPosition.Start();
}
catch(Exception ex)
Expand All @@ -181,15 +156,9 @@ public void Play(IEnumerable<AudioFile> audioFiles)
{
try
{
// Replace playlist
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>) -- Clearing playlist...");
playerService.Player.Playlist.Clear();
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>) -- Adding items...");
playerService.Player.Playlist.AddItems(audioFiles.ToList());

// Start playback
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>) -- Starting playback...");
Play();
playerService.Play(audioFiles);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
timerRefreshSongPosition.Start();
}
catch(Exception ex)
{
Expand All @@ -205,15 +174,9 @@ public void Play(IEnumerable<string> filePaths)
{
try
{
// Replace playlist
Tracing.Log("PlayerPresenter.Play(IEnumerable<string>) -- Clearing playlist...");
playerService.Player.Playlist.Clear();
Tracing.Log("PlayerPresenter.Play(IEnumerable<string>) -- Adding items...");
playerService.Player.Playlist.AddItems(filePaths.ToList());

// Start playback
Tracing.Log("PlayerPresenter.Play(IEnumerable<string>) -- Starting playback...");
Play();
playerService.Play(filePaths);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
timerRefreshSongPosition.Start();
}
catch(Exception ex)
{
Expand All @@ -230,29 +193,9 @@ public void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath)
{
try
{
// Replace playlist
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Clearing playlist...");
playerService.Player.Playlist.Clear();
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Adding items...");
if(audioFiles == null)
{
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Adding items: audioFiles == null");
List<AudioFile> listAudioFiles = audioFiles.ToList();
playerService.Player.Playlist.AddItems(listAudioFiles); // simulate bug
}
else
{
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Adding items...");
List<AudioFile> listAudioFiles = audioFiles.ToList();
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Adding items (count = " + listAudioFiles.Count + "...");
playerService.Player.Playlist.AddItems(listAudioFiles);
}
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Skipping to item " + startAudioFilePath + " in playlist...");
playerService.Player.Playlist.GoTo(startAudioFilePath);

// Start playback
Tracing.Log("PlayerPresenter.Play(IEnumerable<AudioFile>, string) -- Starting playback...");
Play();
playerService.Play(audioFiles, startAudioFilePath);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
timerRefreshSongPosition.Start();
}
catch(Exception ex)
{
Expand All @@ -267,22 +210,18 @@ public void Stop()
{
try
{
// Check if the player is playing
if(playerService.Player.IsPlaying)
{
// Stop timer
Tracing.Log("PlayerPresenter.Stop -- Stopping timer...");
timerRefreshSongPosition.Stop();

// Stop player
Tracing.Log("PlayerPresenter.Stop -- Stopping playback...");
playerService.Player.Stop();

// Refresh view with empty information
Tracing.Log("PlayerPresenter.Stop -- Refresh song information and position with empty entity...");
View.RefreshSongInformation(null);
View.RefreshPlayerPosition(new PlayerPositionEntity());
}
// Stop timer
Tracing.Log("PlayerPresenter.Stop -- Stopping timer...");
timerRefreshSongPosition.Stop();

// Stop player
Tracing.Log("PlayerPresenter.Stop -- Stopping playback...");
playerService.Stop();

// Refresh view with empty information
Tracing.Log("PlayerPresenter.Stop -- Refresh song information and position with empty entity...");
View.RefreshSongInformation(null);
View.RefreshPlayerPosition(new PlayerPositionEntity());
}
catch(Exception ex)
{
Expand All @@ -297,13 +236,7 @@ public void Pause()
{
try
{
// Check if the player is playing
if(playerService.Player.IsPlaying)
{
// Pause player
Tracing.Log("PlayerPresenter.Stop -- Pausing playback...");
playerService.Player.Pause();
}
playerService.Pause();
}
catch(Exception ex)
{
Expand All @@ -320,11 +253,11 @@ public void Next()
{
// Go to next song
Tracing.Log("PlayerPresenter.Next -- Skipping to next item in playlist...");
playerService.Player.Next();
playerService.Next();

// Refresh controls
Tracing.Log("PlayerPresenter.Next -- Refreshing song information...");
RefreshSongInformation(playerService.Player.Playlist.CurrentItem.AudioFile);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
}
catch(Exception ex)
{
Expand All @@ -341,10 +274,10 @@ public void Previous()
{
// Go to previous song
Tracing.Log("PlayerPresenter.Previous -- Skipping to previous item in playlist...");
playerService.Player.Previous();
playerService.Previous();

// Refresh controls
RefreshSongInformation(playerService.Player.Playlist.CurrentItem.AudioFile);
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
Tracing.Log("PlayerPresenter.Previous -- Refreshing song information...");
}
catch(Exception ex)
Expand All @@ -360,12 +293,6 @@ public void RepeatType()
{
}

/// <summary>
/// Refreshes the song information on the main view.
/// </summary>
/// <param name='audioFile'>
/// Audio file.
/// </param>
private void RefreshSongInformation(AudioFile audioFile)
{
View.RefreshSongInformation(audioFile);
Expand All @@ -378,7 +305,7 @@ public void SetPosition(float percentage)
// Set position
Tracing.Log("PlayerPresenter.SetPosition -- Setting position to " + percentage.ToString("0.00") + "%");
timerRefreshSongPosition.Stop();
playerService.Player.SetPosition((double)percentage);
playerService.SetPosition((double)percentage);
timerRefreshSongPosition.Start();
}
catch(Exception ex)
Expand All @@ -393,7 +320,7 @@ public void SetVolume(float volume)
{
// Set volume and refresh UI
Tracing.Log("PlayerPresenter.SetVolume -- Setting volume to " + volume.ToString("0.00") + "%");
playerService.Player.Volume = volume / 100;
playerService.SetVolume(volume / 100);
View.RefreshPlayerVolume(new PlayerVolumeEntity(){
Volume = volume,
VolumeString = volume.ToString("0") + " %"
Expand All @@ -415,7 +342,7 @@ public void SetTimeShifting(float timeShifting)

// Set time shifting and refresh UI
Tracing.Log("PlayerPresenter.SetTimeShifting -- Setting time shifting to " + timeShifting.ToString("0.00") + "%");
playerService.Player.TimeShifting = result;
playerService.SetTimeShifting(result);
View.RefreshPlayerTimeShifting(new PlayerTimeShiftingEntity(){
TimeShifting = timeShifting,
TimeShiftingString = timeShifting.ToString("0") + " %"
Expand Down
29 changes: 28 additions & 1 deletion MPfm/MPfm.MVP/Services/Interfaces/IPlayerService.cs
Expand Up @@ -15,8 +15,11 @@
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using MPfm.Player;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.Bass.Net;
using MPfm.Sound.Playlists;

namespace MPfm.MVP.Services.Interfaces
{
Expand All @@ -25,9 +28,33 @@ namespace MPfm.MVP.Services.Interfaces
/// </summary>
public interface IPlayerService
{
IPlayer Player { get; }
bool IsSettingPosition { get; }
PlaylistItem CurrentPlaylistItem { get; }
float Volume { get; }

void Initialize(Device device, int sampleRate, int bufferSize, int updatePeriod);
void Dispose();

void Play();
void Play(IEnumerable<AudioFile> audioFiles);
void Play(IEnumerable<string> filePaths);
void Play(IEnumerable<AudioFile> audioFiles, string startAudioFilePath);
void Stop();
void Pause();
void Next();
void Previous();
void RepeatType();

int GetDataAvailable();
long GetPosition();

void SetPosition(double percentage);
void SetPosition(long bytes);
void SetVolume(float volume);
void SetTimeShifting(float timeShifting);

void BypassEQ();
void ResetEQ();
void UpdateEQBand(int band, float gain, bool setCurrentEQPresetValue);
}
}

0 comments on commit bc2ffad

Please sign in to comment.