java wav byte_Java输出的wav文件无法播放,有大佬给看看吗

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public class CutMusic {

public static void main(String[] args) {

//f1,f2分别为需要剪切的歌曲路径

File f1 = new File("D:\\bofangqi\\周杰伦 - 夜曲.wav");

File f2 = new File("D:\\bofangqi\\郑中基 - 答应不爱你.wav");

//f为合并的歌曲

File f = new File("D:\\bofangqi\\MergeMusic.wav");

cut1(f1,f2,f);

}

public static void cut1(File f1,File f2,File f){

BufferedInputStream bis1 = null;

BufferedInputStream bis2 = null;

BufferedOutputStream bos = null;

//第一首歌剪切部分起始字节

int start1 = 2375680;//320kbps(比特率)*58s*1024/8=2375680 比特率可以查看音频属性获知

int end1 = 4915200;//320kbps*120s*1024/8=4915200

//第二首歌剪切部分起始字节,计算方式同上

int start2 = 3686400;

int end2 = 5324800;

int tatol1 = 0;

int tatol2 = 0;

try {

//两个输入流

bis1 = new BufferedInputStream(new FileInputStream(f1));

bis2 = new BufferedInputStream(new FileInputStream(f2));

//缓冲字节输出流(true表示可以在流的后面追加数据,而不是覆盖!!)

bos = new BufferedOutputStream(new FileOutputStream(f,true));

//第一首歌剪切、写入

byte[] b1= new byte[1024];

int len1 = 0;

while((len1 = bis1.read(b1))!=-1){

tatol1+=len1; //累积tatol

if(tatol1

continue;

}

bos.write(b1); //写入的都是在我们预先指定的字节范围之内

if(tatol1>=end1 ){ //当tatol的值超过预先设定的范围,则立刻刷新bos流对象,并结束循环

bos.flush();

break;

}

}

System.out.println("第一首歌剪切完成!");

//第二首歌剪切、写入,原理同上

byte[] b2= new byte[1024];

int len2 = 0;

while((len2 = bis2.read(b2))!=-1){

tatol2 += len2;

if(tatol2 < start2){

continue;

}

bos.write(b2);

if(tatol2>=end2){

bos.flush();

break;

}

}

System.out.println("第二首歌剪切完成!");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {//切记要关闭流!!

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

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

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

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值