bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

利用libmp3lame實現(xiàn)在Android上錄音MP3文件示例-創(chuàng)新互聯(lián)

之前項目需要實現(xiàn)MP3的錄音,于是使用上了Lame這個庫。這次做一個demo,使用AndroidStudio+Cmake+NDK進行開發(fā)。利用Android SDK提供的AndroidRecorder進行錄音,得到PCM數(shù)據(jù),并使用jni調用Lame這個C庫將PCM數(shù)據(jù)轉換為MP3文件。并使用MediaPlayer對錄音的MP3文件進行播放。另外此次的按鍵是仿微信的語音按鍵,按下錄音,松開結束,若中途上滑松開即取消

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站制作、成都網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的涪城網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

效果如下:

利用libmp3lame實現(xiàn)在Android上錄音MP3文件示例

項目地址: LameMp3ForAndroid_jb51.rar

一、主要類的介紹

  • Mp3Recorder—— 是負責調用AudioRecorder進行錄音的類
  • SimpleLame——是負責將MP3Recorder錄制出的PCM數(shù)據(jù)轉換成MP3文件
  • DataEncodeThread——是負責執(zhí)行PCM轉MP3的線程
  • LameMp3Manager——是對Mp3Recorder的多一次封裝,增加了取消后刪除之前錄制的數(shù)據(jù)的邏輯
  • MediaPlayerUtil——是對系統(tǒng)的MediaPlayer進行簡單的封裝,使其只需要三步就可以播放音頻文件
  • MediaRecorderButton ——是一個仿微信錄音按鍵的控件,按下錄制,松開結束,錄制時上滑則取消錄制

二、錄制的流程

  1. Mp3Recorder調用startRecording()開始錄制并初始化DataEncoderThread線程,并定期將錄制的PCM數(shù)據(jù),傳入DataEncoderThread中。
  2. 在DataEncoderThread里,SimpleLame將Mp3Recorder傳入的PCM數(shù)據(jù)轉換成MP3格式并寫入文件,其中SimpleLame通過jni對Lame庫進行調用
  3. Mp3Recorder調用stopRecording()停止錄制,并通知DataEncoderThread線程錄制結束,DataEncoderThread將剩余的數(shù)據(jù)轉換完畢。

三、主要的實現(xiàn)代碼

Mp3Recorder

public class Mp3Recorder {
  static {
    System.loadLibrary("lamemp3");
  }
  //默認采樣率
  private static final int DEFAULT_SAMPLING_RATE = 44100;
  //轉換周期,錄音每滿160幀,進行一次轉換
  private static final int FRAME_COUNT = 160;
  //輸出MP3的碼率
  private static final int BIT_RATE = 32;
  //根據(jù)資料假定的大值。 實測時有時超過此值。
  private static final int MAX_VOLUME = 2000;
  private AudioRecord audioRecord = null;
  private int bufferSize;
  private File mp3File;
  private int mVolume;
  private short[] mPCMBuffer;
  private FileOutputStream os = null;
  private DataEncodeThread encodeThread;
  private int samplingRate;
  private int channelConfig;
  private PCMFormat audioFormat;
  private boolean isRecording = false;
  private ExecutorService executor = Executors.newFixedThreadPool(1);
  private OnFinishListener finishListener;

  public interface OnFinishListener {
    void onFinish(String mp3SavePath);
  }

  public Mp3Recorder(int samplingRate, int channelConfig, PCMFormat audioFormat) {
    this.samplingRate = samplingRate;
    this.channelConfig = channelConfig;
    this.audioFormat = audioFormat;
  }

  public Mp3Recorder() {
    this(DEFAULT_SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, PCMFormat.PCM_16BIT);
  }


  public void startRecording(File mp3Save) throws IOException {
    if (isRecording) return;
    this.mp3File = mp3Save;
    if (audioRecord == null) {
      initAudioRecorder();
    }
    audioRecord.startRecording();
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        isRecording = true;
        //循環(huán)的從AudioRecord獲取錄音的PCM數(shù)據(jù)
        while (isRecording) {
          int readSize = audioRecord.read(mPCMBuffer, 0, bufferSize);
          if (readSize > 0) {
            //待轉換的PCM數(shù)據(jù)放到轉換線程中
            encodeThread.addChangeBuffer(mPCMBuffer,readSize);
            calculateRealVolume(mPCMBuffer, readSize);
          }
        }
        // 錄音完畢,釋放AudioRecord的資源
        try {
          audioRecord.stop();
          audioRecord.release();
          audioRecord = null;
          // 錄音完畢,通知轉換線程停止,并等待直到其轉換完畢
          Message msg = Message.obtain(encodeThread.getHandler(), DataEncodeThread.PROCESS_STOP);
          msg.sendToTarget();
          encodeThread.join();
          //轉換完畢后回調監(jiān)聽
          if(finishListener != null) finishListener.onFinish(mp3File.getPath());
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          if (os != null) {
            try {
              os.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    };
    executor.execute(runnable);
  }

  public void stopRecording() throws IOException {
    isRecording = false;
  }

  //計算音量大小
  private void calculateRealVolume(short[] buffer, int readSize) {
    double sum = 0;
    for (int i = 0; i < readSize; i++) {
      sum += buffer[i] * buffer[i];
    }
    if (readSize > 0) {
      double amplitude = sum / readSize;
      mVolume = (int) Math.sqrt(amplitude);
    }
  }

  public int getVolume(){
    if (mVolume >= MAX_VOLUME) {
      return MAX_VOLUME;
    }
    return mVolume;
  }

  public int getMaxVolume(){
    return MAX_VOLUME;
  }

  public void setFinishListener(OnFinishListener listener){
    this.finishListener = listener;
  }

  private void initAudioRecorder() throws IOException {
    int bytesPerFrame = audioFormat.getBytesPerFrame();
    //計算緩沖區(qū)的大小,使其是設置周期幀數(shù)的整數(shù)倍,方便循環(huán)
    int frameSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, audioFormat.getAudioFormat()) / bytesPerFrame;
    if (frameSize % FRAME_COUNT != 0) {
      frameSize = frameSize + (FRAME_COUNT - frameSize % FRAME_COUNT);
    }
    bufferSize = frameSize * bytesPerFrame;

    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, channelConfig, audioFormat.getAudioFormat(), bufferSize);
    mPCMBuffer = new short[bufferSize];
    SimpleLame.init(samplingRate, 1, samplingRate, BIT_RATE);
    os = new FileOutputStream(mp3File);
    // 創(chuàng)建轉碼的線程
    encodeThread = new DataEncodeThread(os, bufferSize);
    encodeThread.start();
    //給AudioRecord設置刷新監(jiān)聽,待錄音幀數(shù)每次達到FRAME_COUNT,就通知轉換線程轉換一次數(shù)據(jù)
    audioRecord.setRecordPositionUpdateListener(encodeThread, encodeThread.getHandler());
    audioRecord.setPositionNotificationPeriod(FRAME_COUNT);
  }
}

分享標題:利用libmp3lame實現(xiàn)在Android上錄音MP3文件示例-創(chuàng)新互聯(lián)
新聞來源:http://vcdvsql.cn/article22/ddjicc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站微信小程序網(wǎng)站維護外貿網(wǎng)站建設靜態(tài)網(wǎng)站品牌網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名