Skip to content

Commit

Permalink
add support for restarting songs when playing them
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMakesGames committed Oct 6, 2024
1 parent daca47a commit b187acd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>Get seamless looping music, and cross-fade, in your MonoGame-PlayPlayMini game using NAudio.</Description>
<Copyright>2024 Ben Hendel-Doying</Copyright>
<Version>0.5.0</Version>
<Version>0.6.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini naudio music</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface INAudioMusicPlayer: IServiceLoadContent, IServiceUpdate
float GetVolume();
bool IsPlaying(string name);
string[] GetPlayingSongs();
INAudioMusicPlayer PlaySong(string name, int fadeInMilliseconds = 0);
INAudioMusicPlayer PlaySong(string name, int fadeInMilliseconds = 0, long? startPosition = null);
INAudioMusicPlayer StopAllSongs(int fadeOutMilliseconds = 0);
INAudioMusicPlayer StopAllSongsExcept(string[] songsToContinue, int fadeOutMilliseconds = 0);
INAudioMusicPlayer StopAllSongsExcept(string name, int fadeOutMilliseconds = 0);
Expand Down Expand Up @@ -201,8 +201,9 @@ public INAudioMusicPlayer SetVolume(float volume)
/// </summary>
/// <param name="name"></param>
/// <param name="fadeInMilliseconds"></param>
/// <param name="startPosition">Position to start song from. If null, song resumes from where it left off.</param>
/// <returns></returns>
public INAudioMusicPlayer PlaySong(string name, int fadeInMilliseconds = 0)
public INAudioMusicPlayer PlaySong(string name, int fadeInMilliseconds = 0, long? startPosition = null)
{
if(!Songs.TryGetValue(name, out var song))
{
Expand All @@ -213,6 +214,9 @@ public INAudioMusicPlayer PlaySong(string name, int fadeInMilliseconds = 0)
if(PlayingSongs.ContainsKey(name))
return this;

if(startPosition.HasValue)
song.Stream.Position = startPosition.Value;

var initiallySilent = fadeInMilliseconds > 0;

var gainAdjusted = Math.Abs(song.Gain - 1) < 0.001
Expand Down

0 comments on commit b187acd

Please sign in to comment.