Album art is now cached so reuse of album art in UI elements doesn't always need to reload image from disc.
This commit is contained in:
parent
086e56548c
commit
b9517d995a
5
pom.xml
5
pom.xml
@ -139,6 +139,11 @@
|
|||||||
<version>1.6.5-1</version>
|
<version>1.6.5-1</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>21.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package musicplayer.model;
|
package musicplayer.model;
|
||||||
|
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
import org.jaudiotagger.audio.AudioFileIO;
|
import org.jaudiotagger.audio.AudioFileIO;
|
||||||
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
||||||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
||||||
@ -19,6 +22,7 @@ import java.nio.file.Path;
|
|||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Representation of an Album.
|
* Representation of an Album.
|
||||||
@ -41,6 +45,17 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
|
|||||||
private static final String albumArtRegex = "*{Cover,Folder,cover,folder}\\.{jpg,png}";
|
private static final String albumArtRegex = "*{Cover,Folder,cover,folder}\\.{jpg,png}";
|
||||||
@Transient
|
@Transient
|
||||||
private static final int imageScaleToSize = 50;
|
private static final int imageScaleToSize = 50;
|
||||||
|
@Transient
|
||||||
|
private static final LoadingCache<Album, Optional<ImageIcon>> albumArtCache = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(1000)
|
||||||
|
.build(
|
||||||
|
new CacheLoader<Album, Optional<ImageIcon>>() {
|
||||||
|
@Override
|
||||||
|
public Optional<ImageIcon> load(Album album) throws Exception {
|
||||||
|
return getAlbumArt(album);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
protected Album() {
|
protected Album() {
|
||||||
}
|
}
|
||||||
@ -100,13 +115,26 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get album art for a given album.
|
* Get album art for this given album.
|
||||||
* @return Album art.
|
* @return Album art.
|
||||||
*/
|
*/
|
||||||
@Transient
|
@Transient
|
||||||
public Optional<ImageIcon> getAlbumArt(){
|
public Optional<ImageIcon> getAlbumArt(){
|
||||||
|
try {
|
||||||
|
return albumArtCache.get(this);
|
||||||
|
} catch (ExecutionException ignored) {}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get album are for a given album.
|
||||||
|
* @param album Album to get art for.
|
||||||
|
* @return Album art.
|
||||||
|
*/
|
||||||
|
@Transient
|
||||||
|
private static Optional<ImageIcon> getAlbumArt(Album album){
|
||||||
Tag audioTags;
|
Tag audioTags;
|
||||||
File targetFile = songs.iterator().next().getSongFile();
|
File targetFile = album.songs.iterator().next().getSongFile();
|
||||||
Path baseDir = targetFile.getParentFile().toPath();
|
Path baseDir = targetFile.getParentFile().toPath();
|
||||||
try {
|
try {
|
||||||
audioTags = AudioFileIO.read(targetFile).getTag();
|
audioTags = AudioFileIO.read(targetFile).getTag();
|
||||||
|
Loading…
Reference in New Issue
Block a user