问题描述
package soundTest;
import java.applet.*;
import java.net.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
public class SoundTest {
public static void main(String[] args) throws Exception {
try {
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
clip1.play();
} catch (MalformedURLException murle) {
System.out.println(murle);
}
URL url = new URL(
"http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
URL url2 = new URL(
"http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.
getAudioInputStream(url2);
clip2.open(ais2);
clip.loop(1);
clip2.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
我无法弄清楚如何从我的电脑(而不是从URL)在Java中发挥wav文件。我敢肯定,我把它放置在右侧区域中,SRC(我也把它放在几乎所有其他的空间,以防万一......)。
I can't figure out how to play a wav file from my computer (not from a URL) in java. I'm sure that I have it placed in the right area, the SRC (I also placed it in practically every other space just in case...).
第一次尝试从
它使我的catch语句。
The first attempt is from http://www.cs.cmu.edu/~illah/CLASSDOCS/javasound.pdfIt gives the me the catch statement.
第二次尝试把我的记录.wav文件的主人MediaFire。但是,没有工作。 异常线程mainjavax.sound.sampled.UnsupportedAudioFileException:无法从输入网址音频输入流
The second attempt was putting my recorded .wav file on mediafire. However, that didn't work. "Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL"
第三个例子工作正常,但不像我的文件,它是从网上的文件。当你点击一个,它带给你的只是一个音频播放器,而主人MediaFire链接带给你与其他的东西和一些应用可以播放该文件的网页。
The third example works fine, but, unlike my file, it's a file from online. When you click on that one, it brings you to just an audio player, while the mediafire link brings you to a page with other stuff and some application that plays the file.
推荐答案
第一次尝试
AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath()));
这是不是你如何构建一个网址
到文件
。相反,你应该使用文件#getURI#的getURL
This is not how you construct a URL
to a File
. Instead, you should use File#getURI#getURL
AudioClip clip1 = Applet.newAudioClip(new File("/full/path/to/audio.wav").toURI().toURL());
第二次尝试
主人MediaFire返回一个HTML响应,而不是音频文件......你可以......测试
mediafire is returning a html response, not the audio file...You can test it with...
URL url = new URL("http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");
try (InputStream is = url.openStream()) {
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} catch (IOException exp) {
exp.printStackTrace();
}
第三次尝试
您打开
片段,但从来没有启动
吧...
You open
the clip, but never start
it...
URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
clip2.open(ais2);
clip2.start();
这篇关于从电脑文件播放在java中的.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!