
正文
javawav播放代码 java播放视频代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
编写一个java类,实现mid或者wav格式的背景音乐播放,要循环的,用作一个小游戏的背景音乐,谢
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
public class MusicAudioClip {
AudioClip clip = null;
public AudioClip getAudioClip() {
return this.clip;
}
public void setAudioClip(AudioClip clip) {
this.clip = clip;
}
public void play() {//播放
if (getAudioClip() != null) {
getAudioClip().play();
}
}
public void loop() {//循环
if (getAudioClip() != null) {
getAudioClip().loop();
}
}
public void stop() {//停止
if (getAudioClip() != null) {
getAudioClip().stop();
}
}
public static void main(String[] args) {
MusicAudioClip mac = new MusicAudioClip();
try {
mac.setAudioClip(Applet
.newAudioClip((new java.io.File("music\\0.wav")).toURL()));//填写你自己的文件路径
} catch (MalformedURLException e) {
e.printStackTrace();
}
mac.loop();//循环播放
}
}
相关问答
Q1: 求一个简单java播放wav音频程序
/**
* 播放音乐
*
* @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下载 需要自己下载
Q2: java如何播放wav文件
建议使用jmf(java media framwork),这样就能播放mp3等众多格式的音乐了;去sun官网下一个jmf,安装好后,把
jmf.jar包引入便可使用,给出例zi代码:使用方法:构造函数中传入文件路径名即可,播放、暂停、继续、停止等功能均已实现。
/*************************************************
* Subclass: MusicPlay
*************************************************/
public class MusicPlay implements Runnable {
private Time zeroTime = new Time(0);
private Player player;
private boolean isloop = false;
/*************************************************
* Function: MusicPlay Description: constructor, load the music file and
* get ready for play Called By: MultiMedia()
*************************************************/
// 实例化各个参数 filename 为文件名,可为绝对路径
public MusicPlay(String filename) {
File file = new File(filename);
try {
player = Manager.createRealizedPlayer(file.toURI().toURL());
player.addControllerListener(new ControllListener());
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*************************************************
* Function: isRunning Description: test if this music is playing Called
* By:
*************************************************/
public boolean isRunning() {
return player.getState() == Player.Started;
}
/*************************************************
* Function: play Description: play the music for once Called By:
* resumeAll()
*************************************************/
// 只播放一次
public void play() {
if (!turnOff)
player.start();
}
/*************************************************
* Function: replay Description: replay the music Called By: musics that
* will be played many times will invoke this methed
*************************************************/
// 再播放一次
public void replay() {
if (turnOff)
return;
if (player.getState() == Controller.Prefetched)
player.setMediaTime(zeroTime);
player.start();
}
/*************************************************
* Function: stop Description: stop this music Called By: stopAll() of
* upper class,suspendAll() of upper
* class,BackroundForMenuPanel,GameOverPanel
*************************************************/
public void stop() {
player.stop();
}
/*************************************************
* Function: close Description: dispose the music Called By: closeAll()
* of super class
*************************************************/
public void close() {
player.stop();
player.close();
}
/*************************************************
* Function: loop Description: make the music played repetitiously
* Called By: music that will repetitious play
*************************************************/
// 循环播放
public void loop() {
if (turnOff)
return;
isloop = true;
player.prefetch();
replay();
}
/*************************************************
* Function: run Description: trig this music Called By: Override method
*************************************************/
@Override
public void run() {
loop();
}
/*************************************************
* Subclass: ControllListener Description: listener for playing and
* implement playing repetitiously
*************************************************/
// 通过对播放进度的监听,实现循环播放
private class ControllListener implements ControllerListener {
public void controllerUpdate(ControllerEvent e) {
if (e instanceof EndOfMediaEvent) {
if (isloop) {
player.setMediaTime(new Time(0));
player.start();
}
}
}
}
}
关于javawav播放代码和java播放视频代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







