-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
186 lines (155 loc) · 5.58 KB
/
MainWindow.xaml.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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Data.SQLite;
using NAudio.Wave;
using System.Net;
using pfcode_stations.classes;
using System.Threading;
namespace pfcode_stations
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private WaveOutEvent waveOut;
private CancellationTokenSource cancellationTokenSource;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Run your code here after the MainWindow has loaded completely
LoadStations();
// Additional code to execute after the MainWindow is fully loaded
stationName.SelectionChanged += stationName_SelectionChanged;
}
public void LoadStations()
{
SQLiteConnection connection = DbConnector.Connect();
SQLiteCommand command = new SQLiteCommand("SELECT name FROM stations", connection);
SQLiteDataReader reader = command.ExecuteReader();
List<string> stations = new List<string>();
while (reader.Read())
{
string stationName = reader.GetString(0);
stations.Add(stationName);
}
foreach (string station in stations)
{
stationName.Items.Add(station);
}
//stationName.SelectedIndex = 0;
stationName.SelectedIndex = -1;
reader.Close();
}
private async void stationName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cancellationTokenSource?.Cancel();
waveOut?.Stop();
waveOut?.Dispose();
if (stationName.SelectedItem != null)
{
stopButton.Visibility = Visibility.Visible;
try
{
string mp3Url = await GetMp3UrlAsync(stationName.SelectedItem.ToString());
await PlayAudioStreamAsync(mp3Url);
}
catch (NAudio.MmException ex)
{
MessageBox.Show($"NAudio.MmException: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Exception: {ex.Message}");
}
}
else
{
stopButton.Visibility = Visibility.Hidden;
}
}
private async Task PlayAudioStreamAsync(string mp3Url, int maxRetries = 3, TimeSpan retryDelay = default)
{
int retryCount = 0;
bool success = false;
while (!success && retryCount < maxRetries)
{
try
{
cancellationTokenSource = new CancellationTokenSource();
using (var reader = new MediaFoundationReader(mp3Url))
{
waveOut = new WaveOutEvent();
waveOut.Init(reader);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
await Task.Delay(300);
}
waveOut.Stop();
waveOut.Dispose();
success = true;
}
}
catch (NAudio.MmException ex)
{
MessageBox.Show($"NAudio.MmException: {ex.Message}");
retryCount++;
if (retryCount < maxRetries)
{
await Task.Delay(retryDelay);
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception: {ex.Message}");
}
}
if (!success)
{
MessageBox.Show("Failed to play audio stream after multiple retries.");
}
}
private async Task<string> GetMp3UrlAsync(string stationName)
{
using (SQLiteConnection connection = DbConnector.Connect())
{
StringBuilder getUrl = new StringBuilder();
getUrl.Append("SELECT url FROM stations WHERE name = @name");
using (SQLiteCommand command = new SQLiteCommand(getUrl.ToString(), connection))
{
command.Parameters.AddWithValue("@name", stationName);
return await command.ExecuteScalarAsync() as string;
}
}
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
// Stop the audio stream and do any cleanup required
cancellationTokenSource?.Cancel();
waveOut?.Stop();
waveOut?.Dispose();
stopButton.Visibility = Visibility.Collapsed;
stationName.SelectedIndex = -1;
}
}
}