Tugas PBO B Music Organizer
Kali ini saya akan membahas tugas untuk membuat music organizer sederhana melalui Blue J.
Saya menggunakan beberapa class, 2 diantaranya yaitu music organizer class & music player class, sisanya saya import dari java.jar.
Dibawah ialah source code beserta screenshotnya
Class music player
Class music organizer
jangan lupa download javazoom untuk mengimport clas tambahan lalu taruh di folder +libs pada projectnya
Class music player
import java.io.BufferedInputStream;// panggil class melalui +libs
import java.io.FileInputStream;// fungsi class library bisa diunduh di internet
import java.io.InputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
/**
* @author (Vinsensius Yuda P)
* @version (1.0.0)
*/
public class MusicPlayer
{
///player music awalnya masih kosong
private AdvancedPlayer player;
//membuat object class music player
public MusicPlayer()
{
player = null;
}
public void playSample(String filename)
{
try {
setupPlayer(filename);
player.play(500);
}
catch(JavaLayerException e) {
reportProblem(filename);
}
finally {
killPlayer();
}
}
/**
* mulai memain setelah diberi file musik
*/
public void startPlaying(final String filename)
{
try {
setupPlayer(filename);
Thread playerThread = new Thread() {
public void run()
{
try {
player.play(5000);
}
catch(JavaLayerException e) {
reportProblem(filename);
}
finally {
killPlayer();
}
}
};
playerThread.start();
}
catch (Exception ex) {
reportProblem(filename);
}
}
public void stop()
{
killPlayer();
}
/**
* player siap memutarkan musik yang ada di list.
*/
private void setupPlayer(String filename)
{
try {
InputStream is = getInputStream(filename);
player = new AdvancedPlayer(is, createAudioDevice());
}
catch (IOException e) {
reportProblem(filename);
killPlayer();
}
catch(JavaLayerException e) {
reportProblem(filename);
killPlayer();
}
}
private InputStream getInputStream(String filename)
throws IOException
{
return new BufferedInputStream(
new FileInputStream(filename));
}
/**
* buat audiodevice
*/
private AudioDevice createAudioDevice()
throws JavaLayerException
{
return FactoryRegistry.systemRegistry().createAudioDevice();
}
/**
* matikan player apa bila lagu selesai
*/
private void killPlayer()
{
synchronized(this) {
if(player != null) {
player.stop();
player = null;
}
}
}
/**
* apabila ada masalah buat report
*/
private void reportProblem(String filename)
{
System.out.println("There was a problem playing: " + filename);
}
}
Class music organizer
import java.util.ArrayList;
/**
* @author (Vinsensius Yuda P)
* @version (1.0.0)
*/
public class MusicOrganizer
{
// sebuah arraylist untuk mentimpan nama file musik
private ArrayList<String> files;
// class musicplayer
private MusicPlayer player;
/**
* membuat sebuah music organizer
*/
public MusicOrganizer()
{
files = new ArrayList<>();
player = new MusicPlayer();
}
/**
* menembahkan sebuah file ke koleksi
*/
public void addFile(String filename)
{
files.add(filename);
}
/**
* return file ke koleksi
*/
public int getNumberOfFiles()
{
return files.size();
}
/**
* list sebuah file dari koleksi
*/
public void listFile(int index)
{
if(index >= 0 && index < files.size()) {
String filename = files.get(index);
System.out.println(filename);
}
}
/**
* remove file di list/koleksi
*/
public void removeFile(int index)
{
if(index >= 0 && index < files.size()) {
files.remove(index);
}
}
/**
* mulai memainkan file musik yang ada di list
*/
public void startPlaying(int index)
{
String filename = files.get(index);
player.startPlaying(filename);
}
/**
* hentikan player musik
*/
public void stopPlaying()
{
player.stop();
}
}
jangan lupa download javazoom untuk mengimport clas tambahan lalu taruh di folder +libs pada projectnya
Buat musicorganizer baru
Copy path file music yang akan diputar
Hasil list dari music organizer
Mainkan music dengan void startPlaying
Komentar
Posting Komentar