Java: play WAV sounds

I am trying to create a java drum machine that should play samples of WAV sound from various parts of a drum (bass drum, trap, etc.). Since I need to play sounds in a difficult sequence, I need high performance. I am currently 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 is faster than the MP3 + Javazoom jLayer, it still sounds choppy at high speeds, and when I do an intensive processor process, like resizing the application window.

Any tips on improving performance?

BTW. I also read that I am sun.audio.*out of date. Is there a similar solution?

+3
source share
3

Java Media Framework (JMF):

API Java Media Framework (JMF) , , , Java . , , , , , Java 2, Standard Edition (J2SE) , , - .

+1

, : , , . , (, ...).

+1

One thing you could try is to make a couple of extra threads.

If you can play two sounds at once, just create a buffer with 5 instances of the same sound and play one of the available ones.

+1
source

Source: https://habr.com/ru/post/1702705/


All Articles