New time handling in song

This commit is contained in:
2015-10-30 16:48:33 +01:00
parent e6d4eacd1c
commit 41dd995175
5 changed files with 43 additions and 19 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ namespace MusicPlayer
dynamic songs = o["songs"];
for (int i = 0; i < songs.Count; i++)
{
songslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), this));
songslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), songs[i][1].ToString(), (int)songs[i][9], this));
}
}
return songslist;
+32 -11
View File
@@ -18,13 +18,19 @@ namespace MusicPlayer
public BufferState BState { get; set; }
public int Buffered { get { return Math.Min((int)((bufpos / (double)LengthBuffer) * 100), 100); } }
public int Position { get { return Math.Min((int)((playpos / (double)Length) * 100), 100); } }
public string CurrentTime { get; set; }
public string TotalTime { get; set; }
private long LengthBuffer { get; set; }
private long Length { get; set; }
private long bufpos = 0;
public int Position { get { return Math.Min((int)((playpos / (double)Length) * 100), 100); } }
private long Length { get; set; }
private long playpos = 0;
public int CurrentTime { get; set; }
public int TotalTime { get { return CurrentSong != null ? CurrentSong.Seconds : 0; } }
private long seek = 0;
private Stream ms;
@@ -44,6 +50,8 @@ namespace MusicPlayer
AState = AudioState.STOPPED;
BState = BufferState.EMPTY;
CurrentSong = null;
Thread.Sleep(11);
ms = new MemoryStream();
@@ -58,6 +66,7 @@ namespace MusicPlayer
LengthBuffer = 1;
bufpos = 0;
playpos = 0;
CurrentTime = 0;
}
public void Play(Song s)
@@ -105,9 +114,6 @@ namespace MusicPlayer
ms.Position = position;
Mp3FileReader mp3fr = new Mp3FileReader(ms);
Length = mp3fr.Length;
CurrentTime = mp3fr.CurrentTime + "";
TotalTime = mp3fr.TotalTime + "";
using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(mp3fr)))
{
@@ -116,6 +122,9 @@ namespace MusicPlayer
waveOut.Init(blockAlignedStream);
waveOut.Play();
Length = CurrentSong.Seconds * waveOut.OutputWaveFormat.AverageBytesPerSecond;
CurrentTime = (int)(ms.Position / waveOut.OutputWaveFormat.AverageBytesPerSecond);
while (waveOut.PlaybackState != PlaybackState.Stopped)
{
System.Threading.Thread.Sleep(10);
@@ -142,13 +151,13 @@ namespace MusicPlayer
}
playpos = blockAlignedStream.Position;
Length = mp3fr.Length;
CurrentTime = mp3fr.CurrentTime + "";
TotalTime = mp3fr.TotalTime + "";
CurrentTime = (int)(playpos / waveOut.OutputWaveFormat.AverageBytesPerSecond);
}
AState = AudioState.STOPPED;
playpos = 0;
CurrentTime = 0;
}
}
}
@@ -156,7 +165,19 @@ namespace MusicPlayer
private void LoadAudio(object o)
{
Song s = (Song) o;
var response = WebRequest.Create(s.Url).GetResponse();
WebResponse response = null;
try
{
response = WebRequest.Create(s.Url).GetResponse();
}
catch(Exception e)
{
BState = BufferState.EMPTY;
AState = AudioState.STOPPED;
return;
}
BState = BufferState.EMPTY;
LengthBuffer = response.ContentLength;
+2 -2
View File
@@ -78,8 +78,8 @@ namespace MusicPlayer
if(!clicked)
PositionTrackBar.Value = main.audio.Position;
LabelCurrentTime.Text = main.audio.CurrentTime;
LabelTotalTime.Text = main.audio.TotalTime;
LabelCurrentTime.Text = main.audio.CurrentTime + "";
LabelTotalTime.Text = main.audio.TotalTime + "";
}
private void GenreListBox_SelectedIndexChanged(object sender, EventArgs e)
+4 -4
View File
@@ -27,10 +27,10 @@ namespace MusicPlayer
// {
// Console.WriteLine(s.Name);
// });
api.GetSongsByGenre("Melodic Death Metal").ForEach(s =>
{
Console.WriteLine(s.Name);
});
// api.GetSongsByGenre("Melodic Death Metal").ForEach(s =>
// {
// Console.WriteLine(s.Name);
// });
// api.GetSongsByAlbum("Stronger").ForEach(s =>
// {
// Console.WriteLine(s.Name);
+4 -1
View File
@@ -12,18 +12,21 @@ namespace MusicPlayer
public string Name { get; set; }
public string Album { get; set; }
public string Artist { get; set; }
public string Genre { get; set; }
public string Url { get { return GetURL(); } set { SetURL(value); } }
public int Seconds { get; set; }
private APIHandler api;
private string url;
public Song(string songid, string name, string album, string artist, APIHandler api)
public Song(string songid, string name, string album, string artist, string genre, int seconds, APIHandler api)
{
SongID = songid;
Name = name;
Album = album;
Artist = artist;
Seconds = seconds;
this.api = api;