play audio in java - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


play audio in java

saran185saran185 Posts: 23Member
is it possible to play any format of audio file with the same code. below i ve the code to play mp3 audio files. now is it possible for me to include code that would play wav files also..if so what are the changes to be included....

import javazoom.jl.player.Player;

public class MP3 {
private String filename;
private Player player;

// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;

public void close() { if (player != null) player.close(); }

// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
catch (Exception e) {
System.out.println("Problem playing file " + filename);

// run in new thread to play in background
new Thread() {
public void run() {
try {; }
catch (Exception e) { System.out.println(e); }


// test client
public static void main(String[] args) {
String filename = args[0];
MP3 mp3 = new MP3(filename);;

// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);

// when the computation is done, stop playing it

// play from the beginning
mp3 = new MP3(filename);;


Sign In or Register to comment.