我在这里遇到了一个大问题,我尝试了一百万种方法,但没有任何帮助,这里是我尝试做到这一点的链接...
https://www.youtube.com/watch?v=nUKya2DvYSo
如何在Java中播放声音?
http://forum.codecall.net/topic/58228-playing-simple-sampled-audio-in-java/
http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
只有一些链接仍在浏览器中打开,但没关系..我正在 NetBeans 中制作此内容,我真的不知道该怎么做,请帮助我。 这是我想做的课程,还有按钮。如果你能为我做,我会很高兴
public class Machines extends javax.swing.JFrame {
public Machines() {
initComponents();
}
private void spinBtnActionPerformed(java.awt.event.ActionEvent evt) {
}
音乐文件也是
spinsound.wav
,我已经放在我的项目所在的目录中,这是链接C:\Users\MONSTER\Desktop\STEFAN\NetBeansProjects\PasswordSaver\spinsound.wav
你能试试这个吗:
ClassLoader CLDR = this.getClass().getClassLoader();
InputStream soundName = CLDR.getResourceAsStream("yourDirectory/yourSound.wav");
AudioStream audioStream = new AudioStream(soundName);
AudioPlayer.player.start(audioStream);
在检查谷歌后取自这里。
由于您只想播放声音,您可以使用我下面提供的示例: 看看
file:C:/image/song.wav
,在目录声明之前有一个 file:
关键字..
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WavPlayer extends JFrame {
JButton btn = new JButton("Play Sound");
File wavFile;
URL defaultSound;
public static Clip clip;
public static AudioInputStream audioInputStream;
public WavPlayer(String url) {
try {
setSize(300, 100);
setLocation(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
defaultSound = new URL (url);
jp.add(btn);
getContentPane().add(jp);
pack();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
} catch (MalformedURLException ex) {
Logger.getLogger(WavPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void play() {
try {
audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
try {
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(20000);
clip.start();
} catch (LineUnavailableException e) {
}
} catch (UnsupportedAudioFileException | IOException e) {
}
}
public void stop() {
clip.stop();
}
public static void main(String args[]) {
WavPlayer t = new WavPlayer("file:C:/image/song.wav");
t.setVisible(true);
}
}
寻找完全复制的代码对你的帮助不如研究和了解编码及其过程。
这是我的代码:
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Screen extends JPanel implements ActionListener {
private JButton button;
public Screen() {
setLayout(null);
button = new JButton("Play Sound");
button.setBounds(250, 50, 150, 50);
add(button);
button.addActionListener(this);
}
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
try {
String soundName = "sound.wav";
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}