溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在Java項(xiàng)目中使用IO流實(shí)現(xiàn)一個(gè)音頻剪切和拼接功能

發(fā)布時(shí)間:2020-11-21 16:17:06 來源:億速云 閱讀:572 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)在Java項(xiàng)目中使用IO流實(shí)現(xiàn)一個(gè)音頻剪切和拼接功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

需求: 使用IO流將指定目錄下的若干個(gè)音頻文件的高潮部分,進(jìn)行剪切,并重新拼接成一首新的音頻文件 

思路(以兩首歌為例):

  第一首歌有一個(gè)輸入流對象bis1。第二首歌有一個(gè)輸入流對象bis2,他們公用一條輸出流對象bos(在選擇構(gòu)造方法的時(shí)候選擇含有布爾類型參數(shù)的那個(gè)),待第一首歌剪切完成后,在此基礎(chǔ)上追加第二首歌的“高潮部分”。

實(shí)現(xiàn)代碼:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * 音樂剪切和拼接(音樂串燒)
 * @author 
 *
 */
public class CutMusic {

 public static void main(String[] args) {
  //f1,f2分別為需要剪切的歌曲路徑
   File f1 = new File("E:\\CutMusicTest\\殘酷月光(Cover:林宥嘉).mp3");
  File f2 = new File("E:\\CutMusicTest\\慢慢.mp3");
  //f為合并的歌曲
  File f = new File("E:\\CutMusicTest\\MergeMusic.mp3");
  cut1(f1,f2,f);
 }
 
 public static void cut1(File f1,File f2,File f){
  BufferedInputStream bis1 = null;
  BufferedInputStream bis2 = null;
  BufferedOutputStream bos = null;
  //第一首歌剪切部分起始字節(jié)
  int start1 = 2375680;//320kbps(比特率)*58s*1024/8=2375680 比特率可以查看音頻屬性獲知
  int end1 = 4915200;//320kbps*120s*1024/8=4915200
  
  //第二首歌剪切部分起始字節(jié),計(jì)算方式同上
  int start2 = 3686400;
  int end2 = 5324800;
  
  int tatol1 = 0;
  int tatol2 = 0;
  try {
   //兩個(gè)輸入流
   bis1 = new BufferedInputStream(new FileInputStream(f1));
   bis2 = new BufferedInputStream(new FileInputStream(f2));
   //緩沖字節(jié)輸出流(true表示可以在流的后面追加數(shù)據(jù),而不是覆蓋?。。?
   bos = new BufferedOutputStream(new FileOutputStream(f,true));
   
   //第一首歌剪切、寫入
   byte[] b1= new byte[512];
   int len1 = 0;
   while((len1 = bis1.read(b1))!=-1){
    tatol1+=len1;   //累積tatol
    if(tatol1<start1 ){  //tatol小于起始值則跳出本次循環(huán)
     continue;
    }
    bos.write(b1);   //寫入的都是在我們預(yù)先指定的字節(jié)范圍之內(nèi)
    if(tatol1>=end1 ){  //當(dāng)tatol的值超過預(yù)先設(shè)定的范圍,則立刻刷新bos流對象,并結(jié)束循環(huán)
     bos.flush();
     break;
    }
    
   }
   System.out.println("第一首歌剪切完成!");
   
   //第二首歌剪切、寫入,原理同上
   byte[] b2= new byte[512];
   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 {//切記要關(guān)閉流??!
     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();
    }
  }
 }

}

獲取音頻文件比特率的方式:

在Java項(xiàng)目中使用IO流實(shí)現(xiàn)一個(gè)音頻剪切和拼接功能

運(yùn)行結(jié)果:

在Java項(xiàng)目中使用IO流實(shí)現(xiàn)一個(gè)音頻剪切和拼接功能

看完上述內(nèi)容,你們對在Java項(xiàng)目中使用IO流實(shí)現(xiàn)一個(gè)音頻剪切和拼接功能有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI