Skip to content

Commit

Permalink
Allows the library to work even when the system does not have any aud…
Browse files Browse the repository at this point in the history
…io devices or audio service is disabled
  • Loading branch information
SuRGeoNix committed Nov 18, 2021
1 parent 8eb7387 commit c9a936d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
20 changes: 16 additions & 4 deletions FlyleafLib/AudioMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public string Device
}
}

/// <summary>
/// Whether no audio devices were found or audio failed to initialize
/// </summary>
public bool Failed { get; private set; }

/// <summary>
/// Gets or sets the master's volume (valid values 0 - 100)
/// </summary>
Expand Down Expand Up @@ -113,10 +118,17 @@ public void Initialize()
foreach(var player in Master.Players.Values)
player.InitializeAudio();

if (Device == DefaultDeviceName)
device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
else
device = deviceEnum.GetDevice(DeviceId);
try
{
if (Device == DefaultDeviceName)
device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
else
device = deviceEnum.GetDevice(DeviceId);
} catch (Exception)
{
Failed = true;
return;
}

device.AudioEndpointVolume.OnVolumeNotification += OnMasterVolumeChanged;
device.AudioSessionManager.OnSessionCreated += (o, newSession) =>
Expand Down
5 changes: 5 additions & 0 deletions FlyleafLib/FlyleafLib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions FlyleafLib/MediaPlayer/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ public AudioPlayer(Config config)
}
internal void InitializeAudio(int sampleRate = -1)
{
if (Master.AudioMaster.Failed) { Config.Audio.Enabled = false; return; }

lock (lockerAudioPlayer)
{
if (sampleRate != -1)
Expand Down

0 comments on commit c9a936d

Please sign in to comment.