• 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[] = {"E:/星月神话.mp3","E:/我只在乎你.mp3"};
            //设置byte数组,每次往输出流中传入8K的内容
            byte by[] = new byte[1024*8];
            try
            {
                fileOutputStream = new FileOutputStream("E:/合并.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();
                }
            }
        }
    }
  • 相关阅读:
    [转]Maven类包冲突终极三大解决技巧
    python chardet 模块
    python pretty-errors模块
    认识执行时机
    python对象销毁顺序
    python字符串驻留
    python绘图练习
    小练习2
    小练习
    4G 内存处理 10G 大小的文件
  • 原文地址:https://www.cnblogs.com/gopark/p/9507171.html
Copyright © 2020-2023  润新知