伙计们正在尝试借助声音 SPI 读取不同的音频格式。我已经在类路径中添加了声音 SPI,但由于某种原因,它仍然没有被使用/或未被检测到。
我正在使用 vorbisspi 并且收到不支持的音频文件格式错误。
这是我的毕业典礼
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()
}
这是播放音频文件的类
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.");
}
}
}
尝试读取 .ogg,.ogx,.opus 时会抛出错误,指出其格式不受支持