-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
217 lines (173 loc) · 6.56 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
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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using Microsoft.Win32;
using System.Windows.Threading;
using System.Windows.Input;
namespace WPF_music_player
{
public partial class MainWindow : Window
{
private MediaPlayer mediaPlayer = new MediaPlayer();
public SongList songList;
public MainWindow()
{
try
{
InitializeComponent();
songList = Open_File();
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private void btnOpenAudioFile_Click(object sender, RoutedEventArgs e)
{
songList = Open_File();
}
private SongList Open_File()
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3|All files (*.*)|*.*";
string fileName;
if(openFileDialog.ShowDialog() == true)
{
songList = OpenSong(openFileDialog.FileName);
try
{
BitmapImage bitmapImage = new BitmapImage(); //Define the image if it is one
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(openFileDialog.FileName);
bitmapImage.EndInit();
songCover.Source = bitmapImage;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
return songList;
}
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
return new SongList("", mediaPlayer);
}
private SongList OpenSong(string path)
{
mediaPlayer.Open(new Uri(path));
SongList songList = new SongList(path, mediaPlayer);
int index = songList.CurrentIndex;
Song song = songList.CurrentSong;
SongName.Content = song.Name;
mediaPlayer.Play();
btnPlay.Visibility = Visibility.Collapsed;
btnPause.Visibility = Visibility.Visible;
return songList;
}
private void timer_Tick(object sender, EventArgs e)
{
if(mediaPlayer.Source != null)
{
lblStatus.Content = String.Format("{0}", mediaPlayer.Position.ToString(@"mm\:ss"));
lblDuration.Content = String.Format("{0}", mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
TimeLapseSlider.Value = mediaPlayer.Position.TotalSeconds;
TimeLapseSlider.Maximum = mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds;
}
else
{
lblStatus.Content = "00:00";
}
}
private void TimeLapseModification(object sender, EventArgs e)
{
mediaPlayer.Position = TimeSpan.FromSeconds(TimeLapseSlider.Value);
}
private void positionSliderSkip(object sender, MouseButtonEventArgs e)
{
if (((Slider)sender).Name == "TimeLapseSlider")
{
Point mousePosition = e.GetPosition(TimeLapseSlider);
double clickPercentage = mousePosition.X / TimeLapseSlider.ActualWidth;
double clickValue = clickPercentage * (TimeLapseSlider.Maximum - TimeLapseSlider.Minimum);
TimeLapseSlider.Value = clickValue;
}
else if (((Slider)sender).Name == "volumeSlider")
{
Point mousePosition = e.GetPosition(volumeSlider);
double clickPercentage = mousePosition.Y / volumeSlider.ActualHeight;
double clickValue = clickPercentage * (volumeSlider.Maximum - volumeSlider.Minimum);
volumeSlider.Value = mousePosition.Y;
}
}
private void PlaySound(object sender, RoutedEventArgs e)
{
mediaPlayer.Play();
btnPlay.Visibility = Visibility.Collapsed;
btnPause.Visibility = Visibility.Visible;
}
private void StopSound(object sender, RoutedEventArgs e)
{
mediaPlayer.Pause();
btnPlay.Visibility = Visibility.Visible;
btnPause.Visibility = Visibility.Collapsed;
}
private string[] songs;
private int currentIndex;
private void NextSong(object sender, RoutedEventArgs e)
{
try
{
songs = songList.Songs;
if (songs == null || songs.Length == 0)
return;
songList.CurrentIndex++;
if (songList.CurrentIndex >= songs.Length)
songList.CurrentIndex = 0;
string filePath = songs[songList.CurrentIndex];
songList = OpenSong(filePath);
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
}
private void PreviousSong(object sender, RoutedEventArgs e)
{
try
{
songs = songList.Songs;
if (songs == null || songs.Length == 0)
return;
songList.CurrentIndex--;
if (songList.CurrentIndex < 0)
songList.CurrentIndex = songs.Length-1;
string filePath = songs[songList.CurrentIndex];
songList = OpenSong(filePath);
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
}
private void ChangeVolume(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mediaPlayer.Volume = volumeSlider.Value;
}
}
}