【问题标题】:Java AudioSystem.getAudioFileTypes() returns empty array in androidJava AudioSystem.getAudioFileTypes() 在 android 中返回空数组
【发布时间】:2020-08-06 17:27:50
【问题描述】:

目标:在 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 上也能工作?

【问题讨论】:

  • 不是您问题的答案,但如果您使用的是 Android,为什么不使用its media API

标签: java audio decode wav pcm


【解决方案1】:

我想通了。

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

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2011-11-06
    • 1970-01-01
    • 2013-02-10
    • 2021-02-11
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多