音乐MP3文件剪切 与 两个MP3文件合并
生活随笔
收集整理的這篇文章主要介紹了
音乐MP3文件剪切 与 两个MP3文件合并
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
對Mp3文件剪切,剪切速度非常非常快。不到一秒
/**\* @param inputPath 原音樂路徑* @param outputPath 新音樂路徑* @param start 剪裁開始位置 單位毫秒* @param end 剪裁結(jié)束位置 單位毫秒* @return*/@TargetApi(Build.VERSION_CODES.JELLY_BEAN)public boolean clip(String inputPath, String outputPath, int start, int end){MediaExtractor extractor = null;BufferedOutputStream outputStream = null;try {extractor = new MediaExtractor();extractor.setDataSource(inputPath);int track = getAudioTrack(extractor);if(track < 0){return false;}//選擇音頻軌道extractor.selectTrack(track);outputStream = new BufferedOutputStream(new FileOutputStream(outputPath), SAMPLE_SIZE);start = start * 1000;end = end * 1000;//跳至開始裁剪位置extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);while (true){ByteBuffer buffer = ByteBuffer.allocate(SAMPLE_SIZE);int sampleSize = extractor.readSampleData(buffer, 0);long timeStamp = extractor.getSampleTime();if(timeStamp > end && timeStamp - end >= 1000000){Toast.makeText(CutMusicActivity.this,"剪切完畢",Toast.LENGTH_SHORT).show();break;}if(sampleSize <= 0){break;}byte[] buf = new byte[sampleSize];buffer.get(buf, 0, sampleSize);//寫入文件outputStream.write(buf);//音軌數(shù)據(jù)往前讀extractor.advance();}} catch (IOException e) {e.printStackTrace();}finally {if(extractor != null){extractor.release();}if(outputStream != null){try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return true;}/*** 獲取音頻數(shù)據(jù)軌道* @param extractor* @return*/@TargetApi(Build.VERSION_CODES.JELLY_BEAN)private static int getAudioTrack(MediaExtractor extractor) {for(int i = 0; i < extractor.getTrackCount(); i++){MediaFormat format = extractor.getTrackFormat(i);String mime = format.getString(MediaFormat.KEY_MIME);if(mime.startsWith("audio")){return i;}}return -1;}兩個MP3文件合并
/*** @param path Mp301路徑* @param path1 Mp302路徑* @param compoundName 合成完畢的路徑* @return* @throws IOException*/public static String heBingMp3(String path, String path1, String compoundName) throws IOException {String fenLiData = fenLiData(path);String fenLiData2 = fenLiData(path1);File file = new File(fenLiData);File file1 = new File(fenLiData2);//生成處理后的文件File file2 = new File(compoundName);FileInputStream in = new FileInputStream(file);FileOutputStream out = new FileOutputStream(file2);byte bs[] = new byte[1024 * 4];int len = 0;//先讀第一個while ((len = in.read(bs)) != -1) {out.write(bs, 0, len);}in.close();out.close();//再讀第二個in = new FileInputStream(file1);out = new FileOutputStream(file2, true);//在文件尾打開輸出流byte bs1[] = new byte[1024 * 4];while ((len = in.read(bs1)) != -1) {out.write(bs1, 0, len);}in.close();out.close();if (file.exists()) file.delete();if (file1.exists()) file1.delete();return file2.getAbsolutePath();}private static String fenLiData(String path) throws IOException {File file = new File(path);// 原文件File file1 = new File(path + "01");// 分離ID3V2后的文件,這是個中間文件,最后要被刪除File file2 = new File(path + "001");// 分離id3v1后的文件RandomAccessFile rf = new RandomAccessFile(file, "rw");// 隨機讀取文件FileOutputStream fos = new FileOutputStream(file1);byte ID3[] = new byte[3];rf.read(ID3);String ID3str = new String(ID3);// 分離ID3v2if (ID3str.equals("ID3")) {rf.seek(6);byte[] ID3size = new byte[4];rf.read(ID3size);int size1 = (ID3size[0] & 0x7f) << 21;int size2 = (ID3size[1] & 0x7f) << 14;int size3 = (ID3size[2] & 0x7f) << 7;int size4 = (ID3size[3] & 0x7f);int size = size1 + size2 + size3 + size4 + 10;rf.seek(size);int lens = 0;byte[] bs = new byte[1024 * 4];while ((lens = rf.read(bs)) != -1) {fos.write(bs, 0, lens);}fos.close();rf.close();} else {// 否則完全復(fù)制文件int lens = 0;rf.seek(0);byte[] bs = new byte[1024 * 4];while ((lens = rf.read(bs)) != -1) {fos.write(bs, 0, lens);}fos.close();rf.close();}RandomAccessFile raf = new RandomAccessFile(file1, "rw");byte TAG[] = new byte[3];raf.seek(raf.length() - 128);raf.read(TAG);String tagstr = new String(TAG);if (tagstr.equals("TAG")) {FileOutputStream fs = new FileOutputStream(file2);raf.seek(0);byte[] bs = new byte[(int) (raf.length() - 128)];raf.read(bs);fs.write(bs);raf.close();fs.close();} else {// 否則完全復(fù)制內(nèi)容至file2FileOutputStream fs = new FileOutputStream(file2);raf.seek(0);byte[] bs = new byte[1024 * 4];int len = 0;while ((len = raf.read(bs)) != -1) {fs.write(bs, 0, len);}raf.close();fs.close();}if (file1.exists()) {file1.delete();}return file2.getAbsolutePath();}?
總結(jié)
以上是生活随笔為你收集整理的音乐MP3文件剪切 与 两个MP3文件合并的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 痞子衡嵌入式:超级下载算法RT-UFL
- 下一篇: 关于文档的层次结构