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();
}
}
}
}