如何在Java中JFrame中添加音乐?

如何在Java中JFrame中添加音乐?,第1张

java中添加背景音乐举例如下:

File musicFile=new File("所要添加的音乐名")

URI uri=musicFile.toURI()

URL url=uri.toURL():

AudioClip=Applet.newAudioClip(url)

控制音乐的播放与停止

clip.play()

clip.stop()

import sun.audio.AudioPlayer

import sun.audio.AudioStream

import sun.audio.ContinuousAudioDataStream

public class Test extends JFrame {

public Test() {

Container c = this.getContentPane()

c.add(new JLabel("背景音乐"))

ContinuousAudioDataStream sound

try {

sound = new ContinuousAudioDataStream(new AudioStream(new FileInputStream(new File("H:\\AddLife.wav"))).getData())

AudioPlayer.player.start(sound)

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

this.setSize(200, 200)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

this.setVisible(true)

}

public static void main(String[] args) {

new Test()

}

}

注意,这种方式值支持读取mid或mave(mav)格式的音乐文件,如果你想要播放mp3,去网上下载个现成的java播放器例子看看吧!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/11201890.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存