Skip to content

Commit

Permalink
added missing audio device mute attribute assignment; added missing m…
Browse files Browse the repository at this point in the history
…edia player mute assignment (#102)

This PR fixes:
    audio devices returned from AudioManager not having "Mute" attribute assigned properly
    media player not reporting mute status
  • Loading branch information
amadeo-alex authored Jun 16, 2024
1 parent 8e67272 commit bd03bf5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public static List<AudioDevice> GetDevices()
Id = device.DeviceId,
FriendlyName = device.FriendlyName,
Volume = Convert.ToInt32(Math.Round(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100, 0)),
Muted = device.MMDevice.AudioEndpointVolume.Mute,
PeakVolume = loudestSession == null ? 0 : loudestSession.PeakVolume,
Sessions = audioSessions,
Default = device.DeviceId == defaultInputDeviceId || device.DeviceId == defaultOutputDeviceId
Expand Down
22 changes: 13 additions & 9 deletions src/HASS.Agent/HASS.Agent/Media/MediaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using MediaPlayerState = HASS.Agent.Enums.MediaPlayerState;
using Octokit;
using Windows.Media.Core;
using HASS.Agent.Shared.Managers.Audio;

namespace HASS.Agent.Media
{
Expand All @@ -38,7 +39,7 @@ internal static async Task InitializeAsync()
Log.Information("[MEDIA] Disabled");
return;
}

if (!Variables.AppSettings.LocalApiEnabled && !Variables.AppSettings.MqttEnabled)
{
Log.Warning("[MEDIA] Both local API and MQTT are disabled, unable to receive media requests");
Expand Down Expand Up @@ -70,7 +71,7 @@ internal static async Task InitializeAsync()
}
catch (Exception ex)
{
Log.Fatal(ex, "[MEDIA] Unable to initialize: {err}" , ex.Message);
Log.Fatal(ex, "[MEDIA] Unable to initialize: {err}", ex.Message);
Variables.AppSettings.MediaPlayerEnabled = false;

Log.Warning("[MEDIA] Failed, disabled");
Expand All @@ -79,7 +80,7 @@ internal static async Task InitializeAsync()

// start monitoring playing media
_ = Task.Run(MediaMonitor);

if (!Variables.AppSettings.MqttEnabled) Log.Warning("[MEDIA] MQTT is disabled, only basic media functionality will work");
else
{
Expand Down Expand Up @@ -191,7 +192,8 @@ private static async void MediaMonitor()
message.AlbumArtist = mediaProperties.AlbumArtist;
message.AlbumTitle = mediaProperties.AlbumTitle;
message.Volume = MediaManagerRequests.GetVolume();

message.Muted = AudioManager.GetDevices().FirstOrDefault(d => d.Type == DeviceType.Output && d.Default)?.Muted == true;

// get timeline info
var timeline = session.GetTimelineProperties();
if (timeline != null)
Expand Down Expand Up @@ -243,9 +245,9 @@ private static GlobalSystemMediaTransportControlsSession GetCurrentMediaSession(
// if none are playing: pick the first

if (sessions.Count == 1) return sessions[0];
return sessions.Any(x => x.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
? sessions.First(x => x.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)

return sessions.Any(x => x.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
? sessions.First(x => x.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
: sessions[0];
}

Expand Down Expand Up @@ -331,7 +333,8 @@ internal static void ProcessCommand(MediaPlayerCommand command)

case MediaPlayerCommand.Play:
if (Variables.ExtendedLogging) Log.Information("[MEDIA] Command received: Play");
if (State == MediaPlayerState.Playing) {
if (State == MediaPlayerState.Playing)
{
if (Variables.ExtendedLogging) Log.Warning("[MEDIA] Media already playing");
break;
}
Expand All @@ -340,7 +343,8 @@ internal static void ProcessCommand(MediaPlayerCommand command)

case MediaPlayerCommand.Pause:
if (Variables.ExtendedLogging) Log.Information("[MEDIA] Command received: Pause");
if (State == MediaPlayerState.Paused) {
if (State == MediaPlayerState.Paused)
{
if (Variables.ExtendedLogging) Log.Warning("[MEDIA] Media already paused");
break;
}
Expand Down

0 comments on commit bd03bf5

Please sign in to comment.