tags:

views:

1907

answers:

2

Hi,

I am trying to build a java drum machine that needs to play WAV sound samples of the various drum parts (bass drum, snare, etc). Because I need to play the sounds in a tight sequence, I need high performance. Currently I'm using:

import sun.audio.*;
import java.io.*;

public class MusicPlayer {

    private String filename;

    public MusicPlayer(String filename) {
        this.filename = filename;
    }

    public void play() {
     try {
      InputStream in = new FileInputStream(filename);
      AudioStream as = new AudioStream(in);
      AudioPlayer.player.start(as); 

     } catch (IOException e) {
      e.printStackTrace();
     }          
    }
}

as suggested here: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java

While it does work faster than MP3 + Javazoom jLayer, it still sounds choppy at high tempo and when I do cpu intensive stuff like resizing the app window.

Any tips on improving performance?

BTW. I've also read that sun.audio.* is deprecated. Is there a similar solution?

+1  A: 

Have you looked at the Java Media Framework (JMF):

The Java Media Framework API (JMF) enables audio, video and other time-based media to be added to applications and applets built on Java technology. This optional package, which can capture, playback, stream, and transcode multiple media formats, extends the Java 2 Platform, Standard Edition (J2SE) for multimedia developers by providing a powerful toolkit to develop scalable, cross-platform technology.

Zach Scrivena
A: 

Perhaps you should put the sounds in cache: if you load them from disk each time you play them, it is indeed slow. Now, you might have a memory problem, but it depends on your sounds (size, number...).

PhiLho