pek*_*pek 128
我写了下面的代码,工作正常.但我认为它只适用于.wav
格式.
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
Gre*_*man 19
一个坏例子:
import sun.audio.*; //import the sun.audio package
import java.io.*;
//** add this into your application code as appropriate
// Open an input stream to the audio file.
InputStream in = new FileInputStream(Filename);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
AudioPlayer.player.stop(as);
Run Code Online (Sandbox Code Playgroud)
我不想拥有这么多代码只是为了播放一个简单的该死的声音.如果你有JavaFX包(已经包含在我的jdk 8中),这可以工作.
private static void playSound(String sound){
// cl is the ClassLoader for the current class, ie. CurrentClass.class.getClassLoader();
URL file = cl.getResource(sound);
final Media media = new Media(file.toString());
final MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
}
Run Code Online (Sandbox Code Playgroud)
注意:您需要初始化JavaFX.一个快速的方法是在你的应用程序中调用一次JFXPanel()的构造函数:
static{
JFXPanel fxPanel = new JFXPanel();
}
Run Code Online (Sandbox Code Playgroud)
小智 7
要在java中播放声音,可以参考以下代码.
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class SoundClipTest extends JFrame {
public SoundClipTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Sound Clip");
this.setSize(300, 200);
this.setVisible(true);
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource("gameover.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SoundClipTest();
}
}
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,当我调用this.getClass()。getResourceAsStream()时,wchargin的最高答案是给我一个空指针错误。
对我有用的是:
void playSound(String soundFile) {
File f = new File("./" + soundFile);
audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
}
Run Code Online (Sandbox Code Playgroud)
我会用以下声音播放声音:
playSound("sounds/effects/sheep1.wav");
Run Code Online (Sandbox Code Playgroud)
sounds / effects / sheep1.wav位于Eclipse中我项目的基本目录中(因此不在src文件夹中)。
归档时间: |
|
查看次数: |
308387 次 |
最近记录: |