/// <summary> /// Play a file on the layer. If any previous file is playing it will be stopped. /// </summary> /// <param name="file">The file to play.</param> public ContinuousAction Play(SoundFile file) { ContinuousAction thisAction = new ContinuousAction(); void PlayInternal() { // Stop whatever was playing before. StopPlayingAll(true); // Queue the file. AL.SourceQueueBuffer(_pointer, file.Pointer); _playList.Add(file); Helpers.CheckErrorAL($"queuing single in source {_pointer}"); // Play it. AL.SourcePlay(_pointer); Status = SoundStatus.Playing; Helpers.CheckErrorAL($"playing single in source {_pointer}"); Debugger.Log(MessageType.Info, MessageSource.SoundManager, $"Started playing [{file.Name}] on {ToString()}."); thisAction.Done(); } // Check if forcing a fade out. if (FadeOutOnChange) { SetupForceFadeOut(PlayInternal); } else { ALThread.ExecuteALThread(PlayInternal); } return(thisAction); }
/// <summary> /// Stop playing any files. /// </summary> /// <param name="now">Whether to stop instantly or perform FadeOutOnChange if enabled.</param> public ContinuousAction StopPlayingAll(bool now = false) { ContinuousAction thisAction = new ContinuousAction(); void StopPlayingAllInternal() { Debugger.Log(MessageType.Info, MessageSource.SoundManager, $"Stopped {ToString()}."); // Stop playback, clear played buffer. AL.Source(_pointer, ALSourceb.Looping, false); AL.SourceStop(_pointer); // Remove played buffers. RemovePlayed(); Status = SoundStatus.Stopped; Helpers.CheckErrorAL("stopping"); // Reset tracker variables. PerformReset(); thisAction.Done(); } if (FadeOutOnChange && !now) { SetupForceFadeOut(StopPlayingAllInternal); } else { ALThread.ExecuteALThread(StopPlayingAllInternal); } return(thisAction); }
/// <summary> /// Queue a file to be played on the layer. /// </summary> /// <param name="file"></param> public ContinuousAction QueuePlay(SoundFile file) { ContinuousAction thisAction = new ContinuousAction(); void QueuePlayInternal() { Debugger.Log(MessageType.Info, MessageSource.SoundManager, $"Queued [{file.Name}] on {ToString()}."); // If playback is over but stop wasn't called then cleanup needs to be performed. if (Status == SoundStatus.Stopped) { PerformReset(); } AL.SourceQueueBuffer(_pointer, file.Pointer); _playList.Add(file); Helpers.CheckErrorAL($"queuing in source {_pointer}"); // Play if not playing. if (Status != SoundStatus.Stopped) { return; } AL.SourcePlay(_pointer); Status = SoundStatus.Playing; Helpers.CheckErrorAL($"playing source {_pointer}"); Debugger.Log(MessageType.Info, MessageSource.SoundManager, $"Started playing [{file.Name}] on {ToString()}."); thisAction.Done(); } if (FadeOutOnChange) { SetupForceFadeOut(QueuePlayInternal); } else { ALThread.ExecuteALThread(QueuePlayInternal); } return(thisAction); }
/// <summary> /// Destroy the layer freeing resources. /// </summary> public ContinuousAction Dispose() { ContinuousAction thisAction = new ContinuousAction(); ALThread.ExecuteALThread(() => { Debugger.Log(MessageType.Info, MessageSource.SoundManager, $"Destroyed {ToString()}."); StopPlayingAll(true); AL.DeleteSource(_pointer); Helpers.CheckErrorAL($"cleanup of source {_pointer}"); _pointer = -1; _playList.Clear(); thisAction.Done(); }); return(thisAction); }