mirror of
https://github.com/Royce551/FRESHMusicPlayer-Core.git
synced 2025-01-22 19:02:35 -05:00
commit
2a2caa8736
7 changed files with 151 additions and 62 deletions
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FRESHMusicPlayer.Backends
|
||||
{
|
||||
interface IAudioBackend : IDisposable
|
||||
{
|
||||
void Play();
|
||||
void Pause();
|
||||
|
||||
TimeSpan CurrentTime { get; set; }
|
||||
TimeSpan TotalTime { get; }
|
||||
|
||||
float Volume { get; set; }
|
||||
|
||||
event EventHandler<EventArgs> OnPlaybackStopped;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
using NAudio.Wave;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FRESHMusicPlayer.Backends
|
||||
{
|
||||
class NAudioBackend : IAudioBackend
|
||||
{
|
||||
private readonly WaveOutEvent outputDevice;
|
||||
|
||||
public event EventHandler<EventArgs> OnPlaybackStopped;
|
||||
|
||||
public AudioFileReader AudioFile { get; set; }
|
||||
|
||||
public float Volume
|
||||
{
|
||||
get => AudioFile.Volume;
|
||||
set => AudioFile.Volume = value;
|
||||
}
|
||||
public TimeSpan CurrentTime
|
||||
{
|
||||
get => AudioFile.CurrentTime;
|
||||
set => AudioFile.CurrentTime = value;
|
||||
}
|
||||
public TimeSpan TotalTime => AudioFile.TotalTime;
|
||||
|
||||
public NAudioBackend(string file)
|
||||
{
|
||||
if (outputDevice is null)
|
||||
{
|
||||
outputDevice = new WaveOutEvent();
|
||||
outputDevice.PlaybackStopped += (object o, StoppedEventArgs e) =>
|
||||
{
|
||||
OnPlaybackStopped(this, new EventArgs());
|
||||
};
|
||||
}
|
||||
|
||||
if (AudioFile is null)
|
||||
{
|
||||
AudioFile = new AudioFileReader(file);
|
||||
outputDevice.Init(AudioFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
outputDevice.Play();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
outputDevice.Dispose();
|
||||
AudioFile.Dispose();
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
outputDevice.Pause();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,6 +63,8 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Backends\IAudioBackend.cs" />
|
||||
<Compile Include="Backends\NAudioBackend.cs" />
|
||||
<Compile Include="Handlers\DatabaseHandler.cs" />
|
||||
<Compile Include="Handlers\ErrorHandler.cs" />
|
||||
<Compile Include="Handlers\Integrations\Integration.cs" />
|
||||
|
|
|
@ -37,9 +37,11 @@ namespace FRESHMusicPlayer.Handlers
|
|||
var database = ReadSongs();
|
||||
|
||||
database.Add(filepath); // Add the new song in
|
||||
var format = new DatabaseFormat();
|
||||
format.Version = 1;
|
||||
format.Songs = new List<string>();
|
||||
var format = new DatabaseFormat
|
||||
{
|
||||
Version = 1,
|
||||
Songs = new List<string>()
|
||||
};
|
||||
format.Songs = database;
|
||||
|
||||
using (var file = File.CreateText(DatabasePath + "\\database.json"))
|
||||
|
@ -54,9 +56,11 @@ namespace FRESHMusicPlayer.Handlers
|
|||
var database = ReadSongs();
|
||||
|
||||
database.AddRange(filepath);
|
||||
var format = new DatabaseFormat();
|
||||
format.Version = 1;
|
||||
format.Songs = new List<string>();
|
||||
var format = new DatabaseFormat
|
||||
{
|
||||
Version = 1,
|
||||
Songs = new List<string>()
|
||||
};
|
||||
format.Songs = database;
|
||||
|
||||
using (var file = File.CreateText(DatabasePath + "\\database.json"))
|
||||
|
@ -71,9 +75,11 @@ namespace FRESHMusicPlayer.Handlers
|
|||
var database = ReadSongs();
|
||||
|
||||
database.AddRange(filepath);
|
||||
var format = new DatabaseFormat();
|
||||
format.Version = 1;
|
||||
format.Songs = new List<string>();
|
||||
var format = new DatabaseFormat
|
||||
{
|
||||
Version = 1,
|
||||
Songs = new List<string>()
|
||||
};
|
||||
format.Songs = database;
|
||||
|
||||
using (var file = File.CreateText(DatabasePath + "\\database.json"))
|
||||
|
@ -88,9 +94,11 @@ namespace FRESHMusicPlayer.Handlers
|
|||
var database = ReadSongs();
|
||||
|
||||
database.AddRange(filepath);
|
||||
var format = new DatabaseFormat();
|
||||
format.Version = 1;
|
||||
format.Songs = new List<string>();
|
||||
var format = new DatabaseFormat
|
||||
{
|
||||
Version = 1,
|
||||
Songs = new List<string>()
|
||||
};
|
||||
format.Songs = database;
|
||||
|
||||
using (var file = File.CreateText(DatabasePath + "\\database.json"))
|
||||
|
@ -104,10 +112,12 @@ namespace FRESHMusicPlayer.Handlers
|
|||
{
|
||||
var database = ReadSongs();
|
||||
database.Remove(filepath);
|
||||
var format = new DatabaseFormat();
|
||||
format.Version = 1;
|
||||
format.Songs = database;
|
||||
|
||||
var format = new DatabaseFormat
|
||||
{
|
||||
Version = 1,
|
||||
Songs = database
|
||||
};
|
||||
|
||||
|
||||
using (var file = File.CreateText(DatabasePath + "\\database.json"))
|
||||
{
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
using DiscordRPC;
|
||||
using NAudio.Wave;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FRESHMusicPlayer.Handlers;
|
||||
using FRESHMusicPlayer.Utilities;
|
||||
using FRESHMusicPlayer.Backends;
|
||||
|
||||
namespace FRESHMusicPlayer
|
||||
{
|
||||
public class Player
|
||||
{
|
||||
private WaveOutEvent outputDevice;
|
||||
public AudioFileReader AudioFile { get; set; }
|
||||
|
||||
private IAudioBackend currentBackend;
|
||||
|
||||
public bool AvoidNextQueue { get; set; }
|
||||
public DiscordRpcClient Client { get; set; }
|
||||
|
||||
//public int position;
|
||||
public float CurrentVolume { get; set; } = 1;
|
||||
public string FilePath { get; set; } = "";
|
||||
public bool Playing { get; set; }
|
||||
|
@ -90,13 +90,12 @@ namespace FRESHMusicPlayer
|
|||
}
|
||||
|
||||
// Music Playing Controls
|
||||
private void OnPlaybackStopped(object sender, StoppedEventArgs args)
|
||||
private void OnPlaybackStopped(object sender, EventArgs args)
|
||||
{
|
||||
if (!AvoidNextQueue) NextSong();
|
||||
if (!AvoidNextQueue)
|
||||
NextSong();
|
||||
else
|
||||
{
|
||||
AvoidNextQueue = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -105,7 +104,7 @@ namespace FRESHMusicPlayer
|
|||
/// <param name="seconds">The position in to the track to skip in, in seconds.</param>
|
||||
public void RepositionMusic(int seconds)
|
||||
{
|
||||
AudioFile.CurrentTime = TimeSpan.FromSeconds(seconds);
|
||||
currentBackend.CurrentTime = TimeSpan.FromSeconds(seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -115,25 +114,23 @@ namespace FRESHMusicPlayer
|
|||
public void PlayMusic(bool repeat = false)
|
||||
{
|
||||
if (!repeat && Queue.Count != 0)
|
||||
FilePath = Queue[QueuePosition]; // Some functions want to play the same song again
|
||||
FilePath = Queue[QueuePosition];
|
||||
QueuePosition++;
|
||||
|
||||
void PMusic()
|
||||
{
|
||||
if (outputDevice == null)
|
||||
//TODO: Check if FilePath is a file
|
||||
//maybe we can use cd://2 for CD track 2, and anything else we can use the NAudio backend?
|
||||
|
||||
if (true)
|
||||
{
|
||||
outputDevice = new WaveOutEvent();
|
||||
outputDevice.PlaybackStopped += OnPlaybackStopped;
|
||||
currentBackend = new NAudioBackend(FilePath);
|
||||
}
|
||||
|
||||
if (AudioFile == null)
|
||||
{
|
||||
AudioFile = new AudioFileReader(FilePath);
|
||||
outputDevice.Init(AudioFile);
|
||||
}
|
||||
currentBackend.Play();
|
||||
currentBackend.Volume = CurrentVolume;
|
||||
currentBackend.OnPlaybackStopped += OnPlaybackStopped;
|
||||
|
||||
outputDevice.Play();
|
||||
outputDevice.Volume = CurrentVolume;
|
||||
Playing = true;
|
||||
}
|
||||
|
||||
|
@ -178,6 +175,11 @@ namespace FRESHMusicPlayer
|
|||
var args = new PlaybackExceptionEventArgs {Details = "This audio file uses VBR \nor might be corrupt!"};
|
||||
SongException?.Invoke(null, args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var args = new PlaybackExceptionEventArgs {Details = $"{e.Message}\n{e.StackTrace}"};
|
||||
SongException?.Invoke(null, args);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -186,28 +188,18 @@ namespace FRESHMusicPlayer
|
|||
public void StopMusic()
|
||||
{
|
||||
if (!Playing) return;
|
||||
try
|
||||
{
|
||||
outputDevice.Dispose();
|
||||
outputDevice = null;
|
||||
AudioFile?.Dispose();
|
||||
AudioFile = null;
|
||||
//outputDevice.Dispose();
|
||||
//outputDevice = null;
|
||||
//AudioFile?.Dispose();
|
||||
//AudioFile = null;
|
||||
|
||||
currentBackend.Dispose();
|
||||
currentBackend = null;
|
||||
|
||||
Playing = false;
|
||||
Paused = false;
|
||||
//position = 0;
|
||||
SongStopped?.Invoke(null, EventArgs.Empty);
|
||||
}
|
||||
catch (NAudio.MmException) // This is an old workaround from the original FMP days. Shouldn't be needed anymore, but is kept anyway for the sake of
|
||||
{ // stability.
|
||||
Console.WriteLine("Things are breaking!");
|
||||
Console.WriteLine(FilePath);
|
||||
outputDevice?.Dispose();
|
||||
outputDevice = new WaveOutEvent();
|
||||
outputDevice.PlaybackStopped += OnPlaybackStopped; // Does the same initiallization PlayMusic() does.
|
||||
AudioFile = new AudioFileReader(FilePath);
|
||||
outputDevice.Init(AudioFile);
|
||||
PlayMusic(true);
|
||||
}
|
||||
//position = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -215,7 +207,7 @@ namespace FRESHMusicPlayer
|
|||
/// </summary>
|
||||
public void PauseMusic()
|
||||
{
|
||||
if (!Paused) outputDevice?.Pause();
|
||||
if (!Paused) currentBackend?.Pause();
|
||||
Paused = true;
|
||||
} // Pauses the music without completely disposing it
|
||||
|
||||
|
@ -224,7 +216,7 @@ namespace FRESHMusicPlayer
|
|||
/// </summary>
|
||||
public void ResumeMusic()
|
||||
{
|
||||
if (Paused) outputDevice?.Play();
|
||||
if (Paused) currentBackend?.Play();
|
||||
//playing = true;
|
||||
Paused = false;
|
||||
} // Resumes music that has been paused
|
||||
|
@ -235,7 +227,7 @@ namespace FRESHMusicPlayer
|
|||
/// </summary>
|
||||
public void UpdateSettings()
|
||||
{
|
||||
outputDevice.Volume = CurrentVolume;
|
||||
currentBackend.Volume = CurrentVolume;
|
||||
}
|
||||
|
||||
// Other Logic Stuff
|
||||
|
@ -243,7 +235,7 @@ namespace FRESHMusicPlayer
|
|||
/// Returns a formatted string of the current playback position.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string SongPositionString() => $"{AudioFile.CurrentTime:c} / {AudioFile.TotalTime:c}";
|
||||
public string SongPositionString() => $"{currentBackend.CurrentTime:c} / {currentBackend.TotalTime:c}";
|
||||
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
|||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("2.0.1.0")]
|
||||
[assembly: AssemblyFileVersion("2.0.1.0")]
|
||||
[assembly: AssemblyVersion("2.1.1.0")]
|
||||
[assembly: AssemblyFileVersion("2.1.1.0")]
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace FRESHMusicPlayer.Utilities
|
|||
{
|
||||
static class PlayerUtils
|
||||
{
|
||||
private static Random rng = new Random();
|
||||
private static readonly Random rng = new Random();
|
||||
|
||||
public static List<string> ShuffleQueue(this Player player, List<string> list)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue