using NetCoreAudio.Interfaces;
using NetCoreAudio.Players;
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace NetCoreAudio
{
public class Player : IPlayer
{
private readonly IPlayer _internalPlayer;
///
/// Internally, sets Playing flag to false. Additional handlers can be attached to it to handle any custom logic.
///
public event EventHandler PlaybackFinished;
///
/// Indicates that the audio is currently playing.
///
public bool Playing => _internalPlayer.Playing;
///
/// Indicates that the audio playback is currently paused.
///
public bool Paused => _internalPlayer.Paused;
public Player()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_internalPlayer = new WindowsPlayer();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_internalPlayer = new LinuxPlayer();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
_internalPlayer = new MacPlayer();
else
throw new Exception("No implementation exist for the current OS");
_internalPlayer.PlaybackFinished += OnPlaybackFinished;
}
///
/// Will stop any current playback and will start playing the specified audio file. The fileName parameter can be an absolute path or a path relative to the directory where the library is located. Sets Playing flag to true. Sets Paused flag to false.
///
///
///
public async Task Play(string fileName)
{
await _internalPlayer.Play(fileName);
}
///
/// Pauses any ongong playback. Sets Paused flag to true. Doesn't modify Playing flag.
///
///
public async Task Pause()
{
await _internalPlayer.Pause();
}
///
/// Resumes any paused playback. Sets Paused flag to false. Doesn't modify Playing flag.
///
///
public async Task Resume()
{
await _internalPlayer.Resume();
}
///
/// Stops any current playback and clears the buffer. Sets Playing and Paused flags to false.
///
///
public async Task Stop(bool force = false)
{
await _internalPlayer.Stop(force);
}
private void OnPlaybackFinished(object sender, EventArgs e)
{
PlaybackFinished?.Invoke(this, e);
}
///
/// Sets the playing volume as percent
///
///
public async Task SetVolume(byte percent)
{
await _internalPlayer.SetVolume(percent);
}
public void Dispose()
{
_internalPlayer.Dispose();
}
}
}