Java's sound API doesn't work with all .WAV files?

后端 未结 2 2039
心在旅途
心在旅途 2021-01-24 23:36

I am currently having trouble playing around with the javax.sound.sampled library. Here\'s the MCV code that I use to play my audio files:

import ja         


        
2条回答
  •  太阳男子
    2021-01-24 23:40

    You can use the following code to get a list of the supported formats for playback:

    public static List getSupportedAudioFormats() {
        List result = new ArrayList<>();
        for (Line.Info info : AudioSystem.getSourceLineInfo(
                    new Line.Info(SourceDataLine.class))) {
            if (info instanceof SourceDataLine.Info) {
                Collections.addAll(result, ((SourceDataLine.Info) info).getFormats());
            }
        }
        return result;
    }
    

    AudioFormat.Encoding lists the encodings supported by javax.sound.sampled.

    A safe WAV audio format is 16-bit PCM at 44100Hz.

    You can discover the format of a particular file with:

    File file = new File("path_to_file.wav");
    AudioFormat fmt = AudioSystem.getAudioFileFormat(file).getFormat();
    

    This will be a little more lenient than trying to get a line for example, but it will still throw if the WAV file has e.g. mp3 data. A WAV file is a container that can store encodings beyond PCM, some of which javax.sound.sampled does not normally support.

提交回复
热议问题