为什么即使使用.wav文件,我的Java程序也不会添加声音? [英] Why won't my java program add sound even with .wav file?

查看:110
本文介绍了为什么即使使用.wav文件,我的Java程序也不会添加声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来测试声音.为此,我正在观看一个视频,该视频的链接可以在这里 https://www. youtube.com/watch?v=VMSTTg5EEnY .但是,当我运行它时,它会创建一个带有按钮的框架,但是该按钮不会执行任何操作.有什么问题吗?

I am making a program just to test around with sound. To make it I was following a video which a link to can be found here https://www.youtube.com/watch?v=VMSTTg5EEnY. However, when I run it, it makes a frame with a button, but the button doesn't do anything. What is the problem?

   package RefrenceCode;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import java.awt.event.ActionListener;

public class Sound {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(200,200);
        JButton button = new JButton("Click me");
        frame.add(button);
        button.addActionListener(new AL());
        frame.show(true);
    }
    public static class AL implements ActionListener{
        public final void actionPerformed(ActionEvent e) {
            music();
        }}
    public static void music(){
        AudioPlayer BGP = AudioPlayer.player;
        AudioStream BGM;
        AudioData MD;
        ContinuousAudioDataStream loop = null;
        try {
            //InputStream test = new FileInputStream("C:\\ wiiMusic.wav");
            //BGM = new AudioStream(test);
            BGM = new AudioStream(new FileInputStream("wiiMusic.wav"));
            MD = BGM.getData();
            loop = new ContinuousAudioDataStream(MD);
        }catch(IOException error) {}
        BGP.start(loop);
    }
}

推荐答案

过时的教程似乎是一个日益严重的问题!

Obsolete tutorials seem to be a growing problem!

我将OP注释解释为询问以下问题:如何处理和加载本地音频文件,即与Java程序打包在一起的音频文件,而不是远程访问.

I am interpreting the OP comment as asking the following question: how to address and load an audio file that is local, i.e., packaged with the java program, not access remotely.

由安德鲁·汤普森(Andrew Thompson)在评论中提供的链接很有用,但没有提供解决独立音频文件的示例.我不知道如何编辑它以包含示例.不论出于何种原因,《 Oracle Sound Trail教程》都存在类似的缺陷.

The link provided by Andrew Thompson in the comments is useful, but it does not have an example of addressing a self-contained audio file. I don't know how to edit it to include an example. The Oracle Sound Trail Tutorial is similarly deficient, for whatever reason.

首先,最常见的是创建AudioInputStream的URL形式:

First off, the URL form for creating the AudioInputStream is most usually best:

AudioInputStream ais = AudioSystem.getAudioInputStream(url);

为什么?因为它具有定位打包在jar中的资源的附加功能,而文件系统则不能.

Why? Because it has the additional capability of locating resources that are packed in a jar, whereas the File system cannot.

我认为访问独立音频资源的最常见做法是利用

I think the most usual practice for accessing a self-contained audio resource is to make use of the ClassLoader for a Class that you are sure will be part of your project. The address of the audio resource is then given as a relative address to the address of this class.

例如,假设您的项目中有"MySound"类.如果音频文件位于同一文件夹或包中,则相对寻址方案将如下所示:

For example, let's say you have the class "MySound" in your project. If the audio files are in the same folder or package, the relative addressing scheme would look like this:

URL beepURL = MySound.class.getResource("beep.wav");

如果音频文件位于MySound所在的子文件夹或子包中,例如"/audio",则命令如下:

If the audio file was in a subfolder or subpackage of the one where MySound is located, such as "/audio", the command would be as follows:

URL beepURL = MySound.class.getResource("audio/beep.wav");

用于指定相对地址的基本HTML规则应该起作用.其中包括用于寻址父文件夹或数据包的符号,例如"..".

The basic HTML rules for specifying relative addresses should work. That includes symbols such as ".." for addressing a parent folder or package.

例如,如果您有一个名为res/audio的软件包,该软件包与保存MySound的软件包相邻

For example, if you have a package named res/audio that is adjacent to the package holding MySound

URL beepURL = MySound.class.getResource("../res/audio/beep.wav");

以上示例的文件结构:

src/com.mydomainname.myproject/utils/MySound 
src/com.mydomainname.myproject/res/audio/beep.wav 

如果您以"/"开头的网址作为"/beep.wav",则有不同的规则适用.文档将其称为绝对"形式.我没有在自己的编码中使用它,所以我不会冒用错误的方式解释它的风险.

There are different rules that apply if you start the url address with "/" as is "/beep.wav". The documentation refers to this as an "absolute" form. I have not used it in my own coding, so I will not risk explaining it in a faulty way.

对于 JavaSound信息页面示例,我们可以按以下方式编辑这些行:

For the JavaSound Info Page example we might edit these lines as follows:

    URL url = LoopSound.class.getResource("leftright.wav");
    Clip clip = AudioSystem.getClip();
    AudioInputStream ais = AudioSystem.getAudioInputStream( url );
    clip.open(ais);

这假定leftright.wav与LoopSound位于同一程序包中.

This assumes that leftright.wav is located in the same package as LoopSound.

这篇关于为什么即使使用.wav文件,我的Java程序也不会添加声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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