-
Notifications
You must be signed in to change notification settings - Fork 1
/
SongList.cs
65 lines (51 loc) · 1.88 KB
/
SongList.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace WPF_music_player
{
public class SongList
{
private string[]? songs;
private int currentIndex;
private Song currentSong;
string songDuration;
public string[]? Songs { get => songs; set => songs = value; }
public int CurrentIndex { get => currentIndex; set => currentIndex = value; }
public Song CurrentSong { get => currentSong; set => currentSong = value; }
public SongList(string filePath, MediaPlayer mediaPlayer)
{
string? directoryPath = Path.GetDirectoryName(filePath);
string title = System.IO.Path.GetFileName(filePath);
if (directoryPath != null)
{
this.Songs = Directory.GetFiles(directoryPath, "*.mp3");
currentIndex = Array.IndexOf(this.Songs, filePath);
}
var task = WaitForDuration(mediaPlayer);
if (mediaPlayer.NaturalDuration.HasTimeSpan)
{
songDuration = mediaPlayer.NaturalDuration.TimeSpan.ToString((@"mm\:ss")) ?? "00:00";
}
else
{
songDuration = "00:00";
}
currentSong = new Song(title, songDuration);
}
private async Task WaitForDuration(MediaPlayer mediaPlayer)
{
while (!mediaPlayer.NaturalDuration.HasTimeSpan || mediaPlayer.NaturalDuration.TimeSpan == TimeSpan.Zero)
{
await Task.Delay(1000);
if (mediaPlayer.NaturalDuration.HasTimeSpan && mediaPlayer.NaturalDuration.TimeSpan != TimeSpan.Zero)
{
break;
}
}
}
}
}