Fixed albumcover load on startup

This commit is contained in:
Janco Kock
2015-10-31 22:23:39 +01:00
parent 00dfd2a78b
commit 68ca3af5ff
4 changed files with 42 additions and 21 deletions
+13 -12
View File
@@ -13,10 +13,11 @@ namespace MusicPlayer
public class APIHandler public class APIHandler
{ {
private NetworkHandler nw; private NetworkHandler nw;
private Image defaultCover;
public APIHandler(NetworkHandler nw) public APIHandler(NetworkHandler nw)
{ {
this.nw = nw; this.nw = nw;
defaultCover = Image.FromStream(nw.downloadArtwork("default-cover.png"));
} }
public string GetSongURLByID(string id) public string GetSongURLByID(string id)
@@ -94,26 +95,26 @@ namespace MusicPlayer
return artistlist; return artistlist;
} }
public Image getAlbumCover(string album)
{
MemoryStream stream = nw.downloadArtwork(album + ".jpg");
if(stream != null)
{
return Image.FromStream(stream);
}
return defaultCover;
}
public List<Album> GetAlbums() public List<Album> GetAlbums()
{ {
List<Album> albumlist = new List<Album>(); List<Album> albumlist = new List<Album>();
return albumlist;
Image def = Image.FromStream(nw.downloadArtwork("default-cover.png"));
JObject o = nw.SendString("getalbums?id=hallo"); JObject o = nw.SendString("getalbums?id=hallo");
if (o["result"].ToString() == "OK") if (o["result"].ToString() == "OK")
{ {
for (int i = 0; i < o["albums"].Count(); i++) for (int i = 0; i < o["albums"].Count(); i++)
{ {
MemoryStream stream = nw.downloadArtwork(o["albums"][i][0].ToString() + ".jpg"); albumlist.Add(new Album(o["albums"][i][0].ToString()));
if (stream != null) {
albumlist.Add(new Album(o["albums"][i][0].ToString(), Image.FromStream(stream)));
stream.Close();
}
else
{
albumlist.Add(new Album(o["albums"][i][0].ToString(), def));
}
} }
} }
+1 -3
View File
@@ -6,12 +6,10 @@ namespace MusicPlayer
public class Album public class Album
{ {
public string albumnaam { get; set; } public string albumnaam { get; set; }
public Image cover;
public Album(string albumnaam, Image c) public Album(string albumnaam)
{ {
this.albumnaam = albumnaam; this.albumnaam = albumnaam;
this.cover = c;
} }
} }
} }
+27 -5
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -16,8 +17,7 @@ namespace MusicPlayer
public AudioHandler audio; public AudioHandler audio;
public SongsTable table; public SongsTable table;
private ImageList imagelist;
private List<string> genres; private List<string> genres;
private List<string> artists; private List<string> artists;
@@ -33,7 +33,6 @@ namespace MusicPlayer
audio = new AudioHandler(this); audio = new AudioHandler(this);
table = new SongsTable(); table = new SongsTable();
imagelist = new ImageList();
form.SongsTableView.DataSource = table; form.SongsTableView.DataSource = table;
form.SongsTableView.Columns[5].Visible = false; form.SongsTableView.Columns[5].Visible = false;
@@ -47,11 +46,34 @@ namespace MusicPlayer
private void Populate() private void Populate()
{ {
form.AlbumListView.LargeImageList = imagelist; this.api.GetAlbums().ForEach(a => { form.AlbumListView.Items.Add(a.albumnaam);});
this.api.GetAlbums().ForEach(a => { var item = form.AlbumListView.Items.Add(a.albumnaam); imagelist.Images.Add(a.albumnaam, a.cover); item.ImageKey = a.albumnaam;});
this.api.GetArtists().ForEach(a => { artists.Add(a.naam); form.ArtistListBox.Items.Add(a.naam); }); this.api.GetArtists().ForEach(a => { artists.Add(a.naam); form.ArtistListBox.Items.Add(a.naam); });
this.api.GetGenres().ForEach(g => { genres.Add(g.name); form.GenreListBox.Items.Add(g.name); }); this.api.GetGenres().ForEach(g => { genres.Add(g.name); form.GenreListBox.Items.Add(g.name); });
this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name)); this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name));
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(
delegate (object o, DoWorkEventArgs args)
{
BackgroundWorker b = o as BackgroundWorker;
var i = 0;
ImageList imagelist = new ImageList();
foreach (ListViewItem item in form.AlbumListView.Items)
{
imagelist.Images.Add(i.ToString(), api.getAlbumCover(item.Text));
i++;
}
i = 0;
Action action = () => {
form.AlbumListView.LargeImageList = imagelist;
foreach (ListViewItem item in form.AlbumListView.Items)
{
item.ImageKey = i.ToString();
i++;
}
};
form.Invoke(action);
});
bw.RunWorkerAsync();
} }
public void Repopulate() public void Repopulate()
+1 -1
View File
@@ -17,7 +17,7 @@ namespace MusicPlayer
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
NetworkHandler nw = new NetworkHandler("http://imegumii.nl"); NetworkHandler nw = new NetworkHandler("http://jancokock.me");
APIHandler api = new APIHandler(nw); APIHandler api = new APIHandler(nw);
MainForm form = new MainForm(); MainForm form = new MainForm();
PlaylistHandler pl = new PlaylistHandler(api); PlaylistHandler pl = new PlaylistHandler(api);