Untuk contoh ringkas penggunaan JLayer, bisa dilihat di sini. Contoh di sini cukup bagus, sederhana tapi sudah menggunakan Thread. Cocok untuk game.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; public class MP3 { private static InputStream parseArgs(String[] args) { if (args.length != 1) { System.err.println("Usage: java MP3 []"); System.exit(1); } if (args[0].equals('-')) return System.in; try { return new FileInputStream(args[0]); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); return null; } } private static Player play(InputStream in) { final Player player; try { player = new Player(in); } catch (JavaLayerException e) { System.err.println(e); System.exit(1); return null; } new Thread() { public void run() { try { player.play(); } catch (JavaLayerException e) { System.err.println(e); System.exit(1); } } }.start(); return player; } private static void status(String filename, Player player) { int millis = player.getPosition(); int secs = millis / 1000; int mins = secs / 60; int hours = mins / 60; System.out.printf("%s: %4d:%02d:%02d.%03d\r", filename, hours, mins % 60, secs % 60, millis % 1000); } public static void main(String[] args) { InputStream in = parseArgs(args); Player player = play(in); while (!player.isComplete()) { status(args[0], player); try { Thread.sleep(100); } catch (InterruptedException ignored) {} } System.out.println(); } }
Adapun apabila ingin menggunakan file OGG, karena mungkin file OGG itu bebas lisensi berbayar, bisa menggunakan J-OGG.
No comments:
Post a Comment