Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Playing WAV sounds

Tags:

java

audio

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: 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?

like image 253
Dan Burzo Avatar asked Dec 03 '25 22:12

Dan Burzo


1 Answers

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.

like image 170
Zach Scrivena Avatar answered Dec 05 '25 12:12

Zach Scrivena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!