gpt4 book ai didi

java - Java AudioSystem.getAudioFileTypes()在android中返回空数组

转载 作者:行者123 更新时间:2023-12-03 01:12:51 25 4
gpt4 key购买 nike

目标:借助Android上的Java中的AudioinputStream和AudioSystem播放wav声音

private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

public void playSound(){

AudioFileFormat.Type[] types=AudioSystem.getAudioFileTypes();

System.out.println("supported formats: ");
for(AudioFileFormat.Type t1: types){
System.out.println(t1.getExtension());
}

try {
audioStream = AudioSystem.getAudioInputStream(Thread.currentThread().getContextClassLoader().getResource("res/raw/" + "tobu_wav" + ".wav"));


audioFormat = audioStream.getFormat();

DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();

} catch (Exception e) {
e.printStackTrace();

}

sourceLine.start();

int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}

sourceLine.drain();
sourceLine.close();
} catch (Exception e){
e.printStackTrace();

}
}
该代码工作正常。
该探针位于此行之后:
System.out.println(“支持的格式:”);
Android:类型= {}
PC(Windows):types = {“wav,” au“,” aif“}
为什么? -如何添加这些受支持的文件类型以在android上正常工作?

最佳答案

我想通了。
AudioTrack是Android设备上的解决方案。

关于java - Java AudioSystem.getAudioFileTypes()在android中返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63288833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com