Skip to content

Commit 1c9dcd7

Browse files
[AudioGraphSettings] Move AudioSettings To Base
1 parent f225527 commit 1c9dcd7

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

HyPlayer.PlayCore.Abstraction/Models/AudioServiceSettingsBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace HyPlayer.PlayCore.Abstraction.Models
44
{
5-
public class AudioServiceSettingsBase : IAudioServiceSettings
5+
public abstract class AudioServiceSettingsBase : IAudioServiceSettings
66
{
7+
public abstract string DefaultOutputDeviceId { get; set; }
8+
public abstract double OutputVolume { get; set; }
79
}
810
}

HyPlayer.PlayCore.Demo.AudioGraph.WinUI3/App.xaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
using HyPlayer.PlayCore.Implementation.AudioGraphService.Abstractions;
99
using HyPlayer.PlayCore.Implementation.AudioGraphService.Abstractions.Notifications;
1010
using Microsoft.UI.Xaml;
11-
using Windows.Media.Audio;
12-
using Windows.Media.Render;
1311

1412
// To learn more about WinUI, the WinUI project structure,
1513
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -29,7 +27,7 @@ public App()
2927
{
3028
this.InitializeComponent();
3129
var container = DepositoryFactory.CreateNew();
32-
var settings = new AudioGraphServiceSettings() { AudioGraphSettings = new AudioGraphSettings(AudioRenderCategory.Media) };
30+
var settings = new AudioGraphServiceSettings();
3331
container.AddSingleton<AudioServiceSettingsBase, AudioGraphServiceSettings>(settings);
3432
container.AddSingleton<AudioGraphService>();
3533
container.AddSingleton<INotificationHub, NotificationHub>();
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
using HyPlayer.PlayCore.Abstraction.Models;
2+
using System;
3+
using System.Threading.Tasks;
4+
using Windows.Devices.Enumeration;
25
using Windows.Media.Audio;
36

47
namespace HyPlayer.PlayCore.Implementation.AudioGraphService.Abstractions
58
{
69
public class AudioGraphServiceSettings : AudioServiceSettingsBase
710
{
8-
public AudioGraphSettings AudioGraphSettings { get; set; }
11+
public override string DefaultOutputDeviceId { get; set; }
12+
public override double OutputVolume { get; set; }
13+
internal async Task<AudioGraphSettings> GetAudioGraphSettingsAsync()
14+
{
15+
if (!string.IsNullOrEmpty(DefaultOutputDeviceId))
16+
{
17+
var deviceInfomation = await DeviceInformation.CreateFromIdAsync(DefaultOutputDeviceId);
18+
return new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media)
19+
{
20+
PrimaryRenderDevice = deviceInfomation
21+
};
22+
}
23+
else return new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media);
24+
}
925
}
1026
}

HyPlayer.PlayCore.Implementation.AudioGraph/Abstractions/Notifications/AudioGraphPlaybackPositionChangedNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace HyPlayer.PlayCore.Implementation.AudioGraphService.Abstractions.Notifications
22
{
3-
public class AudioGraphPlaybackPositionChangedNotification:PlaybackPositionChangedNotification
3+
public class AudioGraphPlaybackPositionChangedNotification : PlaybackPositionChangedNotification
44
{
55
public override double CurrentPlaybackPosition { get; init; }
66
public AudioGraphPlaybackPositionChangedNotification(double currentPlaybackPosition)

HyPlayer.PlayCore.Implementation.AudioGraph/Abstractions/Notifications/AudioTicketReachesEndNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace HyPlayer.PlayCore.Implementation.AudioGraphService.Abstractions.Notifications
55
{
6-
public class AudioGraphTicketReachesEndNotification:AudioTicketReachesEndNotification
6+
public class AudioGraphTicketReachesEndNotification : AudioTicketReachesEndNotification
77
{
88
public override AudioTicketBase AudioGraphTicket { get; init; }
99
public AudioGraphTicketReachesEndNotification(AudioGraphTicket ticket)

HyPlayer.PlayCore.Implementation.AudioGraph/AudioGraphService.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public class AudioGraphService :
3838
public double OutgoingVolume
3939
{
4040
get => _outgoingVolume;
41-
private set => _outgoingVolume = value;
41+
private set
42+
{
43+
_outgoingVolume = value;
44+
_audioGraphServiceSettings.OutputVolume = _outgoingVolume;
45+
}
4246
}
4347

4448
private double _outgoingVolume = 1d;
@@ -49,6 +53,8 @@ public double OutgoingVolume
4953

5054
private AudioGraphSettings _audioGraphSettings;
5155

56+
private AudioGraphServiceSettings _audioGraphServiceSettings;
57+
5258
private readonly List<AudioGraphTicket> _createdAudioTickets = [];
5359

5460
private Dictionary<AudioGraphTicket, Task> _currentSeekingSessions = new();
@@ -237,10 +243,7 @@ public async Task SetOutputDevicesAsync(OutputDeviceBase device, CancellationTok
237243
{
238244
audioGraphServiceSettings = new AudioGraphServiceSettings()
239245
{
240-
AudioGraphSettings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media)
241-
{
242-
PrimaryRenderDevice = outputDevice.DeviceInformation
243-
}
246+
DefaultOutputDeviceId = outputDevice.DeviceInformation.Id
244247
};
245248
await CreateAudioGraphFromSettings(audioGraphServiceSettings);
246249
}
@@ -250,15 +253,16 @@ public async Task SetOutputDevicesAsync(OutputDeviceBase device, CancellationTok
250253
{
251254
audioGraphServiceSettings = new AudioGraphServiceSettings()
252255
{
253-
AudioGraphSettings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media)
256+
DefaultOutputDeviceId = null
254257
};
255258
await CreateAudioGraphFromSettings(audioGraphServiceSettings);
256259
}
257260

258261
}
259262
private async Task CreateAudioGraphFromSettings(AudioGraphServiceSettings audioGraphServiceSettings)
260263
{
261-
var newAudioGraphResult = await AudioGraph.CreateAsync(audioGraphServiceSettings.AudioGraphSettings);
264+
var settings = await audioGraphServiceSettings.GetAudioGraphSettingsAsync();
265+
var newAudioGraphResult = await AudioGraph.CreateAsync(settings);
262266
if (newAudioGraphResult.Status != AudioGraphCreationStatus.Success)
263267
{
264268
throw newAudioGraphResult.ExtendedError;
@@ -379,7 +383,8 @@ public AudioGraphService(AudioServiceSettingsBase serviceSettings, INotification
379383
{
380384
if (serviceSettings is AudioGraphServiceSettings settings)
381385
{
382-
_audioGraphSettings = settings.AudioGraphSettings;
386+
_audioGraphServiceSettings = settings;
387+
_audioGraphSettings = settings.GetAudioGraphSettingsAsync().GetAwaiter().GetResult();
383388
}
384389
_notificationHub = notificationHub;
385390
_positionNotifyTimer.Elapsed += OnPositionNotifyTimerElapsed;

0 commit comments

Comments
 (0)