"Wahai jin dan manusia, jika kalian mampu menembus penjuru langit dan bumi, tembuslah! Kalian tidak dapat menembusnya kecuali dengan kekuatan (ilmu)." (Ar-Rahman:33)

A java program to play simple audio. It also supports mp3 and wav audio extension.

Below is the code for a simple program that loads an audio file.

 


import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;

/**
 *
 * @author ariska hidayat
 */
public class PlayingAudio {
 public static void main(String[] args)
 {

 SoundJLayer soundToPlay = new SoundJLayer("Rapuh.mp3");

soundToPlay.play();

 soundToPlay.getTread().start();
 }
}
class SoundJLayer extends PlaybackListener implements Runnable
{
 private String filePath;
 private AdvancedPlayer player;
 private Thread playerThread;

public SoundJLayer(String filePath)
 {
 this.filePath = filePath;
 }

public void play()
 {
 try
 {
 String urlAsString =
 "file:///"
 + new java.io.File(".").getCanonicalPath()
 + "/"
 + this.filePath;

System.out.println("file"+urlAsString);
 this.player = new AdvancedPlayer
 (
 new java.net.URL(urlAsString).openStream(),
 javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice()
 );

this.player.setPlayBackListener(this);

this.playerThread = new Thread(this, "AudioPlayerThread");

// this.playerThread.start();

 }
 catch (IOException | JavaLayerException ex)
 {
 }
 }

public AdvancedPlayer getAdvacedPlayer(){
 return player;
 }

 public Thread getTread(){
 return playerThread;
 }

 // PlaybackListener members

@Override
 public void playbackStarted(PlaybackEvent playbackEvent)
 {
 System.out.println("playbackStarted()");
 }

@Override
 public void playbackFinished(PlaybackEvent playbackEvent)
 {
 System.out.println("playbackEnded()");
 }

// Runnable members

@Override
 public void run()
 {
 try
 {
 this.player.play();
 }
 catch (javazoom.jl.decoder.JavaLayerException ex)
 {
 }

}
}

you can download this project: <download>
(this project uses netbeans 7.4)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: