IO:字节流截取音乐

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 截取一个音乐的高潮部分
 *
 */
public class CutMusic {

public static void main(String[] args) {


//如:截取一段 40s~80秒 比特率为320kpbs
// start=40*320*1024/8 = 1,638,400
// end=82*320*1024/8 = 3,358,720

BufferedInputStream bis = null;
BufferedOutputStream bos = null;

int start = 1638400;
int end = 3358720;

try {

bos = new BufferedOutputStream(new FileOutputStream(“E:\\music\\截取.mp3”));

bis = new BufferedInputStream(new FileInputStream(“E:\\music\\待截取.mp3”));

byte[] b = new byte[512];

int len = 0;

int total = 0;

while ((len = bis.read(b)) != -1) {

total += len;

// 如果小于开始字节的就抛弃掉

if (total < start) {

continue;

}

bos.write(b);

// 如果大于 结尾字节数 就终止读取

if (total >= end) {

bos.flush();

break;

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bos != null) bos.close();

if (bis != null) bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

    原文作者:来源于互联网
    原文地址: https://blog.csdn.net/lanqiao_zcg/article/details/70193573
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞