wav音频的剪切

wav格式音频剪切功能的完美实现方案。




import java.io.*;
import javax.sound.sampled.*;

public class AudioFileProcessor {



    /**
     *
     * @param sourceFileName 源文件路径
     * @param destinationFileName 生成文件路径
     * @param start 切割开始时间(毫秒)
     * @param end 切割结束时间(毫秒)
     */
    public static void CutAudio(String sourceFileName, String destinationFileName, int start, int end) {
        AudioInputStream inputStream = null;
        AudioInputStream shortenedStream = null;
        try {
            File file = new File(sourceFileName);
            //获取指定的音频文件格式 File
            AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
            //获取包含在音频文件中的音频数据的格式
            AudioFormat format = fileFormat.getFormat();
            //从提供的输入流中获取音频输入流
            inputStream = AudioSystem.getAudioInputStream(file);
            float bytesPerSecond = format.getFrameSize() *  format.getFrameRate()/1000;
            //跳过并丢弃该音频输入流中指定数量的字节。
            inputStream.skip((long)(start * bytesPerSecond));
            long framesOfAudioToCopy =(long)( (end-start) *  format.getFrameRate()/1000);
            shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
            File destinationFile = new File(destinationFileName);
            AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    System.out.println(e);
                }
            }

            if (shortenedStream != null)
            {
                try {
                    shortenedStream.close();
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }

   
}

转载于:https://www.cnblogs.com/zofun/p/11443941.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值