zoukankan      html  css  js  c++  java
  • 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);
                    }
                }
            }
        }
    
       
    }
    
    
  • 相关阅读:
    HTTP request smuggling
    Do you really know CSS linear-gradients?
    Populating the page: how browsers work
    船舶智能管理系统API文档
    DocGuarder
    BUC LNB 器件
    BUC 与 LNB 的区别
    EIRP G/T 的意义
    语音的频率、频率分辨率、采样频率、采样点数、量化、增益
    机械波、电磁波的异同
  • 原文地址:https://www.cnblogs.com/zofun/p/11443941.html
Copyright © 2011-2022 走看看