AudioCLip主要的方法有:play()播放依次声音;loop()循环播放音乐;stop()停止播放。
做法一:
InputStream is =null
AudioStream as = null
is = getClass().getResourceAsStream("a.wav")
try {
as = new AudioStream(is)
} catch (IOException e) {}
AudioPlayer.player.start(as)
此方法将音乐文件放入流中在播放,仅限于Java Application,容易报错,空指针异常,或者是流异常,不推荐。
做法二:
String music = "a.wav"
AudioClip clip = Applet.newAudioClip(getClass().getResource(music))
次方法在Applet中运行没有问题,但是Application中getclass()会返回空指针,导致失败。
推荐做法:
private URL url
private AudioClip ac
File f1 = new File("C:/3.wav")
try {
url= f1.toURL()
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
ac= Applet.newAudioClip(cb1)
ac.play()
public class Sound {public static void main(String args[]) {
try {
FileInputStream fileau = new FileInputStream(new File("c:/a.wav"))
AudioStream as = new AudioStream(fileau)
AudioPlayer.player.start(as)
} catch (Exception e) {
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)