Java的声音API不适用于所有.WAV文件? [英] Java's sound API doesn't work with all .WAV files?

查看:149
本文介绍了Java的声音API不适用于所有.WAV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无法使用 javax.sound.sampled 库来解决问题。以下是用于播放音频文件的 MCV 代码:

  import javax.sound.sampled。*; 
import java.io.File;

public class Foo
{
public static void main(String [] args)
{
try
{
File f = new File(alarm.wav);音频输入
剪辑剪辑= AudioSystem.getClip();

clip.open(ais);
clip.start();
}
catch(异常e){}
}
}

此代码有时会抛出 UnsupportedAudioFileException



基本上,我有5个.WAV文件,我知道是没有坏的,因为当我在我的音乐播放软件中打开它们时,它们玩得很好。但是,Java程序只适用于其中的3个。



Oracle在这里提到对此文件格式的支持如何确保我的所有.WAV文件与Java的音频API兼容?如果由于某些原因他们没有适当的编码,是否有一个万无一失的玩WAV文件?

解决方案

您可以使用以下代码获取支持的播放格式列表:

  static List< AudioFormat> getSupportedAudioFormats(){
列表< AudioFormat> result = new ArrayList< AudioFormat>();
for(Line.Info info:AudioSystem.getSourceLineInfo(
new Line.Info(SourceDataLine.class))){
if(info instanceof SourceDataLine.Info){
result。 addAll(Arrays.asList(
((SourceDataLine.Info)info).getFormats()));
}
}
返回结果;
}

AudioFormat.Encoding 列出了支持的编码 javax.sound.sampled



安全的WAV音频格式是44100Hz的16位PCM。 >

您可以通过以下方式发现特定文件的格式:

 文件文件=新文件(path_to_file.wav); 
AudioFormat fmt = AudioSystem.getAudioFileFormat(file).getFormat();

这比尝试获取一行更宽松一些,但仍然会抛出如果WAV文件有例如mp3数据。 WAV文件是一个可以存储PCM以外的编码的容器,其中一些 javax.sound.sampled 通常不支持。


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 javax.sound.sampled.*;
import java.io.File;

public class Foo
{
    public static void main(String[] args)
    {
        try
        {
            File f = new File("alarm.wav");
            AudioInputStream ais = AudioSystem.getAudioInputStream(f);
            Clip clip = AudioSystem.getClip();

            clip.open(ais);
            clip.start();
        }
        catch (Exception e) {}
    }
}

This code will sometimes throw an UnsupportedAudioFileException.

Basically, I have 5 .WAV files that I know are uncorrupted because they play perfectly fine when I open them in my music playing software. However, the Java program only works with 3 of them.

Oracle mentions support for this file format here. How can I make sure that all of my .WAV files are compatible with Java's audio API? Is there a foolproof way of playing .WAV files if for some reason they do not have the appropriate encoding?

解决方案

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

static List<AudioFormat> getSupportedAudioFormats() {
    List<AudioFormat> result = new ArrayList<AudioFormat>();
    for(Line.Info info : AudioSystem.getSourceLineInfo(
            new Line.Info(SourceDataLine.class))) {
        if(info instanceof SourceDataLine.Info) {
            result.addAll(Arrays.asList(
                ((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.

这篇关于Java的声音API不适用于所有.WAV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆