Why won't my java program add sound even with .wav file?

China☆狼群 提交于 2019-12-13 20:34:52

问题


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);
    }
}

回答1:


Obsolete tutorials seem to be a growing problem!

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.

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.

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

AudioInputStream ais = AudioSystem.getAudioInputStream(url);

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.

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");

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");

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

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");

File structure for above example:

src/com.mydomainname.myproject/utils/MySound 
src/com.mydomainname.myproject/res/audio/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.

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);

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



来源:https://stackoverflow.com/questions/50710703/why-wont-my-java-program-add-sound-even-with-wav-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!