栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从原始文件中获取最多x个字节的AudioInputStream(剪切音频文件)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从原始文件中获取最多x个字节的AudioInputStream(剪切音频文件)

下面的代码向您展示如何复制音频流的一部分,从一个文件读取并写入另一个文件。

import java.io.*;import javax.sound.sampled.*;class AudioFileProcessor {  public static void main(String[] args) {    copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", 2, 1);  }  public static void copyAudio(String sourceFileName, String destinationFileName, int startSecond, int secondsToCopy) {    AudioInputStream inputStream = null;    AudioInputStream shortenedStream = null;    try {      File file = new File(sourceFileName);      AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);      AudioFormat format = fileFormat.getFormat();      inputStream = AudioSystem.getAudioInputStream(file);      int bytesPerSecond = format.getframeSize() * (int)format.getframeRate();      inputStream.skip(startSecond * bytesPerSecond);      long framesOfAudioToCopy = secondsToCopy * (int)format.getframeRate();      shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);      File destinationFile = new File(destinationFileName);      AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile);    } catch (Exception e) {      println(e);    } finally {      if (inputStream != null) try { inputStream.close(); } catch (Exception e) { println(e); }      if (shortenedStream != null) try { shortenedStream.close(); } catch (Exception e) { println(e); }    }  }  public static void println(Object o) {    System.out.println(o);  }  public static void print(Object o) {    System.out.print(o);  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号