运行时出现在运行 .wav 音频时。

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

Runtime error showing in running .wav audio

问题

import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.*;

class SoundTest {
    public static void main(String[] args) throws Exception {
        URL file = new URL("file:C:/Users/a/Documents/My/baby.wav");

        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        Clip clip = AudioSystem.getClip();
        clip.open(ais);

        JButton button = new JButton("Play Clip");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                clip.setFramePosition(0);
                clip.start();
            }
        });

        JOptionPane.showMessageDialog(null, button);
    }
}

错误信息:

javax.sound.sampled.UnsupportedAudioFileException:
无法从输入URL获取音频输入流(位于javax.sound.sampled.AudioSystem)

请有人帮我解决问题。

英文:
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.*;

class SoundTest
{
	public static void main(String[] args) throws Exception
	{
		URL file = new URL("file:C:/Users/a/Documents/My/baby.wav");

		AudioInputStream ais = AudioSystem.getAudioInputStream(file);
		Clip clip = AudioSystem.getClip();
		clip.open(ais);

		JButton button = new JButton("Play Clip");
		button.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent ae)
			{
				clip.setFramePosition(0);
				clip.start();
			}
		});

		JOptionPane.showMessageDialog(null, button);
	}
}

I'm trying this code but when I am trying to run it is showing error -

javax.sound.sampled.UnsupportedAudioFileException:
    could not get audio input stream from input URL (in javax.sound.sampled.AudioSystem) 

Please anyone solve my problem

答案1

得分: 2

看起来最有可能发生的是有两种情况之一(或者两种情况都有可能发生)。其中一种情况是文件的寻址不正确。另一种情况是该文件是 Java 不支持的格式。

我会在评论中发出信息请求,但那里的情况变得相当混乱了!

你能告诉我们这个 wav 文件的格式吗?尝试检查它的属性。毫无疑问,Java 将支持 16 位,44100 fps,单声道或立体声,小端序,这是最常见的格式。如果来自您的操作系统的属性不完整,您可以使用 Audacity 来检查属性。现在有很多文件是 24 位或者 32 位,或者是 48000fps,甚至是 92000fps,而 Java 目前不支持它们,据我所知。

一个“安全”的测试声音可以是这个雷声,您可以在 Freesound.org 上找到它。您可以在页面右侧看到规格,它是 16 位,44100 fps 立体声。我建议您将其下载并添加到您的程序中,而不是尝试从网络位置播放它。

为了帮助测试寻址是否是一个问题,我会将声音放在与您的 SoundTest.java 文件相同的文件夹中。然后,使用以下代码获取文件的 URL。

URL url = this.getClass().getResource("soundfilename.wav");

这将在项目文件夹中搜索资源。虽然有其他用于不同目的的寻址模式,但这应该有助于使基本代码正常工作。如果确实需要,可以在验证您拥有可工作的声音文件后,让您的程序直接从磁盘位置读取数据。

英文:

It seems most likely that one (or both!) of two things could be happening. One is that the addressing of the file is not working. The other is that the file is a format that Java does not support.

I'd put a request for info in the comments but things are getting quite cluttered there!

Can you tell us the format of the wav file? Try checking it's properties. For sure, Java will support 16-bit, 44100 fps, mono or stereo, little endian, which is the most common format. You can check properties using Audacity if the properties from your OS are incomplete. There are a lot more files these days that are 24-bit or 32-bit, or 48000fps or even 92000fps, and Java doesn't support them yet, afaik.

A "safe" test sound would be this thunder clap at Freesound.org. You can see on the specs on the right side of the page it's 16-bit, 44100 fps stereo. I would download it and add it to your program rather than trying to play it from its web location.

To help test if the addressing is a problem, I would put the sound in the same folder as your SoundTest.java file. Then, use the following line to get the file's URL.

URL url = this.getClass().getResource("soundfilename.wav");

This will search in the project folder for the resource. There are other addressing modes for different purposes, but this should help with getting the basic code working. Having your program read directly from a disc location, if truly needed, can be worked out after verifying that you have a working sound file.

答案2

得分: 0

URL输入有误,请尝试以下内容:

C:\\Users\\a\\Documents\\My\\baby.wav

我还建议从资源文件夹中读取文件,而不是从系统的任何位置读取,这篇博文对资源的解释做得不错。

如果你想要更简便的方式来访问不同的文件,我注意到你正在使用Swing库,可以考虑使用JFileChooser组件。

编辑:更改了URL路径。

英文:

The string for the URL you're inputting is incorrect, try this instead:

C:\\Users\\a\\Documents\\My\\baby.wav

I also recommend reading files form a resources folder instead of from anywhere on your system, this blog post does a decent job explaining resources.

If you would like an easier way to access different files, I see that you're using the Swing library so consider using the JFileChooser component.

Edit: Changed URL path

huangapple
  • 本文由 发表于 2020年10月26日 19:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64536221.html
匿名

发表评论

匿名网友

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

确定