日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

音乐MP3文件剪切 与 两个MP3文件合并

發布時間:2024/3/26 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 音乐MP3文件剪切 与 两个MP3文件合并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對Mp3文件剪切,剪切速度非常非常快。不到一秒

/**\* @param inputPath 原音樂路徑* @param outputPath 新音樂路徑* @param start 剪裁開始位置 單位毫秒* @param end 剪裁結束位置 單位毫秒* @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);//音軌數據往前讀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;}/*** 獲取音頻數據軌道* @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 {// 否則完全復制文件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 {// 否則完全復制內容至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();}

?

總結

以上是生活随笔為你收集整理的音乐MP3文件剪切 与 两个MP3文件合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。