Java Wav 文件错误(javax.sound.sampled.UnsupportedAudioFileException:无法从输入文件获取音频输入流)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14943962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Wav file Error (javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file)
提问by user1991965
I have two Classes in my Java Project one is
我的 Java 项目中有两个类,一个是
Rebound
反弹
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Rebound");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
and the second one is ReboundPanel
第二个是 ReboundPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
public class ReboundPanel extends JPanel
{
private final int WIDTH = 600, HEIGHT = 300;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image1, image2, image3;
private Timer timer;
private int x1, y1, moveX1, moveY1, x2, y2, moveX2, moveY2, x3, y3;
Clip clip;
DataLine.Info info;
public ReboundPanel() throws Exception
{
timer = new Timer(DELAY, new ReboundListener());
image1 = new ImageIcon ("happyFace1.gif");
image2 = new ImageIcon ("happyFace2.gif");
x1 = 0;
y1 = 100;
x2 = 40;
y2 = 0;
moveX1 = moveY1 = 3;
moveX2 = moveY2 = 5;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.start();
image3 = new ImageIcon ("fire.gif");
File soundFile = new File("explosion.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info (Clip.class, sound.getFormat());
clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
clip.start();
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image1.paintIcon (this, page, x1, y1);
image2.paintIcon (this, page, x2, y2);
image3.paintIcon (this, page, x2, y2);
}
private class ReboundListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x1 += moveX1;
y1 += moveY1;
x2 += moveX2;
y2 += moveY2;
if (x1 <= 0 || x1 >= WIDTH-IMAGE_SIZE)
moveX1 = moveX1 * -1;
if (y1 <= 0 || y1 >= HEIGHT-IMAGE_SIZE)
moveY1 = moveY1 * -1;
if (x2 <= 0 || x2 >= WIDTH-IMAGE_SIZE)
moveX2 = moveX2 * -1;
if (y2 <= 0 || y2 >= HEIGHT-IMAGE_SIZE)
moveY2 = moveY2 * -1;
if (Math.abs(y1-y2) <= IMAGE_SIZE-2 && Math.abs(x1-x2)<= IMAGE_SIZE-2)
{
moveY2 = moveY2 * -1;
moveX2 = moveX2 * -1;
moveY1 = moveY1 * -1;
moveX1 = moveX1 * -1;
x3=x2;
y3=y2-2*IMAGE_SIZE;
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
if (Math.abs(y1-y2) >= 4*IMAGE_SIZE && Math.abs(x1-x2) >= 4*IMAGE_SIZE)
{
x3=-200;
y3=-200;
if (clip.isRunning()) clip.stop();
}
repaint();
}
}
}
When I try to run the program I get the following error
当我尝试运行该程序时,出现以下错误
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
at ReboundPanel.<init>(ReboundPanel.java:47)
at Rebound.main(Rebound.java:21)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ReboundPanel$ReboundListener.actionPerformed(ReboundPanel.java:99)
at javax.swing.Timer.fireActionPerformed(Timer.java:312)
at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:682)
at java.awt.EventQueue.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
According to my Code my goal is to make two happy faces move in the frame and when they both collide a sound along with an image of fire/explosion should show up and disappear
根据我的代码,我的目标是让两个快乐的脸在框架中移动,当它们碰撞时,声音和火焰/爆炸的图像应该出现并消失
Please help I have to show this project to my instructor...
请帮助我必须向我的导师展示这个项目......
回答by Bjorn Roche
Just because a file is a WAV doesn't mean it is supported by JavaSound. A WAV file is a container format, and the actual raw data (the samples) may be in different formats, including signed/unsigned PCM at a variety of bit depths or it may be in a compressed format. I can't find the compatibility table for JavaSound, but the compatibility table for JMF (which will give you a good idea) is here:
仅仅因为文件是 WAV 并不意味着它受 JavaSound 支持。WAV 文件是一种容器格式,实际的原始数据(样本)可能采用不同的格式,包括各种位深度的有符号/无符号 PCM,也可能采用压缩格式。我找不到 JavaSound 的兼容性表,但 JMF 的兼容性表(这会给你一个好主意)在这里:
http://www.oracle.com/technetwork/java/javase/formats-138492.html
http://www.oracle.com/technetwork/java/javase/formats-138492.html
The solution is to convert the WAV file to a more generic format, like 16-bit PCM using a conversion program that can read your current file (SoX, Adacity, QuickTime, ProTools, etc).
解决方案是使用可以读取当前文件(SoX、Adacity、QuickTime、ProTools 等)的转换程序将 WAV 文件转换为更通用的格式,例如 16 位 PCM。
To be more precise, you may want to create a WAV file with a bit depth of 16-bits (little endian -- this is the default, so if you don't see it don't worry), stereo or mono should work. You may also want to consider common sample rates such as 44,100 Hz, or 48,000 Hz (most computers nowadays should support both rates, but the former is somewhat more common).
更准确地说,您可能想要创建一个位深度为 16 位(小端 - 这是默认值,所以如果您没有看到它,请不要担心)的 WAV 文件,立体声或单声道应该可以工作. 您可能还想考虑常见的采样率,例如 44,100 Hz 或 48,000 Hz(现在大多数计算机都应该支持这两种采样率,但前者更为常见)。
回答by Andrew Thompson
An
UnsupportedAudioFileException
is an exception indicating that an operation failed because a file did not contain valid data of a recognized file type and format.
An
UnsupportedAudioFileException
是一个异常,表示操作失败,因为文件不包含可识别文件类型和格式的有效数据。
If the question were "Why don't the samples play?" the answer would be above. Another way to put it is "The sound uses a format or encoding that the Java Sound API does not understand".
如果问题是“为什么样本不播放?” 答案就在上面。另一种说法是“声音使用了 Java Sound API 无法理解的格式或编码”。
..What is your question?
..你的问题是什么?