Java播放声⾳的三种⽅法⼀、播放wav格式的⾳频⽂件
存放⾳频⽂件:
代码:
package com.xj.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* @Author : xjfu
* @Date : 2021/7/8 16:45
* @Description : ⾳乐播放Deom
*                      1.只⽀持wav的⾳频格式,
*                      2.只能⼀直播放,⽽不能暂停后继续播放
*/
public class MusicPlayDemo1 {
public static void main(String[] args){
//⾳频⽂件路径
String path = "src/main/resources/music/学猫叫.wav";
File file = new File(path);
//AudioInputStream:⾳频输⼊流,是具有指定⾳频格式和长度的输⼊流。
AudioInputStream audioInputStream = null;
//SourceDataLine:源数据⾏,是可以写⼊数据的数据⾏
SourceDataLine auline = null;
try {
//AudioSystem:该类充当取样⾳频系统资源的⼊⼝点
audioInputStream = AudioInputStream(file);
//AudioFormat: 是在声⾳流中指定特定数据安排的类
AudioFormat format = Format();
//该构造⽅法:根据指定信息构造数据⾏的信息对象,这些信息包括单个⾳频格式
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
auline = (SourceDataLine) Line(info);
//该⽅法:打开具有指定格式的⾏,这样可使⾏获得所有所需的系统资源并变得可操作。
auline.open(format);
auline.start();
int nBytesRead = 0 ;
//缓冲⼤⼩
byte[] abData = new byte[512];
while(nBytesRead != -1) {
nBytesRead = ad(abData , 0 , abData.length);
if(nBytesRead >= 0){
auline.write(abData, 0, nBytesRead);
}
}
} catch (Exception e) {
e.printStackTrace();
return;
}finally{
auline.drain();
auline.close();
}
}
}
说明:
1.只⽀持wav的⾳频格式
2.只能⼀直播放,⽽不能暂停后继续播放
⼆、⽀持wav和mp3格式的⾳频⽂件
package com.xj.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* @Author : xjfu
* @Date : 2021/7/8 16:45
* @Description : ⾳乐播放Deom
*                      1.⽀持wav格式⾳频播放
*                      2.⽀持mp3格式⾳频播放,但是需要添加mp3的SPI⽀持库
*                      3.只能⼀直播放,⽽不能暂停后继续播放
*                      3.只能⼀直播放,⽽不能暂停后继续播放
*/
public class MusicPlayDemo2 {
public static void main(String[] args) throws Exception{
//⾳频⽂件路径
String path = "src/main/resources/music/徐良&阿悄-.mp3";
File file = new File(path);
//⾳频播放流
AudioInputStream audioInputStream = AudioInputStream(file);
//⾳频格式
AudioFormat audioFormat = Format();
// 输出设备
SourceDataLine sourceDataLine = null;
//转换格式主要⽤于兼容Mp3格式的⾳乐⽂件
if (Encoding() != AudioFormat.Encoding.PCM_SIGNED){
/**
* 根据给定的参数构建⼀个AudioFormat(1,2,3,4,5,6,7):
* 1- the audio encoding technique ⾳频编码技术
* 2- the number of samples per second 每秒样本数
* 3- the number of bits in each sample 每个样本中的位数
* 4- the number of channels(1 for mono,2 for stereo,and so on) 通道数(1个⽤于单声道,2个⽤于⽴体声,依次类推)
* 5- the number of bytes in each frame 每帧中的字节数
* 6- the number of frames per second 每秒的帧数
* 7- indicates weather the data for a single sample is stored in big-endian byte order(false means little-endian) 指⽰单个样本的数据是否以big-endian字节顺            */
audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
//Obtains an audio input stream of the indicated format,by converting the provided audio input stream
audioInputStream = AudioInputStream(audioFormat, audioInputStream);
}
// 打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine) Line(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt = 0;
//缓存区
byte[] tempBuffer = new byte[320];
while((cnt = ad(tempBuffer,0, tempBuffer.length)) != -1){
if(cnt > 0){
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
//缓存数据输出为空时会block
sourceDataLine.drain();
sourceDataLine.close();
}
}
因为⽀持mp3格式,所以需要在maven中添加jar包依赖:
<!-- MusicPlayDemo2 为了⽀持mp3的播放,添加mp3的SPI⽀持库 -->
<dependency>
<groupId&lecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
说明:
1.⽀持wav格式⾳频播放
2.⽀持mp3格式⾳频播放,但是需要添加mp3的SPI⽀持库
3.只能⼀直播放,⽽不能暂停后继续播放
三、对“⼆”的改进⽅法,⽀持暂停播放功能
package com.xj.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* @Author : xjfu
* @Date : 2021/7/8 16:45
* @Description : ⾳乐播放Deom
*                      1.默认只⽀持wav格式⾳频播放
*                      2.若想⽀持mp3格式⾳频,需要添加mp3的SPI⽀持库
*                      3.可以⼀直播放,也⽀持暂停后继续播放
*/
public class MusicPlayDemo3 {
public static void main(String[] args) throws Exception{
//⾳频⽂件路径
String path = "src/main/resources/music/徐良&阿悄-.mp3";
File file = new File(path);
AudioInputStream AudioInputStream(file);//打开⾳频⽂件流
AudioFormat audioFormat = Format();//获取⾳频格式
Encoding() != AudioFormat.Encoding.PCM_SIGNED){
/**
* 根据给定的参数构建⼀个AudioFormat(1,2,3,4,5,6,7):
* 1- the audio encoding technique ⾳频编码技术
* 2- the number of samples per second 每秒样本数
* 3- the number of bits in each sample 每个样本中的位数
* 4- the number of channels(1 for mono,2 for stereo,and so on) 通道数(1个⽤于单声道,2个⽤于⽴体声,依次类推)
* 5- the number of bytes in each frame 每帧中的字节数
* 6- the number of frames per second 每秒的帧数
* 7- indicates weather the data for a single sample is stored in big-endian byte order(false means little-endian) 指⽰单个样本的数据是否以big-endian字节顺            */
audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
//Obtains an audio input stream of the indicated format,by converting the provided audio input stream
audioInputStream = AudioInputStream(audioFormat, audioInputStream);
}
DataLine.Info info=new DataLine.Info(Clip.class, audioFormat, AudioSystem.NOT_SPECIFIED);
//取得⾳频剪辑接⼝实现类
Clip clip=(Clip) Line(info);
//获取剪辑
clip.open(audioInputStream);
//关闭⾳频流
audioInputStream.close();
html播放音乐代码//播放⾳频
clip.start();
//中继点point
int point = 0;
while(true){
//休眠5秒
Thread.sleep(5000);
System.out.println("暂停5秒");
/
/获取当前播放中继点
point = FramePosition();
//停⽌播放
clip.stop();
//休眠5秒
Thread.sleep(5000);
System.out.println("继续从: " + point + " 播放!");
//设置播放点
clip.setFramePosition(point);
//继续播放
clip.start();
}
}
}
说明:
1.⽀持wav格式⾳频播放
2.⽀持mp3格式⾳频播放,需要添加mp3的SPI⽀持
3.可以⼀直播放,也⽀持暂停后继续播放
上⾯是全部所需代码,供⼤家参考。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。