怎样在JAVA中插入声音

怎样在JAVA中插入声音,第1张

import java.applet.*

import java.net.URL

import java.io.*

class Voice

{

AudioClip beijing

File file999

URL url999,

File file100,

public Voice()

{

file999=new File("Sound\\背景Doom.wav")

try

{

url999=file999.toURL()

}

catch (Exception e)

{

}

beijing=Applet.newAudioClip(url999)

kaishi=Applet.newAudioClip(url000)

}

public void play(int m)

{

switch(m)

{

case 1:beijing.play()

break

}

}

public void stop(int m)

{

switch(m)

{

case 1:beijing.stop()

break}

}

}

用的时候调用函数就可以了

譬如,设置游戏背景音乐的时候,在主界面定义一个成员变量 voice v

然后 ,v.play(1)

就行了

我是这末实现的,一点浅薄的见解,不要见笑

//给你个例子

import java.io.*

import javax.sound.sampled.*

import javax.swing.*

import java.awt.event.*

public class Au extends JFrame

{

public static void main(String[] args)

{

Au w=new Au()

}

Au()

{

JButton b=new JButton("播放")

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

play()

}

})

add(b)

setResizable(false)

pack()

setLocationRelativeTo(null)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

setVisible(true)

}

void play()

{

//将f指定为声音文件的文件名

//声音文件只能是WAVE、AU、AIFF、AIFC、SND格式的。

final String f="s.wav"

Clip c=null

try

{

c=AudioSystem.getClip()

c.open(AudioSystem.getAudioInputStream(new File(f)))

c.loop(0)

}

catch(Exception ex)

{

}

}

}


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

原文地址: https://outofmemory.cn/bake/11258940.html

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

发表评论

登录后才能评论

评论列表(0条)

保存