Files
Radio/src/Radiosender/Services/Audio/MusicPlayer.cs
T
fraujulianandClaude Opus 5.5 ef388d82f4 feat(schedule): rework the schedule page controls and timeline
- Mixer in two areas: what the listeners hear (music, microphone) and
  what you hear; the master volume is gone.
- One start button starts the created plan and goes live when laut.fm
  is on; while on air it ends the broadcast.
- The start field takes Jetzt (default), a full hour or a typed time.
  A Jetzt plan moves to the moment it starts.
- Timeline rows can be dragged to a new place; Hinzufügen inserts a
  song, news, an ad or moderator talk as the item after next.
- All page texts are translated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 09:23:06 +01:00

98 lines
2.8 KiB
C#

using System.IO;
using NAudio.Wave;
using Radiosender.Services.Localization;
namespace Radiosender.Services.Audio;
// Events: thread pool, never UI thread. WPF: marshal via Dispatcher before touching UI.
public sealed class MusicPlayer : IAudioPlayer
{
private static readonly Lazy<MusicPlayer> LazyInstance = new(() => new MusicPlayer());
private readonly ProgramOutput _output = ProgramOutput.Instance;
private readonly Lock _lock = new();
private AudioFileReader? _reader;
private ISampleProvider? _input;
private MusicPlayer()
{
// Audio thread must not wait for _lock (Stop holds it while waiting for that thread) -> thread pool.
_output.InputEnded += (_, input) => ThreadPool.QueueUserWorkItem(_ => OnInputEnded(input));
AppDomain.CurrentDomain.ProcessExit += (_, _) => Stop();
}
public static MusicPlayer Instance => LazyInstance.Value;
/// <summary>Not raised by <see cref="Stop"/> or by <see cref="Play"/> replacing a track.</summary>
public event EventHandler? TrackFinished;
public bool IsPlaying
{
get { lock (_lock) return _input is not null; }
}
public TimeSpan Duration
{
get { lock (_lock) return _reader?.TotalTime ?? TimeSpan.Zero; }
}
public void Play(string filePath)
{
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
lock (_lock)
{
Release();
if (!File.Exists(filePath))
throw new FileNotFoundException(Loc.F("Audio_FileNotFound", filePath), filePath);
try
{
_reader = new AudioFileReader(filePath);
_input = _output.Add(_reader, AudioSource.Music);
}
catch (Exception ex) when (ex is not (OutOfMemoryException or NAudio.MmException))
{
Release();
throw new InvalidDataException(Loc.F("Audio_FileUnreadable", filePath), ex);
}
catch
{
Release(); // no output device: file must not stay open
throw;
}
}
}
public void Stop()
{
lock (_lock) Release();
}
// Caller holds _lock. Clearing _input first: Stop/replace never raise TrackFinished.
private void Release()
{
if (_input is not null)
{
// Returns after audio thread let go of reader -> safe to dispose below.
_output.Remove(_input);
_input = null;
}
_reader?.Dispose();
_reader = null;
}
private void OnInputEnded(ISampleProvider input)
{
lock (_lock)
{
if (!ReferenceEquals(input, _input)) return;
Release();
}
TrackFinished?.Invoke(this, EventArgs.Empty);
}
}