发布网友
共2个回答
热心网友
/**
* 播放音乐
*
* @param mp3Path
* @param repeat
*/
public static void playSound(final String mp3Path, final int repeat) {
new Thread(new Runnable() {
@Override
public void run() {
if (mp3Path.endsWith("mp3")) {
// TODO Auto-generated method stub
MP3Player mp3 = new MP3Player(mp3Path);
int ccc = 0;
while (ccc < repeat) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ccc++;
mp3.play();
}
} else {
try {
// .wav 文件放在java project 下面
//System.getProperty("user.dir") + File.separator+ "ring.wav"
System.out.println(mp3Path);
FileInputStream fileau = new FileInputStream(
mp3Path);
AudioStream as = new AudioStream(fileau);
int ccc = 0;
while (ccc < repeat) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ccc++;
AudioPlayer.player.start(as);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
sun audio jar下载 需要自己下载
热心网友
import java.applet.*;
import java.io.*;
import java.net.*;
public class AudioPlay {
URL radio = null;
AudioClip clip = null;
File file = null;
public static void main(String[] args) throws MalformedURLException {
new AudioPlay().play("C:\\a.wav");
}
public void play(String path) throws MalformedURLException {
file = new File(path);
radio = file.toURL();
clip = Applet.newAudioClip(radio);
clip.play();
}}追问无法播放声音 也没有异常