This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
executable file
·226 lines (216 loc) · 8.64 KB
/
Player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using ManagedBass;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using YandexMusicApi;
namespace Yamux
{
public class Player
{
public delegate void PlayerTrack();
public static event PlayerTrack TrackStop;
public static event PlayerTrack TrackLast;
public static event PlayerTrack TrackNext;
public static event PlayerTrack ChangeCurrentTrack;
private static bool boolNextTrack = false;
private static bool boolLastTrack = false;
private static bool boolStopTrack = false;
private static bool StopAwait = false;
private static bool ClickButtonOrNo = false;
private static bool PlayTrackOrNo;
public static bool PlayTrackOrPause = true;
public static int stream;
public static int currentTrack = -1;
public static List<string> trackIds;
public static async void PlayUrlFile()
{
if (!PlayTrackOrNo) { Bass.Init(); }
else
{
Bass.StreamFree(stream);
Bass.Free();
Bass.Init();
}
currentTrack = -1;
string directLinkToTrack = "";
await Task.Run(() => { directLinkToTrack = GetDirectLinkWithTrack(trackIds[0]); });
Search.directLink = directLinkToTrack;
stream = Bass.CreateStream(directLinkToTrack, 0, BassFlags.StreamDownloadBlocks, null, IntPtr.Zero);
if (stream != 0)
{
Bass.ChannelPlay(stream);
PlayTrackOrNo = true;
PlayTrackOrPause = true;
ChangeCurrentTrack.Invoke();
}
else Console.WriteLine("Error: {0}!", Bass.LastError);
}
public static void NextTrack(object sender, EventArgs a) { TrackNext.Invoke(); }
public static void LastTrack(object sender, EventArgs a) { TrackLast.Invoke(); }
public static async void PlayPlaylist()
{
currentTrack = -1;
while (true)
{
try
{
ClickButtonOrNo = false;
if (boolStopTrack)
{
Bass.StreamFree(stream);
Bass.Free();
Bass.Init();
boolStopTrack = false;
}
if (PlayTrackOrNo == false) { Bass.Init(); }
else
{
Bass.StreamFree(stream);
Bass.Free();
Bass.Init();
}
currentTrack++;
StopAwait = false;
string directLinkToTrack = "";
await Task.Run(() => { directLinkToTrack = GetDirectLinkWithTrack(trackIds[currentTrack]); });
Console.WriteLine(directLinkToTrack);
Search.directLink = directLinkToTrack;
Bass.Init();
stream = Bass.CreateStream(directLinkToTrack, 0, BassFlags.StreamDownloadBlocks, null, IntPtr.Zero);
if (stream != 0)
{
ChangeCurrentTrack.Invoke();
PlayTrackOrPause = true;
PlayTrackOrNo = true;
Bass.ChannelPlay(stream);
}
else Console.WriteLine("Error: {0}!", Bass.LastError);
await Task.Run(() =>
{
TrackNext += () => { StopAwait = true; };
TrackStop += () => { boolStopTrack = true; StopAwait = true; };
TrackLast += () =>
{
if (ClickButtonOrNo == false)
{
currentTrack -= 2;
ClickButtonOrNo = true;
StopAwait = true;
}
};
while (true)
{
Thread.Sleep(1000);//Просто сон async метода, чтобы пк не офигел от проверки
if (Bass.ChannelGetPosition(stream) != -1)
{
if (Bass.ChannelGetLength(stream) == Bass.ChannelGetPosition(stream) || StopAwait) //Если трек закончился, начать следующию песню
{
StopAwait = false;
Bass.Free();
Bass.Init();
break;
}
}
}
});
}
catch (ArgumentOutOfRangeException)
{
currentTrack = -1;
PlayTrackOrNo = false;
PlayTrackOrPause = false;
Console.WriteLine("End playlist");
break;
}
}
}
public static void StopTrack(object sender, EventArgs a)
{
Bass.StreamFree(stream);
Bass.Free();
try
{ TrackStop.Invoke(); }catch (NullReferenceException){}
}
public static void PauseOrStartPlay()
{
if (PlayTrackOrPause)
{
Bass.ChannelPause(stream);
PlayTrackOrNo = false;
PlayTrackOrPause = false;
}
else
{
Bass.PauseNoPlay = 1;
Bass.ChannelPlay(stream);
PlayTrackOrNo = true;
PlayTrackOrPause = true;
}
}
public static int GetLength()
{
if (stream == 0) { return stream; }
return Convert.ToInt32(Bass.ChannelBytes2Seconds(stream, Bass.ChannelGetLength(stream)));
}
private static string GetDirectLinkWithTrack(string trackId)
{
JObject result = Track.GetDownloadInfoWithToken(trackId);
Console.WriteLine(result);
var resultData = result.GetValue("result");
resultData = resultData[0];
string url = resultData.ToList()[3].ToList()[0].ToString();
return Track.GetDirectLink(url);
}
public static async Task DownloadUriWithThrottling(string url, string path, double speedKbps = -0.0)
{
Uri uri = new Uri(url);
var req = WebRequest.CreateHttp(uri);
using (var resp = await req.GetResponseAsync())
using (var stream = resp.GetResponseStream())
using (var outfile = File.OpenWrite(path))
{
var startTime = DateTime.Now;
long totalDownloaded = 0;
var buffer = new byte[0x10000];
while (true)
{
var actuallyRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (actuallyRead == 0)
return;
await outfile.WriteAsync(buffer, 0, actuallyRead);
totalDownloaded += actuallyRead;
if (speedKbps != -0.0)
{
var expectedTime = totalDownloaded / 1024.0 / speedKbps;
var actualTime = (DateTime.Now - startTime).TotalSeconds;
if (expectedTime > actualTime)
await Task.Delay(TimeSpan.FromSeconds(expectedTime - actualTime));
}
}
}
}
public static void DownloadWithSync(Uri uri, string path, double speedKbps = -0.0)
{
var req = WebRequest.CreateHttp(uri);
using (var resp = req.GetResponse())
using (var stream = resp.GetResponseStream())
using (var outfile = File.OpenWrite(path))
{
long totalDownloaded = 0;
var buffer = new byte[0x10000];
while (true)
{
var actuallyRead = stream.Read(buffer, 0, buffer.Length);
if (actuallyRead == 0)
return;
outfile.Write(buffer, 0, actuallyRead);
}
}
}
}
}