Java工具类之音频播放与mp3转pcm-yellowcong

今天玩百度的api,我不知道为啥,就是识别不了俺的mp3,俺就怒了,啥JB破玩意,连个mp3都识别不了,还搞毛线,后来发现mp3的音频包含文件头描述啥的,而pcm的音频格式就纯音频了,没有文件头信息,百度的语音识别不支持mp3的,所以,我需要一个工具类,将mp3格式的音频转化为pcm的,这样我就可以玩语音识别了。

依赖jar包

<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>mp3spi</artifactId>
    <version>1.9.5.4</version>
</dependency>

工具代码

package com.yellowcong.baidu.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.sound.sampled.AudioFileFormat;
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;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

/**
 * 创建日期:2018年1月14日
 * 创建时间:下午10:09:39
 * 创建者    :yellowcong
 * 机能概要:MP3转PCM Java方式实现
 * http://ai.baidu.com/forum/topic/show/496972
 */
public class AudioUtils {
    private static AudioUtils audioUtils = null;
    private AudioUtils(){}

    //双判断,解决单利问题
    public static AudioUtils getInstance(){
        if(audioUtils == null){
            synchronized (AudioUtils.class) {
                if(audioUtils == null){
                    audioUtils = new AudioUtils();
                }
            }
        }
        return audioUtils;
    }

    /**
     * MP3转换PCM文件方法
     * 
     * @param mp3filepath 原始文件路径
     * @param pcmfilepath 转换文件的保存路径
     * @return 
     * @throws Exception
     */
    public boolean convertMP32Pcm(String mp3filepath, String pcmfilepath){
        try {
            //获取文件的音频流,pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //将音频转化为  pcm的格式保存下来
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 播放MP3方法
     * 
     * @param mp3filepath
     * @throws Exception
     */
    public void playMP3(String mp3filepath) throws Exception {
        //获取音频为pcm的格式
        AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);

        // 播放
        if (audioInputStream == null){
            System.out.println("null audiostream");
            return;
        }
        //获取音频的格式
        AudioFormat targetFormat = audioInputStream.getFormat();
        DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, targetFormat, AudioSystem.NOT_SPECIFIED);
        //输出设备
        SourceDataLine line = null;
        try {
            line = (SourceDataLine) AudioSystem.getLine(dinfo);
            line.open(targetFormat);
            line.start();

            int len = -1;
//            byte[] buffer = new byte[8192];
            byte[] buffer = new byte[1024];
            //读取音频文件
            while ((len = audioInputStream.read(buffer)) > 0) {
                //输出音频文件
                line.write(buffer, 0, len);
            }

            // Block等待临时数据被输出为空
            line.drain();

            //关闭读取流
            audioInputStream.close();

            //停止播放
            line.stop();
            line.close();

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("audio problem " + ex);

        }
    }

    /**
     * 创建日期:2018年1月14日<br/>
     * 创建时间:下午9:53:14<br/>
     * 创建用户:yellowcong<br/>
     * 机能概要:获取文件的音频流
     * @param mp3filepath
     * @return
     */
    private AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            AudioInputStream in = null;

            //读取音频文件的类
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();

            //设定输出格式为pcm格式的音频文件
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);

            //输出到音频
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }
}

测试代码

package yellowcong.yuyin;

import org.junit.Test;

import com.yellowcong.baidu.utils.AudioUtils;

/**
 * 创建日期:2018年1月14日
 * 创建时间:下午10:09:39
 * 创建者    :yellowcong
 * 机能概要:MP3转PCM Java方式实现
 */
public class TestAudioUtils {
    //测试播放音频
    @Test
    public void testPaly() throws Exception{
        AudioUtils utils  = AudioUtils.getInstance();
        utils.playMP3("D:/xx.mp3");
    }

    @Test
    public void testConvert() throws Exception{
        AudioUtils utils  = AudioUtils.getInstance();
        utils.convertMP32Pcm("D:/xx.mp3", "D:/xx.pcm");
    }
}

测试的播放音频,我就不能截图了,截图截不出来声音啊,但是转化的图片如下,大家可以瞅一眼。
这里写图片描述

参考文章

http://ai.baidu.com/forum/topic/show/496972

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
要在CentOS上进行流量统计,你可以按照以下步骤进行操作: 1. 首先,你需要修改nginx配置文件来启用流量统计模块。编辑nginx配置文件(一般是`/usr/local/nginx/conf/nginx.conf`),在server配置块中添加以下配置: ``` location ~/status { stub_status on; # 开启流量统计 access_log off; # 关闭日志记录 } ``` 这将启用nginx的流量统计功能。 2. 你可以使用tcpdump抓取流量包,并进行过滤和录制。使用以下命令抓取具有源IP的请求,并将抓取的流量保存到文件中: ``` tcpdump -i eth1 -s 0 port 33357 host 172.16.xx.xx -w output-with-compose-100.cap ``` 其中,`eth1`是要监听的网络接口,`port 33357`是要抓取的端口,`host 172.16.xx.xx`是要过滤的源IP地址。抓取的流量将保存在`output-with-compose-100.cap`文件中。 3. 你可以使用Wireshark来导入抓取的流量文件,并计算整体请求的平均流量。打开Wireshark,选择“File”->“Open”来导入抓取的流量文件。然后,使用Wireshark的统计功能来计算流量的平均值。 通过以上步骤,你可以在CentOS上进行流量统计并计算请求的平均流量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [CentOS 7.6 Nginx的源码安装、流量统计和虚拟主机配置](https://blog.csdn.net/shengjie87/article/details/107871726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Centos之统计一个请求的流量(上传/下载流量)-yellowcong](https://blog.csdn.net/yelllowcong/article/details/114665101)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值