0

guys am trying to read different audio formats with help of sound SPI. I have added sound SPI to classpath but still for some reason its not being used/or not detected.

Am using vorbisspi and am getting unsupported audio file format error.

here is my gradle

buildscript{
    dependencies {

       // classpath // https://mvnrepository.com/artifact/com.github.trilarion/vorbis-support
       classpath group: 'com.github.trilarion', name: 'vorbis-support', version: '1.1.0'

    }
}

plugins {
    id 'java'

}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {


}

test {
    useJUnitPlatform()
}

here is the class which plays audio file

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Player implements LineListener {

    boolean playCompleted;

    public void play(String audioFilePath) {
        File audioFile = new File(audioFilePath);

        try {
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

            AudioFormat format = audioStream.getFormat();

            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip audioClip = (Clip) AudioSystem.getLine(info);

            audioClip.addLineListener(this);

            audioClip.open(audioStream);

            audioClip.start();

            while (!playCompleted) {
                // wait for the playback completes
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            audioClip.close();

        } catch (UnsupportedAudioFileException ex) {
            System.out.println("The specified audio file is not supported.");
            ex.printStackTrace();
        } catch (LineUnavailableException ex) {
            System.out.println("Audio line for playing back is unavailable.");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Error playing the audio file.");
            ex.printStackTrace();
        }

    }
    @Override
    public void update(LineEvent event) {
        LineEvent.Type type = event.getType();

        if (type == LineEvent.Type.START) {
            System.out.println("Playback started.");

        } else if (type == LineEvent.Type.STOP) {
            playCompleted = true;
            System.out.println("Playback completed.");
        }

    }
}

when trying to read .ogg,.ogx,.opus it throws error saying that its unsupported format

klaus
  • 43
  • 1
  • 5
  • If this is on macOS or Windows, you might want to try https://www.tagtraum.com/ffsampledsp/index.html – Hendrik Jan 06 '21 at 13:08
  • @Hendrik i need build and install ffsampledsp-x86_64-macos right before using it in my program right – klaus Jan 07 '21 at 07:54
  • To use FFSampledSP you need to declare the dependency as mentioned on the website (no idea how that's done with gradle, sorry) and then simply use the regular Java API. Note that IMHO you are not using the API correctly. Most likely you have to convert to the desired format (typically some PCM format supported by your sound adapter), as described [here](https://stackoverflow.com/a/41850901/942774). – Hendrik Jan 07 '21 at 13:14
  • I'm wondering if JOrbis also needs to present for .ogg decoding. Just noticed that the old javazoom site is down! – Phil Freihofner Jan 25 '21 at 00:28
  • @PhilFreihofner I'm not sure I understand your comment, but in case it can be interpreted as 'does Java Sound support OGG by default', then .. no. The audio file types supported in this Java 14(ish could not be bothered checking) enabled PC without any other SPI added is WAVE, AU & AIFF. – Andrew Thompson Jan 25 '21 at 01:56
  • 1
    @AndrewThompson - Understood, OGG is not supported by core Java. IDK the specifics for `vorbisspi.jar`. The Javazoom site was one of few places having documentation. The one app I wrote reading ogg used both JOrbis and JOgg parts. I tweaked the code `JOrbis.DecodeExample` to intercept the PCM before it was converted to bytes. Did not use `vorbisspi.jar`. Was thinking maybe additional jars needed on classpath for the vorbisspi functionality to work. Purely a blind guess. Maybe I shouldn't have commented at all. – Phil Freihofner Jan 25 '21 at 02:48
  • @PhilFreihofner *"Maybe I shouldn't have commented at all."* And maybe you should! I lean toward the latter. ‍♂️ – Andrew Thompson Jan 25 '21 at 03:26

0 Answers0