<form id="dlljd"></form>
        <address id="dlljd"><address id="dlljd"><listing id="dlljd"></listing></address></address>

        <em id="dlljd"><form id="dlljd"></form></em>

          <address id="dlljd"></address>
            <noframes id="dlljd">

              聯系我們 - 廣告服務 - 聯系電話:
              您的當前位置: > 關注 > > 正文

              【世界報資訊】音頻裁剪代碼有哪些?基于androidapi16的音頻裁剪代碼分享

              來源:CSDN 時間:2023-01-10 15:08:11


              (資料圖片僅供參考)

              若有朋友需要WAV 音頻裁剪,可以參考我的另外一篇博文——wav 音頻裁剪 在android開發中,一說到音視頻,程序員第一反應肯定是FFMpeg,但是FFMpeg這個庫,又復雜,又性能差,如果只想簡單的要個音頻裁剪的功能,就要去引入這么大的庫,完全不值得。然而自從android api 16之后谷歌就開始在音視頻方面進行布局,android 18之后繼續豐富音視頻編碼庫,讓很多android app在不依賴于繁瑣jni就能進行一些基礎的音視頻開發。下面將是基于android api16的對mp3文件進行裁剪的代碼例子。當前由于時間問題,先分享最主流的mp3格式的音頻裁剪,以后有時間會分享更多其他音頻方面的經驗。

              //適當的調整SAMPLE_SIZE可以更加精確的裁剪音樂  private static final int SAMPLE_SIZE = 1024 * 200;  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  public static 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();              // >= 1000000是要裁剪停止和指定的裁剪結尾不小于1秒,否則可能產生需要9秒音頻              //裁剪到只有8.6秒,大多數音樂播放器是向下取整,這樣對于播放器變成了8秒,              // 所以要裁剪比9秒多一秒的邊界              if(timeStamp > end && timeStamp - end >= 1000000){                  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文件。絕大部分情況下,產品經理的需求都是希望裁剪后的音樂除了長度之外,其他的都和原始音頻一樣,以防不同的播放器由于編解碼庫的差異,導致播放失敗的情況。下面的代碼是用來提取音頻的關鍵數據,來比較裁剪之后的音頻和原始音頻的關鍵數據是否一樣。

              @TargetApi(Build.VERSION_CODES.JELLY_BEAN)private static void printMusicFormat(String musicPath){    try {        MediaExtractor extractor = new MediaExtractor();        extractor.setDataSource(musicPath);        MediaFormat format = extractor.getTrackFormat(getAudioTrack(extractor));        Log.i("music", "碼率:" + format.getInteger(MediaFormat.KEY_BIT_RATE));        Log.i("music", "軌道數:" + format.getInteger(MediaFormat.KEY_CHANNEL_COUNT));        Log.i("music", "采樣率:" + format.getInteger(MediaFormat.KEY_SAMPLE_RATE));    } catch (IOException e) {        e.printStackTrace();    }}

              通過反復驗證,通過以上方法裁剪的音頻除了時間長度之外,其他格式數據都和原始音頻一樣,裁剪的音頻在各種平臺(android, IOS, windows,mac)都能正常的播放和使用。

              責任編輯:

              標簽:

              相關推薦:

              精彩放送:

              新聞聚焦
              Top 中文字幕在线观看亚洲日韩