MediaRecorder 录音实现

文中一共展示了MediaRecorder.setAudioEncoder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: 


public void Audio recordAudio(UQI uqi, long duration) throws IOException {
    List<Integer> amplitudes = new ArrayList<>();

    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(Globals.AudioConfig.audioSource);
    recorder.setOutputFormat(Globals.AudioConfig.outputFormat);
    recorder.setAudioEncoder(Globals.AudioConfig.audioEncoder);

    String audioPath = "temp/audio_" + TimeUtils.getTimeTag() + ".amr";
    File tempAudioFile = StorageUtils.getValidFile(uqi.getContext(), audioPath, false);
    recorder.setOutputFile(tempAudioFile.getAbsolutePath());

    recorder.prepare();
    recorder.start();   // Recording is now started

    long startTime = System.currentTimeMillis();
    while (true) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime > duration) {
            break;
        }
        amplitudes.add(recorder.getMaxAmplitude());
    }

    recorder.stop();
    recorder.reset();   // You can reuse the object by going back to setAudioSource() step
    recorder.release(); // Now the object cannot be reused

    AudioData audioData = AudioData.newTempRecord(tempAudioFile, amplitudes);

    return new Audio(startTime, audioData);
}
 

示例2: 


public void startRecording(Context context, MediaRecorder recorder, String word) {
    recorder.reset();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    recorder.setMaxDuration(10000);
    recorder.setAudioSamplingRate(16000);
    recorder.setAudioEncodingBitRate(16000*4*1);
    recorder.setAudioSamplingRate(44100);
    recorder.setAudioChannels(1);
    recorder.setOutputFile(context.getFilesDir().getAbsolutePath() + "/" + word + ".aac");
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    recorder.start();
}
 

示例3: 


public void startRecording() {
    if (this.paras.isTonghualuyin()) {
        try {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(4);
            mediaRecorder.setAudioSamplingRate(16000);
            mediaRecorder.setOutputFormat(3);
            mediaRecorder.setAudioEncoder(1);
            mediaRecorder.setOutputFile(this.file.getAbsolutePath());
            mediaRecorder.prepare();
            mediaRecorder.start();
            this.isRecord = true;
        } catch (Exception e) {
            this.isRecord = false;
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
        }
    }
}
 

示例4: 


private void startRecording() {
    audioRec = new MediaRecorder();
    audioRec.setAudioSource(MediaRecorder.AudioSource.MIC);
    audioRec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    audioRec.setOutputFile(audioFilePath);
    audioRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    audioRec.setAudioSamplingRate(256);
    try {
        audioRec.prepare();
        audioRec.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 

示例5: 


public void startRecording() {

        setFileNameAndPath();

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(mFilePath);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

        try {
            mRecorder.prepare();
            mRecorder.start();
            mStartingTimeMillis = System.currentTimeMillis();

            //startTimer();
            //startForeground(1, createNotification());

        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }
 

示例6: 


private void initMediaRecorder() {
    try {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mediaRecorder.setAudioSamplingRate(44100);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setAudioEncodingBitRate(96000);
        Date currentTime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        fileName = dateFormat.format(currentTime) + ".m4a";
        File audioFile = new File(getCacheDir().getAbsolutePath() + "/" + fileName);  // FIXED: 路径待确定
        Log.i("nib", audioFile.getAbsolutePath());
        mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
        mediaRecorder.prepare();
        mediaRecorder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 

示例7:


private void getRecorder() {
    recorder = new MediaRecorder();
    //麦克风
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    //设置格式 3GP
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    //设置保存目录 权限
    recorder.setOutputFile("sdcard/audio.3gp");
    //设置音频编码
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        //准备录音
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 

示例8: 


RecordingController(String savedRecording) throws IOException {
  // pick a pathname if none was specified
  file = (savedRecording.equals("")) ?
      FileUtil.getRecordingFile("3gp").getAbsolutePath() :
        savedRecording;

  recorder = new MediaRecorder();
  recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  Log.i(TAG, "Setting output file to " + file);
  recorder.setOutputFile(file);
  Log.i(TAG, "preparing");
  recorder.prepare();
  recorder.setOnErrorListener(SoundRecorder.this);
  recorder.setOnInfoListener(SoundRecorder.this);
}
 

示例9: 


public void prepare() throws IOException {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mRecorder.setAudioSamplingRate(44100);
    mRecorder.setAudioEncodingBitRate(96000);
    mRecorder.setOutputFile(mFilePath);
    mRecorder.prepare();
}
 

示例10: 


private void initRecord() {
        try {
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.reset();
            mMediaRecorder.setCamera(mCamera);
            mMediaRecorder.setOnErrorListener(this);
            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频源
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频源
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//视频输出格式 也可设为3gp等其他格式
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频格式
            mMediaRecorder.setVideoSize(mWidthPixel, mHeightPixel);//设置分辨率
//        mMediaRecorder.setVideoFrameRate(25);// 设置每秒帧数 这个设置有可能会出问题,有的手机不支持这种帧率就会录制失败,这里使用默认的帧率,当然视频的大小肯定会受影响
            if (mPictureSize < 3000000) {//这里设置可以调整清晰度
                mMediaRecorder.setVideoEncodingBitRate(3 * 1024 * 512);
            } else if (mPictureSize <= 5000000) {
                mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 512);
            } else {
                mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);
            }
            mMediaRecorder.setOrientationHint(90);//输出旋转90度,保持竖屏录制
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//视频录制格式
            //mMediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000);
            mMediaRecorder.setOutputFile(mRecordFile.getAbsolutePath());
            mMediaRecorder.prepare();
        } catch (Exception e) {
            e.printStackTrace();
            releaseCamera();
        }
    }
 

示例11:


private void startRecording() {

        TransitionManager.beginDelayedTransition(mContainer);

        mFab.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.icon_stop_fab_mini));
        mFab.setSize(FloatingActionButton.SIZE_MINI);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mFab.getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);    //Same as lp.removeRule(...) only this call is supported from API 17 :(
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
        mFab.setLayoutParams(lp);

        mTapToStartRecording.setVisibility(View.GONE);
        mRecordTime.setVisibility(View.VISIBLE);
        mVisualizer.setVisibility(View.VISIBLE);
        mRecIcon.setVisibility(View.VISIBLE);


        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mAudioFilePath);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed");
        }

        mRecorder.start();
        mVizHandler.post(updateVisualizer);
        startTime = SystemClock.uptimeMillis();
        mTextHandler.postDelayed(updateTime, REPEAT_INTERVAL_TIME);
    }
 

示例12: 


@Override
  protected String doInBackground(String... params) {     
   	MediaRecorder recorder = new MediaRecorder();;

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
       String currentDateandTime = sdf.format(new Date());
       
       String filename =currentDateandTime + ".3gp";

       File diretory = new File(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("File", "") + File.separator + "Audio");
       diretory.mkdirs();
    	File outputFile = new File(diretory, filename);
       
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     recorder.setMaxDuration(Integer.parseInt(i));
     recorder.setMaxFileSize(1000000);
     recorder.setOutputFile(outputFile.toString());

 try 
    {
       recorder.prepare();
       recorder.start();
    } catch (IOException e) {
       Log.i("com.connect", "io problems while preparing");
      e.printStackTrace();
    }		   
return "Executed";
  }
 

示例13: 


private void startRecording() {
    String statusMessage = "";
    String audioFileName = prepareOutputFile();
    try {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setOutputFile(audioFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setAudioEncodingBitRate(96000);
        mRecorder.setAudioSamplingRate(mSamplingRate);
        mRecorder.setOnErrorListener(mOnErrorListener);
        mRecorder.prepare();
        mRecorder.start();
        mRecordingStatus = RECORDING_STATUS_STARTED;
        startForeground(1, mRecordingNotif);
    } catch (Exception e) {
        e.printStackTrace();
        mRecordingStatus = RECORDING_STATUS_ERROR;
        statusMessage = e.getMessage();
    } finally {
        Intent i = new Intent(ACTION_RECORDING_STATUS_CHANGED);
        i.putExtra(EXTRA_RECORDING_STATUS, mRecordingStatus);
        if (mRecordingStatus == RECORDING_STATUS_STARTED) {
            i.putExtra(EXTRA_AUDIO_FILENAME, audioFileName);
        }
        i.putExtra(EXTRA_STATUS_MESSAGE, statusMessage); 
        sendBroadcast(i);
    }
}
 

示例14: 


public AudioRecordingAacThread() {
    try{
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        // 设置MediaRecorder录制的音频格式(不要修改其它格式了,后端对格式做了限定)
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setAudioSamplingRate(8000);
    }catch (Exception e){

    }

}
 

示例15: 


public void prepareAudio() {
    try {
        // 一开始应该是false的
        isPrepared = false;

        File dir = new File(mDirString);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String fileNameString = generalFileName();
        File file = new File(dir, fileNameString);

        mCurrentFilePathString = file.getAbsolutePath();

        mRecorder = new MediaRecorder();
        // 设置输出文件
        mRecorder.setOutputFile(file.getAbsolutePath());
        // 设置mediaRecorder的音频源是麦克风
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 设置文件音频的输出格式为amr
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
        // 设置音频的编码格式为amr
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        // 严格遵守google官方api给出的mediaRecorder的状态流程图
        mRecorder.prepare();

        mRecorder.start();
        // 准备结束
        isPrepared = true;
        // 已经准备好了,可以录制了
        if (mListener != null) {
            mListener.wellPrepared();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值