Java AudioSystem.getAudioFileTypes() 在 Android 中返回空数组。

huangapple go评论68阅读模式
英文:

Java AudioSystem.getAudioFileTypes() returns empty array in android

问题

以下是您要翻译的代码部分:

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();

    }
}

请注意,我已将双引号 " 替换为正常的双引号 "。这是因为在代码中," 被用作双引号的转义序列。如果您在实际代码中使用双引号,不需要进行替换。

英文:

The goal: Play wav sound with the help of AudioinputStream and AudioSystem in Java on android

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();
}
}

The code works fine.
The probem is after this line:
System.out.println("supported formats: ");

Android: types={}
PC (Windows): types={"wav,"au","aif"}
Why? - How can I add these supported File types to work on android as well?

答案1

得分: 0

我明白了。

AudioTrack 是 Android 设备上的解决方案。

英文:

I figured out.

AudioTrack is the solution on Android devices.

huangapple
  • 本文由 发表于 2020年8月7日 01:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63288833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定