mp3 metadata complete

This commit is contained in:
Harrison Deng 2018-07-08 18:36:39 -05:00
parent 31bc3eb273
commit 2588b1e4a3
7 changed files with 184 additions and 73 deletions

View File

@ -46,7 +46,6 @@ project(":desktop") {
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
}
}
@ -70,7 +69,6 @@ project(":android") {
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
}
}
@ -84,6 +82,7 @@ project(":core") {
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile "com.github.rwl:jtransforms:2.4.0"
compile "org.apache.commons:commons-math3:3.2"
compile group: 'com.mpatric', name: 'mp3agic', version: '0.9.1'
}
}

View File

@ -4,29 +4,28 @@ import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Disposable;
public class AudioMetadata implements Disposable {
private String author;
private String length;
private Texture albumCover;
private String lastPlayed;
private FileHandle fileHandle;
public interface AudioMetadata extends Disposable {
public AudioMetadata(FileHandle fileHandle) {
this.fileHandle = fileHandle;
}
public void loadAlbumCover();
public void loadAlbumCover() {
public void unloadAlbumCover();
}
public String getAuthor();
public void unloadAlbumCover() {
public String getLength();
}
public Texture getAlbumCover();
public String getLastPlayed();
public String getGenre();
public FileHandle getFileHandle();
public RhythmBulletMetadata getRBMetadata();
public void flushRBMetadata();
@Override
public void dispose() {
// TODO Auto-generated method stub
}
public void dispose();
}

View File

@ -0,0 +1,132 @@
package zero1hd.rhythmbullet.audio;
import java.io.IOException;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json;
import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
public class MP3Metadata implements AudioMetadata {
private String author = "N/A";
private String length;
private String genre;
private String lastPlayed;
private Texture albumCover;
private FileHandle fileHandle;
private RhythmBulletMetadata rbMetadata;
private Json json;
public MP3Metadata() {
json = new Json();
try {
Mp3File mp3file = new Mp3File(fileHandle.file());
long lenInSec = mp3file.getLengthInSeconds();
int min = (int) (lenInSec/60);
length = (lenInSec/60) + ":" + (lenInSec - (min*60));
if (mp3file.hasId3v1Tag()) {
ID3v1 tag = mp3file.getId3v1Tag();
author = tag.getArtist();
genre = tag.getGenreDescription();
}
if (mp3file.hasCustomTag()) {
rbMetadata = json.fromJson(RhythmBulletMetadata.class, new String(Base64Coder.decode(new String(mp3file.getCustomTag()))));
} else {
rbMetadata = new RhythmBulletMetadata();
mp3file.setCustomTag(Base64Coder.encodeString(json.toJson(rbMetadata)).getBytes());
}
} catch (UnsupportedTagException e) {
Gdx.app.debug("MP3Metadata", "Tag not supported by MP3agic.");
} catch (InvalidDataException e) {
Gdx.app.debug("MP3Metadata", "This files metadata has errors.");
} catch (IOException e) {
Gdx.app.debug("MP3Metadata", "IO error while reading metadata.");
}
}
@Override
public void loadAlbumCover() {
try {
Mp3File mp3file = new Mp3File(fileHandle.file());
if (mp3file.hasId3v2Tag()) {
byte[] imageData = mp3file.getId3v2Tag().getAlbumImage();
Pixmap pixmap = new Pixmap(imageData, 0, imageData.length);
albumCover = new Texture(pixmap);
pixmap.dispose();
}
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
e.printStackTrace();
}
}
@Override
public void unloadAlbumCover() {
albumCover.dispose();
albumCover = null;
}
@Override
public String getAuthor() {
return author;
}
@Override
public String getLength() {
return length;
}
@Override
public String getGenre() {
return genre;
}
@Override
public Texture getAlbumCover() {
return albumCover;
}
@Override
public String getLastPlayed() {
return lastPlayed;
}
@Override
public FileHandle getFileHandle() {
return fileHandle;
}
@Override
public RhythmBulletMetadata getRBMetadata() {
return rbMetadata;
}
@Override
public void flushRBMetadata() {
try {
Mp3File mp3file = new Mp3File(fileHandle.file());
mp3file.setCustomTag(Base64Coder.encodeString(json.toJson(rbMetadata)).getBytes());
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
e.printStackTrace();
}
}
@Override
public void dispose() {
if (albumCover != null) {
albumCover.dispose();
albumCover = null;
}
}
}

View File

@ -0,0 +1,29 @@
package zero1hd.rhythmbullet.audio;
public class RhythmBulletMetadata {
private String highScore = "0", lastPlayed = "N/A", difficulty = "N/A";
public void setHighScore(String highScore) {
this.highScore = highScore;
}
public void setLastPlayed(String lastPlayed) {
this.lastPlayed = lastPlayed;
}
public void setDifficulty(String difficulty) {
this.difficulty = difficulty;
}
public String getHighScore() {
return highScore;
}
public String getDifficulty() {
return difficulty;
}
public String getLastPlayed() {
return lastPlayed;
}
}

View File

@ -1,49 +0,0 @@
package zero1hd.rhythmbullet.util;
import java.util.HashMap;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json;
public class Base64Preferences {
FileHandle base64File;
HashMap<String, String> prefKeys;
Json json;
public Base64Preferences(String prefName) {
base64File = Gdx.files.external(".prefs/.priv/" + prefName);
json = new Json();
flush();
}
public void putString(String key, String string) {
prefKeys.put(key, string);
}
public void putObj(String key, Object object) {
prefKeys.put(key, json.toJson(object));
}
public void writeBase64String(String text) {
base64File.writeString(Base64Coder.encodeString(text), true);
}
public void writeBase64Object(Object obj) {
base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true);
}
@SuppressWarnings("unchecked")
public void flush() {
prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString())));
if (prefKeys == null) {
prefKeys = new HashMap<>();
}
base64File.writeString(null, false);
writeBase64Object(json);
}
}

View File

@ -11,6 +11,7 @@ import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Sort;
import zero1hd.rhythmbullet.audio.MusicManager;
import zero1hd.rhythmbullet.desktop.audio.processor.MP3AudioProcessor;
public class MusicList extends Observable {
private Array<FileHandle> musicArray;

View File

@ -1,4 +1,4 @@
package zero1hd.rhythmbullet.desktop.audio;
package zero1hd.rhythmbullet.desktop.audio.processor;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;