Java混剪音频

分享一个之前看过的程序,可以用来剪辑特定长度的音频,将它们混剪在一起,思路如下:

1.使用 FileInputStream 输入两个音频

2.使用 FileInputStream的skip(long n) 方法跳过特定字节长度的音频文件,比如说:输入 skip(1024*1024*3),这样就能丢弃掉音频文件前面的 3MB 的内容。

3.截取中间特定长度的音频文件:每次输入 8KB 的内容,使用 count 记录输入次数,达到设置的次数就终止音频输入。比如说要截取 2MB 的音频,每次往输入流中输入 8KB 的内容,就要输入 1024*2/8 次。

4.往同一个输出流 FileOutputStream 中输出音频,并生成文件,实现音频混合。

相关代码:

复制代码

public class MusicCompound 
{
    public static void main(String args[])
    {
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        String fileNames[] = {"D:/01.mp3","D:/02.mp3"};
        //设置byte数组,每次往输出流中传入8K的内容
        byte by[] = new byte[1024*8];
        try
        {
            fileOutputStream = new FileOutputStream("D:/合并音乐.mp3");
            for(int i=0;i<2;i++)
            {
                int count = 0;
                fileInputStream = new FileInputStream(fileNames[i]);
                //跳过前面3M的歌曲内容
                fileInputStream.skip(1024*1024*3);
                while(fileInputStream.read(by) != -1)
                {                    
                    fileOutputStream.write(by);
                    count++;
                    System.out.println(count);
                    //要截取中间2MB的内容,每次输入8k的内容,所以输入的次数是1024*2/8
                    if(count == (1024*2/8))
                    {
                        break;
                    }
                }
            }
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                //输出完成后关闭输入输出流
                fileInputStream.close();
                fileOutputStream.close();
            } 
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值