@@ -23,13 +23,12 @@ public class PlayerController : IAsyncDisposable
23
23
private readonly ALContext _context ;
24
24
private readonly int _sourceId ;
25
25
private readonly ALFormat _targetFormat ;
26
+ private readonly YoutubeClient _youtubeClient ;
26
27
private Matroska ? _matroskaPlayerBuffer = null ;
27
28
private AudioSender ? _audioSender = null ;
28
29
private CancellationTokenSource _currentSongTokenSource = new ( ) ;
29
30
private bool _disposed = false ;
30
31
31
- private readonly YoutubeClient youtubeClient = new ( ) ;
32
-
33
32
public event Action ? StateChanged ;
34
33
public event Action < IEnumerable < IVideo > > ? QueueChanged ; //Maybe emit state to show a loading spinner
35
34
public event Action ? OnFinish ;
@@ -54,8 +53,9 @@ public int Volume
54
53
public IReadOnlyCollection < IVideo > Songs => _queue ;
55
54
public LoopState LoopState { get ; set ; }
56
55
57
- public PlayerController ( )
56
+ public PlayerController ( YoutubeClient youtubeClient )
58
57
{
58
+ _youtubeClient = youtubeClient ;
59
59
_device = ALC . OpenDevice ( Environment . GetEnvironmentVariable ( "DeviceName" ) ) ;
60
60
_context = ALC . CreateContext ( _device , new ALContextAttributes ( ) ) ;
61
61
ALC . MakeContextCurrent ( _context ) ;
@@ -112,13 +112,13 @@ public async Task<List<ISearchResult>> SearchAsync(
112
112
string query ,
113
113
CancellationToken token = default
114
114
) =>
115
- await youtubeClient
115
+ await _youtubeClient
116
116
. Search . GetResultsAsync ( query , token )
117
117
. Take ( 50 )
118
118
. ToListAsync ( cancellationToken : token ) ;
119
119
120
120
public async Task < List < Recommendation > > GetRecommendationsAsync ( ) =>
121
- await youtubeClient . Search . GetRecommendationsAsync ( ) . ToListAsync ( ) ;
121
+ await _youtubeClient . Search . GetRecommendationsAsync ( ) . ToListAsync ( ) ;
122
122
123
123
public async Task SkipToAsync ( IVideo video )
124
124
{
@@ -139,10 +139,10 @@ public async Task SetAsync(Recommendation recommendation)
139
139
_currentSongIndex = 0 ;
140
140
141
141
var firstVideo = recommendation . VideoId is not null
142
- ? await youtubeClient . Videos . GetAsync ( recommendation . VideoId . Value )
142
+ ? await _youtubeClient . Videos . GetAsync ( recommendation . VideoId . Value )
143
143
: null ;
144
144
145
- var playlist = await youtubeClient
145
+ var playlist = await _youtubeClient
146
146
. Playlists . GetVideosAsync ( recommendation . PlaylistId )
147
147
. ToListAsync ( ) ;
148
148
@@ -167,7 +167,7 @@ public async Task SetAsync(ISearchResult item)
167
167
168
168
if ( item is PlaylistSearchResult playlistSearchResult )
169
169
{
170
- var videos = await youtubeClient
170
+ var videos = await _youtubeClient
171
171
. Playlists . GetVideosAsync ( playlistSearchResult . Id )
172
172
. ToListAsync < IVideo > ( ) ;
173
173
@@ -176,7 +176,7 @@ public async Task SetAsync(ISearchResult item)
176
176
177
177
if ( item is ChannelSearchResult channelSearchResult )
178
178
{
179
- var videos = await youtubeClient
179
+ var videos = await _youtubeClient
180
180
. Channels . GetUploadsAsync ( channelSearchResult . Id )
181
181
. ToListAsync < IVideo > ( ) ;
182
182
@@ -224,7 +224,7 @@ public async Task PlayAsync()
224
224
try
225
225
{
226
226
_matroskaPlayerBuffer = await Matroska . Create (
227
- new YtDownloadUrlHandler ( youtubeClient , Song . Id ) ,
227
+ new YtDownloadUrlHandler ( _youtubeClient , Song . Id ) ,
228
228
_audioSender ,
229
229
_currentSongTokenSource . Token
230
230
) ;
0 commit comments