正在运行的.wav音频中显示运行时错误 [英] Runtime error showing in running .wav audio

查看:157
本文介绍了正在运行的.wav音频中显示运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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) 

请任何人解决我的问题

推荐答案

似乎很可能发生两件事之一(或两者都有!).一种是文件的寻址不起作用.另一个是该文件是Java不支持的格式.

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!

您能告诉我们wav文件的格式吗?尝试检查其属性.当然,Java将支持16位,44100 fps,单声道或立体声,小端格式,这是最常见的格式.如果操作系统中的属性不完整,则可以使用Audacity检查属性.如今,有更多的文件是24位或32位,48000fps甚至92000fps,而Java尚不支持它们,afaik.

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.

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

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.

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

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.

这篇关于正在运行的.wav音频中显示运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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