-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM3UPlaylist.cs
100 lines (96 loc) · 2.07 KB
/
M3UPlaylist.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Music_Playlist_Randomizer
{
public class M3UPlaylist : BasePlaylist
{
/// <summary>
/// Gets the total seconds for the playlist.
/// </summary>
public int Seconds
{
get
{
int seconds = 0;
foreach (M3USong song in this)
seconds += song.Seconds;
return seconds;
}
}
/// <summary>
/// Gets the time formatted in [hh:m]m:ss.
/// </summary>
public string Time
{
get
{
int seconds = this.Seconds;
int minutes = seconds / 60;
seconds %= 60;
int hours = minutes / 60;
minutes %= 60;
string time = "";
if (hours >= 0)
time += hours + ":";
if (minutes < 10 && hours > 0)
time += "0";
time += minutes + ":";
if (seconds < 10)
time += "0";
time += seconds;
return time;
}
}
public M3UPlaylist()
{
this.Filename = "";
}
public override bool Load(string filename)
{
bool isLoaded = default(bool);
try
{
this.Filename = filename;
string[] lines = File.ReadAllLines(filename);
if (lines.Length < 1 || lines[0] != "#EXTM3U")
throw new FormatException("The file is not an M3U playlist.");
for (int i = 1; i < lines.Length - 1; i += 2)
this.Add(new M3USong(lines[i], lines[i + 1]));
isLoaded = true;
}
catch
{
isLoaded = false;
}
return isLoaded;
}
public override bool Save()
{
bool isSaved = default(bool);
try
{
string[] contents = new string[this.Count * 2 + 1];
// Header
contents[0] = "#EXTM3U";
// Body
for (int i = 0; i < this.Count; i++)
{
M3USong song = (M3USong)(this[i]);
contents[i * 2 + 1] = "#EXTINF:" + song.Seconds.ToString() + "," + song.Filename;
contents[i * 2 + 2] = song.FullPath;
}
// Final write
File.WriteAllLines(this.Filename, contents);
isSaved = true;
}
catch
{
isSaved = false;
}
return isSaved;
}
}
}