Playing ogg files in eclipse

2020-07-22 09:51发布

问题:

So i'm trying to add sound to my pong game, but for some reason i can't seem to get the sounds to play... There are no error messages, meaning the path is correct, but the sounds simply don't play.

The following is my code for background music, with all the pong stuff cut out, thanks in advance~

    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.net.URL;
    import javax.swing.JApplet;
    import javax.swing.JPanel;

    public class pong1 extends JPanel {

        static AudioClip music;
        static URL path; // soundfile path

        public static void main(String[] args) {

            path = pong1.class.getResource("Battle2.ogg"); // change url
            music = Applet.newAudioClip(path); // load sound
            music.loop();
        }
    }

回答1:

Here is a class that plays a OGG file using javazoom library:

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javazoom.spi.PropertiesContainer;

public class OGGPlayer {
    public final String fileName;

    public boolean mustStop = false;


    public OGGPlayer(String pFileName) {
        fileName = pFileName;
    }

    public void play() throws Exception {
        mustStop = false;
        File file = new File(fileName);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
        if (audioInputStream == null) {
            throw new Exception("Unable to get audio input stream");
        }
        AudioFormat baseFormat = audioInputStream.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
            baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
            baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat,
            audioInputStream);
        if (!(decodedAudioInputStream instanceof PropertiesContainer)) {
            throw new Exception("Wrong PropertiesContainer instance");
        }

        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, decodedFormat);
        SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(decodedFormat);

        byte[] tempBuffer = new byte[4096];

        // Start
        sourceDataLine.start();
        int nbReadBytes = 0;
        while (nbReadBytes != -1) {
            if (mustStop) {
                break;
            }
            nbReadBytes = decodedAudioInputStream.read(tempBuffer, 0, tempBuffer.length);
            if (nbReadBytes != -1)
                sourceDataLine.write(tempBuffer, 0, nbReadBytes);
        }

        // Stop
        sourceDataLine.drain();
        sourceDataLine.stop();
        sourceDataLine.close();
        decodedAudioInputStream.close();
        audioInputStream.close();
    }

    public void setMustStop(boolean pMustStop) {
        mustStop = pMustStop;
    }

    public void stop() {
        mustStop = true;
    }

}

Simply call this class in a new Thread of your application so that the music is played in background.