在 Java 中,处理音频文件可以使用 Java 音频库 (Java Sound API)。您可以使用 Java 音频库来读取和播放各种音频格式,包括 WAV 格式。以下是一个使用 Java 音频库读取 WAV 文件的示例代码:import javax.sound.sampled.AudioInputStream
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.Clip
import javax.sound.sampled.DataLine
import javax.sound.sampled.LineUnavailableException
import java.io.File
import java.io.IOException
public class AudioPlayer {
public static void main(String[] args) {
try {
// 打开音频文件
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("audio.wav"))
DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat())
Clip clip = (Clip) AudioSystem.getLine(info)
// 播放音频文件
clip.open(audioInputStream)
clip.start()
// 等待音频播放完成
while (!clip.isRunning())
Thread.sleep(10)
while (clip.isRunning())
Thread.sleep(10)
// 关闭音频文件
clip.close()
audioInputStream.close()
} catch (LineUnavailableException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} catch (Exception e) {
e.printStackTrace()
}
}
}
这个示例代码将读取名为 "audio.wav" 的 WAV 文件并将其播放。您可以根据自己的需要修改代码,例如将其嵌入到 Web 应用程序中以实现通过 Tomcat 来访问和播放音频文件。
Applet有一个属性,类型为AppletStub。你的Applet没有在页面中,AppletStub属性就会为null。
如果你想编写java的音频播放器,请使用jmf。
1、安装windows版jmf(当然,你的 *** 作系统是windows)
2、把%安装目录%\lib\jmf.jar添加到classpath
注意:不能只是下载一个jmf.jar文件,然后添加到classpath,这是不行的。你需要安装jmf。
import java.io.File
import java.io.IOException
import javax.media.CannotRealizeException
import javax.media.Manager
import javax.media.NoPlayerException
import javax.media.Player
public class MyPlayer {
private static String path = "杀破狼.mp3"
public static void main(String[] args) {
File f1 = new File(path)
try {
Player player = Manager.createRealizedPlayer(f1.toURI().toURL())
player.prefetch()
player.start()
} catch (CannotRealizeException ex) {
} catch (NoPlayerException ex) {
} catch (IOException ex) {
}
}
}
建议使用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()
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)