最新消息:关注【已取消】微信公众号,可以获取全套资料,【全套Java基础27天】【JavaEE就业视频4个月】【Android就业视频4个月】

Java将PCM音频格式转化为MP3或WAVE格式的方法示例

Android 太平洋学习网 浏览 评论

我们使用AudioRecord录制的声音文件为PCM格式的,如何使用JAVA将PCM格式转化为MP3或者WAVE格式呢?非常简单,来看看下面的示例吧!

package com.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TestDemo {

	static Map<String,String> map = new HashMap<String,String>();
	
	public static void main(String[] args) throws IOException {
	        //如果想要把pcm转wave,直接将后缀改成wave即可
		convertAudioFiles("D:\\excelFile\\1606288228441.pcm", "D:\\excelFile\\tnafcs.mp3");
	}
	
	//PCM转MP3工具类
	public static String convertAudioFiles(String src, String target) throws IOException {
		try {
	        FileInputStream fis = new FileInputStream(src);
	        FileOutputStream fos = new FileOutputStream(target);
	 
	                 //Calculate the length
	        byte[] buf = new byte[1024 * 4];
	        int size = fis.read(buf);
	        int PCMSize = 0;
	        while (size != -1) {
	            PCMSize += size;
	            size = fis.read(buf);
	        }
	        fis.close();
	        
	        //Fill in the parameters, bit rate, etc. Here is 16-bit mono 8000 Hz
			//一般只需要修改channels和samplesPerSec的值即可,取决于你录制的是8000HZ还是16000HZ
	        WaveHeader header = new WaveHeader();
	        //Length field = content size (PCMSize) + header field size (not including the first 4-byte identifier RIFF and the 4-byte fileLength itself)
	        header.fileLength = PCMSize + (44 - 8);
	        header.FmtHdrLeth = 16;
	        header.BitsPerSample = 16;
	        header.Channels = 1;
	        header.FormatTag = 0x0001;
	        header.SamplesPerSec = 16000;
	        header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8);
	        header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
	        header.DataHdrLeth = PCMSize;
	 
	        byte[] h = header.getHeader();
	 
	                 assert h.length == 44; //WAV standard, the header should be 44 bytes
	        //write header
	        fos.write(h, 0, h.length);
	        //write data stream
	        fis = new FileInputStream(src);
	        size = fis.read(buf);
	        while (size != -1) {
	            fos.write(buf, 0, size);
	            size = fis.read(buf);
	        }
	        fis.close();
	        fos.close();
		}catch(Exception ex) {
			ex.printStackTrace();
		}
        System.out.println("PCM Convert to MP3 OK!");
        return "ok";
    }
}

WaveHeader类的代码如下:

package com.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
 
/**
 * WaveHeader
 *
 * @author QC
 * @since 2020-04-03
 */
public class WaveHeader {
    public final char fileID[] = {'R', 'I', 'F', 'F'};
    public int fileLength;
    public char wavTag[] = {'W', 'A', 'V', 'E'};
    public char FmtHdrID[] = {'f', 'm', 't', ' '};
    public int FmtHdrLeth;
    public short FormatTag;
    public short Channels;
    public int SamplesPerSec;
    public int AvgBytesPerSec;
    public short BlockAlign;
    public short BitsPerSample;
    public char DataHdrID[] = {'d', 'a', 't', 'a'};
    public int DataHdrLeth;
 
    public byte[] getHeader() throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        WriteChar(bos, fileID);
        WriteInt(bos, fileLength);
        WriteChar(bos, wavTag);
        WriteChar(bos, FmtHdrID);
        WriteInt(bos, FmtHdrLeth);
        WriteShort(bos, FormatTag);
        WriteShort(bos, Channels);
        WriteInt(bos, SamplesPerSec);
        WriteInt(bos, AvgBytesPerSec);
        WriteShort(bos, BlockAlign);
        WriteShort(bos, BitsPerSample);
        WriteChar(bos, DataHdrID);
        WriteInt(bos, DataHdrLeth);
        bos.flush();
        byte[] r = bos.toByteArray();
        bos.close();
        return r;
    }
 
    private void WriteShort(ByteArrayOutputStream bos, int s) throws IOException {
        byte[] mybyte = new byte[2];
        mybyte[1] = (byte) ((s << 16) >> 24);
        mybyte[0] = (byte) ((s << 24) >> 24);
        bos.write(mybyte);
    }
 
    private void WriteInt(ByteArrayOutputStream bos, int n) throws IOException {
        byte[] buf = new byte[4];
        buf[3] = (byte) (n >> 24);
        buf[2] = (byte) ((n << 8) >> 24);
        buf[1] = (byte) ((n << 16) >> 24);
        buf[0] = (byte) ((n << 24) >> 24);
        bos.write(buf);
    }
 
    private void WriteChar(ByteArrayOutputStream bos, char[] id) {
		for (char c : id) {
			bos.write(c);
		}
    }
 
}

Java将PCM转为MP3或WAVE格式很简单,复制上方代码即可使用,不需要使用所谓的lame之类的库来操作。

来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/android/1028.html
"文章很值,打赏犒劳作者一下"
微信号: Javaweb_engineer

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

与本文相关的文章

发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论