我正在尝试将音频播放器添加到我的应用程序中。这是处理音频播放的类中的代码:
package me.pogostick29.audiorpg.audio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class AudioPlayer {
private AudioPlayer() { }
private static AudioPlayer instance = new AudioPlayer();
public static AudioPlayer getInstance() {
return instance;
}
private static ArrayList<BigClip> clips = new ArrayList<BigClip>();
private BigClip get(String name) {
for (BigClip clip : clips) {
if (clip.getName().equalsIgnoreCase(name)) return clip;
}
return null;
}
public void play(File file) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
BigClip clip = get(file.getName());
if (clip == null) {
BigClip newClip = new BigClip(file.getName());
clips.add(newClip);
clip = newClip;
}
clip.open(audioIn);
clip.start();
}
catch (Exception e) { e.printStackTrace(); }
}
}
但是,当我尝试使用以下命令运行它时:
AudioPlayer.getInstance().play(new File("audio/people/blacksmith/blacksmith01a_new.wav"));
我得到以下堆栈跟踪:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170)
at me.pogostick29.audiorpg.audio.AudioPlayer.play(AudioPlayer.java:38)
at me.pogostick29.audiorpg.person.people.Blacksmith.playDialogue(Blacksmith.java:12)
at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:33)
这是我在使用 Audition 导出文件时使用的格式。