-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaPlayer.cs
186 lines (155 loc) · 6.16 KB
/
MediaPlayer.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
using Accord.Math.Distances;
using Microsoft.Extensions.Options;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using System;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Media;
using System.Net.Http;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using YoutubeDLSharp;
using YoutubeDLSharp.Options;
using YoutubeExplode.Videos.Streams;
namespace NHMPh_music_player
{
public class MediaPlayer
{
private WaveOutEvent output = new WaveOutEvent();
private _MediaFoundationReader _mf;
private WaveChannel32 wave;
FFTSampleProvider fftProvider;
private MainWindow mainWindow;
private VideoInfo currentSong;
public event EventHandler OnSongChange;
public event EventHandler OnPositionChange;
private SpectrumVisualizer visualizer;
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
public VideoInfo CurrentSong { get { return currentSong; } set { currentSong = value; } }
public WaveChannel32 Wave { get { return wave; } set { wave = value; } }
public PlaybackState PlaybackState { get { return output.PlaybackState; } }
public string PlayBackUrl;
public float Volume { get { return output.Volume; } set { output.Volume = (float)value; } }
public MediaPlayer(MainWindow mainWindow, SpectrumVisualizer visualizer)
{
this.mainWindow = mainWindow;
this.visualizer = visualizer;
}
OptionSet options = new OptionSet() { Format = "m4a", GetUrl = true };
private async Task GetSongStream()
{
string url;
Console.WriteLine(currentSong.Url);
try
{
var streamManifest = await mainWindow.youtube.Videos.Streams.GetManifestAsync(currentSong.Url);
var streamUrl = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();//streamManifest.GetMuxedStreams().GetWithHighestVideoQuality().Url;
System.IO.Stream stream = await mainWindow.youtube.Videos.Streams.GetAsync(streamUrl);
Console.WriteLine(stream.Length);
url = streamUrl.Url;
}
catch
{
mainWindow.status.Text = "Loading";
if (!File.Exists(".\\yt-dlp.exe")) //if ytdl is not downloaded
{
string message = "Downloading yt-dlp.exe please wait: https://github.com/yt-dlp/yt-dlp";
string caption = "Download yt-dlp";
MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Information);
mainWindow.status.Text = "Downloading...";
await YoutubeDLSharp.Utils.DownloadYtDlp();
}
else
{
}
Console.WriteLine("Make up "+StringUtilitiy.MakeValidLink(currentSong.Url));
var _streamUrl = await mainWindow.ytdl.RunWithOptions(
new[] {StringUtilitiy.MakeValidLink(currentSong.Url) },
options,
CancellationToken.None
);
Console.WriteLine("2");
url = _streamUrl.Data[0];
}
Console.WriteLine(url);
_mf = new _MediaFoundationReader(url);
Console.WriteLine("4");
// PlayBackUrl = streamUrl;
wave = new NAudio.Wave.WaveChannel32(_mf);
Console.WriteLine("5");
fftProvider = new FFTSampleProvider(wave.ToSampleProvider(), visualizer);
Console.WriteLine("6");
}
private void GetRadioStream()
{
_mf = new _MediaFoundationReader(currentSong.Url);
wave = new WaveChannel32(_mf);
fftProvider = new FFTSampleProvider(wave.ToSampleProvider(), visualizer);
}
public MediaPlayer(VideoInfo videoInfo)
{
currentSong = videoInfo;
}
public async Task PlayRadio()
{
MusicSetting.isRadio = true;
mainWindow.status.Text = "Please wait";
await Task.Run(() => GetRadioStream());
OnSongChange?.Invoke(this, null);
output?.Dispose();
output.Init(fftProvider);
output.Play();
mainWindow.status.Text = "Playing";
}
public async void PlayMusic()
{
MusicSetting.isRadio = false;
mainWindow.status.Text = "Loading...";
await Task.Run(() => GetSongStream());
OnSongChange?.Invoke(this, null);
Console.WriteLine("Playing");
output?.Dispose();
//output = new WaveOut();
output.Init(fftProvider);
output.Play();
mainWindow.status.Text = "Playing";
}
public void PauseMusic()
{
output.Pause();
mainWindow.status.Text = "Paused";
mainWindow.playpause.Source = new BitmapImage(
new Uri($"{Environment.CurrentDirectory}\\Images\\_Play.png"));
}
public void _PauseMusic()
{
output.Pause();
}
public void ResumeMusic()
{
output.Play();
mainWindow.status.Text = "Playing";
mainWindow.playpause.Source = new BitmapImage(
new Uri($"{Environment.CurrentDirectory}\\Images\\_Pause.png"));
}
public void Seek(int seconds)
{
Console.WriteLine("Stop");
wave.Position = wave.WaveFormat.AverageBytesPerSecond * seconds;
if (output.PlaybackState != PlaybackState.Playing)
output.Play();
mainWindow.songProgress.Value = wave.CurrentTime.TotalMilliseconds;
OnPositionChange?.Invoke(this, null);
}
public int GetMasterPeak()
{
var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
return (int)(60 * device.AudioMeterInformation.MasterPeakValue);
}
}
}