admin 发布:2024-05-27 21:38 183
今天给各位分享pcm转mp3java源代码的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
在下不才,下面的仅供参考,我的也有问题,希望对你能有一点帮助,你有好的方法后希望能发出来看看
import java.awt.Graphics;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class Musicline extends javax.swing.JFrame implements Runnable{
private byte[] audioData=null;
private int intBytes = 0;
private byte[] ml=new byte[1];
private int[] drawl=null;
public Musicline() {
initComponents();
Graphics g;
g=this.getGraphics();
}
public void paint(Graphics g)
{
g.clearRect(0,0,900,900);
if(audioData!=null)
{
drawl=new int[audioData.length];
for(int i=0;iaudioData.length;i++)
{
ml[0]=audioData[i];
drawl[i]=Math.abs((int)ml[0]);
}
System.out.println(drawl[0]);
for(int i=0;idrawl.length-1;i++)
{
g.drawLine(i*this.getWidth()/256,drawl[i]+100,(i+1)*this.getWidth()/256,drawl[i+1]+100);
}
}
}
public void run()
{
while(intBytes!=-1)
{
try {
synchronized(this){
this.wait(10);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
}
}
public void play()
{
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(
"F:/20053383924757.wav"));// 获得音频输入流
ais=AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,ais);
AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排
System.out.println("baseFormat="+baseFormat);
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
baseFormat);
System.out.println("info="+info);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// 从混频器获得源数据行
System.out.println("line="+line);
line.open(baseFormat);// 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。
line.start();// 允许数据行执行数据 I/O
int BUFFER_SIZE = 256;
audioData = new byte[BUFFER_SIZE];
while (intBytes != -1) {
intBytes = ais.read(audioData, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。
if (intBytes = 0) {
int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据行将音频数据写入混频器。
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// editor-fold defaultstate="collapsed" desc=" 生成的代码 "
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// /editor-fold
public static void main(String args[]) {
Musicline msl=new Musicline();
msl.setVisible(true);
Thread yh=new Thread(msl);
yh.start();
msl.play();
}
}
上一篇我们已经学习了PCM音频的保存格式,这一篇我们通过掌握的知识,完成PCM音频的单声道和双声道的互相转换。
首先我们把上一篇的最核心部分贴出来:
我们首先完成单声道转双声道的操作。
单声道转双声道的基本原理:
首先我录制了一个音频保存到ArrayList中:
录制的是16位的数据,所以我们每一个采样的数据会占据两位,所以在拷贝的过程中,我们也要每两位拷贝一次:
单声道转声道的操作就完成了。
双声道转单声道的原理:
我们可以按照单声道双声道的做法,每四位取前两位或后两位的数据即可。但是这里我们换一种做法。
这里我们使用了ByteBuffer帮助我们把Byte转成Short。其中有一个很重要的坑,就是设置Byte转Short的规则:
所以我们一定要确保高低位的顺序,否则得到的Short一定是错的,经过测试,录制的音频是低位在前,所以我们修改ByteBuffer默认的高位在前的配置:
相同的原理,我们需要Byte转Int都可以借助对应的Buffer进行读取,非常的方便。
基本流程和第一种方法一样,如果是你用的Java,你还可以通过位运算进行Short和Byte的转换,但是kotlin的对应的运算符却无法正确转换,具体原因还不清楚,这也是为什么我使用了Buffer进行转换的原因。
只要我们掌握了PCM的保存格式,单声道和双声道的互相转换还是非常轻松的,下一篇我们来了解一下新的音频格式:WAV。
首先下载播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加这个包。
播放器演示代码如下
package com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame {
/**
*
*/
private static final long serialVersionUID = -2605658046194599045L;
boolean isStop = true;// 控制播放线程
boolean hasStop = true;// 播放线程状态
String filepath;// 播放文件目录
String filename;// 播放文件名称
AudioInputStream audioInputStream;// 文件流
AudioFormat audioFormat;// 文件格式
SourceDataLine sourceDataLine;// 输出设备
List list;// 文件列表
Label labelfilepath;//播放目录显示标签
Label labelfilename;//播放文件显示标签
public MusicPlayer() {
// 设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3 Music Player");
setSize(350, 370);
// 建立菜单栏
MenuBar menubar = new MenuBar();
Menu menufile = new Menu("File");
MenuItem menuopen = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
// 文件列表
list = new List(10);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 双击时处理
if (e.getClickCount() == 2) {
// 播放选中的文件
filename = list.getSelectedItem();
play();
}
}
});
add(list, "Center");
// 信息显示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label("Dir:");
labelfilename = new Label("File:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, "North");
// 注册窗体关闭事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
// 打开
private void open() {
FileDialog dialog = new FileDialog(this, "Open", 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText("Dir:" + filepath);
// 显示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {
list.add(filename);
}
}
}
}
// 播放
private void play() {
try {
isStop = true;// 停止播放线程
// 等待播放线程停止
System.out.print("Start:" + filename);
while (!hasStop) {
System.out.print(".");
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.out.println("");
File file = new File(filepath + filename);
labelfilename.setText("Playing:" + filename);
// 取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
audioFormat = audioInputStream.getFormat();
// 转换mp3文件编码
if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(), 16, audioFormat
.getChannels(), audioFormat.getChannels() * 2,
audioFormat.getSampleRate(), false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}
// 打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 创建独立线程进行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
public void run() {
try {
int cnt;
hasStop = false;
// 读取数据到缓存数据
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (isStop)
break;
if (cnt 0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
/*
* 实现录音机的功能
*/
package com.liuyun.MyRecord1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import java.lang.*;
public class MyRecord extends JFrame implements ActionListener{
//定义录音格式
AudioFormat af = null;
//定义目标数据行,可以从中读取音频数据,该 TargetDataLine 接口提供从目标数据行的缓冲区读取所捕获数据的方法。
TargetDataLine td = null;
//定义源数据行,源数据行是可以写入数据的数据行。它充当其混频器的源。应用程序将音频字节写入源数据行,这样可处理字节缓冲并将它们传递给混频器。
SourceDataLine sd = null;
//定义字节数组输入输出流
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
//定义音频输入流
AudioInputStream ais = null;
//定义停止录音的标志,来控制录音线程的运行
Boolean stopflag = false;
//记录开始录音的时间
long startPlay;
//定义所需要的组件
JPanel jp1,jp2,jp3;
JLabel jl1=null;
JButton captureBtn,stopBtn,playBtn,saveBtn;
public static void main(String[] args) {
//创造一个实例
MyRecord mr = new MyRecord();
}
//构造函数
public MyRecord()
{
//组件初始化
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//定义字体
Font myFont = new Font("华文新魏",Font.BOLD,30);
jl1 = new JLabel("录音机功能的实现");
jl1.setFont(myFont);
jp1.add(jl1);
captureBtn = new JButton("开始录音");
//对开始录音按钮进行注册监听
captureBtn.addActionListener(this);
captureBtn.setActionCommand("captureBtn");
//对停止录音进行注册监听
stopBtn = new JButton("停止录音");
stopBtn.addActionListener(this);
stopBtn.setActionCommand("stopBtn");
//对播放录音进行注册监听
playBtn = new JButton("播放录音");
playBtn.addActionListener(this);
playBtn.setActionCommand("playBtn");
//对保存录音进行注册监听
saveBtn = new JButton("保存录音");
saveBtn.addActionListener(this);
saveBtn.setActionCommand("saveBtn");
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
jp3.setLayout(null);
jp3.setLayout(new GridLayout(1, 4,10,10));
jp3.add(captureBtn);
jp3.add(stopBtn);
jp3.add(playBtn);
jp3.add(saveBtn);
//设置按钮的属性
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//设置窗口的属性
this.setSize(400,300);
this.setTitle("录音机");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("captureBtn"))
{
//点击开始录音按钮后的动作
//停止按钮可以启动
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//调用录音的方法
capture();
//记录开始录音的时间
startPlay = System.currentTimeMillis();
}else if (e.getActionCommand().equals("stopBtn")) {
//点击停止录音按钮的动作
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
//调用停止录音的方法
stop();
//记录停止录音的时间
long stopPlay = System.currentTimeMillis();
//输出录音的时间
System.out.println("Play continues " + (stopPlay-startPlay));
}else if(e.getActionCommand().equals("playBtn"))
{
//调用播放录音的方法
play();
}else if(e.getActionCommand().equals("saveBtn"))
{
//调用保存录音的方法
save();
}
}
//开始录音
public void capture()
{
try {
//af为AudioFormat也就是音频格式
af = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class,af);
td = (TargetDataLine)(AudioSystem.getLine(info));
//打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。
td.open(af);
//允许某一数据行执行数据 I/O
td.start();
//创建播放录音的线程
Record record = new Record();
Thread t1 = new Thread(record);
t1.start();
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
}
//停止录音
public void stop()
{
stopflag = true;
}
//播放录音
public void play()
{
//将baos中的数据转换为字节数据
byte audioData[] = baos.toByteArray();
//转换为输入流
bais = new ByteArrayInputStream(audioData);
af = getAudioFormat();
ais = new AudioInputStream(bais, af, audioData.length/af.getFrameSize());
try {
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, af);
sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sd.open(af);
sd.start();
//创建播放进程
Play py = new Play();
Thread t2 = new Thread(py);
t2.start();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//关闭流
if(ais != null)
{
ais.close();
}
if(bais != null)
{
bais.close();
}
if(baos != null)
{
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//保存录音
public void save()
{
//取得录音输入流
af = getAudioFormat();
byte audioData[] = baos.toByteArray();
bais = new ByteArrayInputStream(audioData);
ais = new AudioInputStream(bais,af, audioData.length / af.getFrameSize());
//定义最终保存的文件名
File file = null;
//写入文件
try {
//以当前的时间命名录音的名字
//将录音的文件存放到F盘下语音文件夹下
File filePath = new File("F:/语音文件");
if(!filePath.exists())
{//如果文件不存在,则创建该目录
filePath.mkdir();
}
long time = System.currentTimeMillis();
file = new File(filePath+"/"+time+".wav");
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, file);
//将录音产生的wav文件转换为容量较小的mp3格式
//定义产生后文件名
String tarFileName = time+".mp3";
Runtime run = null;
try {
run = Runtime.getRuntime();
long start=System.currentTimeMillis();
//调用解码器来将wav文件转换为mp3文件
strongspan style="color:#ff0000;"Process p=run.exec(filePath+"/"+"lame -b 16 "+filePath+"/"+file.getName()+" "+filePath+"/"+tarFileName); //16为码率,可自行修改
/span/strong//释放进程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
long end=System.currentTimeMillis();
System.out.println("convert need costs:"+(end-start)+"ms");
//删除无用的wav文件
if(file.exists())
{
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//最后都要执行的语句
//run调用lame解码器最后释放内存
run.freeMemory();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭流
try {
if(bais != null)
{
bais.close();
}
if(ais != null)
{
ais.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//设置AudioFormat的参数
public AudioFormat getAudioFormat()
{
//下面注释部分是另外一种音频格式,两者都可以
AudioFormat.Encoding encoding = AudioFormat.Encoding.
PCM_SIGNED ;
float rate = 8000f;
int sampleSize = 16;
String signedString = "signed";
boolean bigEndian = true;
int channels = 1;
return new AudioFormat(encoding, rate, sampleSize, channels,
(sampleSize / 8) * channels, rate, bigEndian);
// //采样率是每秒播放和录制的样本数
// float sampleRate = 16000.0F;
// // 采样率8000,11025,16000,22050,44100
// //sampleSizeInBits表示每个具有此格式的声音样本中的位数
// int sampleSizeInBits = 16;
// // 8,16
// int channels = 1;
// // 单声道为1,立体声为2
// boolean signed = true;
// // true,false
// boolean bigEndian = true;
// // true,false
// return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,bigEndian);
}
//录音类,因为要用到MyRecord类中的变量,所以将其做成内部类
class Record implements Runnable
{
//定义存放录音的字节数组,作为缓冲区
byte bts[] = new byte[10000];
//将字节数组包装到流里,最终存入到baos中
//重写run函数
public void run() {
baos = new ByteArrayOutputStream();
try {
stopflag = false;
while(stopflag != true)
{
//当停止录音没按下时,该线程一直执行
//从数据行的输入缓冲区读取音频数据。
//要读取bts.length长度的字节,cnt 是实际读取的字节数
int cnt = td.read(bts, 0, bts.length);
if(cnt 0)
{
baos.write(bts, 0, cnt);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//关闭打开的字节数组流
if(baos != null)
{
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
td.drain();
td.close();
}
}
}
}
//播放类,同样也做成内部类
class Play implements Runnable
{
//播放baos中的数据即可
public void run() {
byte bts[] = new byte[10000];
try {
int cnt;
//读取数据到缓存数据
while ((cnt = ais.read(bts, 0, bts.length)) != -1)
{
if (cnt 0)
{
//写入缓存数据
//将音频数据写入到混频器
sd.write(bts, 0, cnt);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
sd.drain();
sd.close();
}
}
}
}
这个程序只要写对了音乐文件的URL地址,例如:new URL("file:/C:/tmp/1/Windows Ding.wav");
就可以播放音乐,除了可以播放.wav格式的音乐,还可以播放.au格式的音乐。
另外,如果你不希望音乐循环播放,你可以去掉audio1.loop();这一语句。
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
public class D extends JFrame{
D(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
URL codebase=null;
try {
codebase = new URL("file:/C:/tmp/1/Windows Ding.wav");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AudioClip audio1=Applet.newAudioClip(codebase);
audio1.loop();
}
public static void main(String[] args) {
new D();
}
}
pcm转mp3java源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、pcm转mp3java源代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;