java音乐必须是wav,为什么即使使用.wav文件,我的Java程序也不会添加声音?

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!

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值