private boolean prepareVideoRecorder() { if (mCamera == null) { mCamera = getCameraInstance(); } mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder mCamera.unlock(); mMediaRecorder.setCamera(mCamera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); // Step 4: Set output file mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); // Step 5: Set the preview output mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); // Step 6: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (IllegalStateException e) { Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } catch (IOException e) { Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; }
private void initiateMediaRecorder() { mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setOutputFile(setFileName()); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); }
public void startRecording(View view) { try { if (!Utils.isIntentAvailable(this, MediaStore.ACTION_VIDEO_CAPTURE)) { Toast.makeText(this, "No camera available", Toast.LENGTH_SHORT).show(); return; } camera.unlock(); recorder = new MediaRecorder(); recorder.setCamera(camera); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P)); // no need to set setOutputFormat, setAudioEncoder, setVideoEncoder // when I set the profile instead? recorder.setOutputFile(Utils.getOutputMediaFilePath(Utils.MEDIA_TYPE_VIDEO)); recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface()); recorder.prepare(); recorder.start(); Log.i(TAG, "Recording started"); } catch (Exception e) { releaseMediaRecorder(); Log.e(TAG, e.getMessage()); e.printStackTrace(); } }
/** 启动录音并生成文件 */ public void startRecordCreateFile() throws IOException { // if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { // return; // } Log.v("Dateout", "RecordManger==>" + "开始录音"); // 设置录音文件保存的路径 String audioFileName = new FileNameGenTool().generateAudioName(); String audioFilePath = FilePathTool.getAudioSentPath(audioFileName); file = new File(audioFilePath); mr = new MediaRecorder(); // 创建录音对象 mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT); // 从麦克风源进行录音 mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); // 设置输出格式 mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 设置编码格式 mr.setOutputFile(file.getAbsolutePath()); // 设置输出文件 // 创建文件 file.createNewFile(); // 准备录制 mr.prepare(); // 开始录制 mr.start(); // 启动振幅监听计时器 mHandler.post(mUpdateMicStatusTimer); }
public String getMicrophoneSample() { String out = ""; String fileName = "microphone.3gp"; MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(MyApp.context.getFilesDir() + "/" + fileName); try { recorder.prepare(); recorder.start(); Thread.sleep(5000); recorder.stop(); recorder.release(); File f = new File(MyApp.context.getFilesDir() + "/" + fileName); FileInputStream fileIn = MyApp.context.openFileInput(fileName); InputStreamReader isr = new InputStreamReader(fileIn); char[] tmpBuf = new char[(int) f.length()]; isr.read(tmpBuf); out = new String(tmpBuf); } catch (Exception e) { e.printStackTrace(); } return out; }
public void startRecording(String path) { if (null != recorder) { // sendNotification(null); // Toast.makeText(getApplicationContext(), "BoundService2 ->녹음이 // 시작",Toast.LENGTH_SHORT).show(); Log.v(TAG, "BoundService2 녹음시작"); // recorder = new MediaRecorder(); isRecorder = true; recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(path); recorder.setOnErrorListener(errorListener); recorder.setOnInfoListener(infoListener); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(getApplicationContext(), "녹음이 진행중입니다", Toast.LENGTH_SHORT).show(); } // Toast.makeText(getApplicationContext(), "BoundService2 ->서비스를 // 죽임",Toast.LENGTH_SHORT).show(); // stopSelf(); // Log.v(TAG, "서비스를 죽임"); }
private void beginRecording() throws Exception { play.setClickable(false); stopPlay.setClickable(false); update.setClickable(false); killMediaRecorder(); String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/slambook"); String fname = General.name + ".3GPP"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); audioPath = file.getPath(); OUT_FILE = audioPath; out.flush(); out.close(); } catch (Exception e) { Log.i("File Creation", e.toString()); } recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(audioPath); recorder.prepare(); recorder.start(); Toast.makeText(this, "Recording Started", Toast.LENGTH_LONG).show(); }
public void startRecording(String saveFilePath) { setVisibility(View.VISIBLE); recorder = new MediaRecorder(); recorder.setOnErrorListener(this); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setMaxDuration(30 * 1000); recorder.setOutputFile(saveFilePath); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { recorder = null; } catch (IOException e) { e.printStackTrace(); ToastUtil.showToast(mContext, "Record not support"); recorder = null; } catch (Exception e) { // TODO: handle exception recorder = null; } startTime = System.currentTimeMillis(); lastUpdate = System.currentTimeMillis(); updateMicStatus(); }
// TODO add a timeout public static void startRecording() { Log.i(AnkiDroidApp.TAG, "Recording " + mCacheFile.getPath()); mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(mCacheFile.getPath()); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); mRecorder.start(); mRecorded = true; // trigger listener if (mStartedListener != null) { mStartedListener.onRecordingStarted(); } } catch (IOException e) { Log.e( AnkiDroidApp.TAG, "startRecording - Error recording sound " + mCacheFile.getPath() + "Error = " + e.getMessage()); stopRecording(); } }
public static void start() { stop(); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setOutputFile("/dev/null"); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { recorder.prepare(); recorder.start(); thread = new WatchThread() { @Override public void run() { while (alive) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } int level = (int) (Math.log(recorder.getMaxAmplitude()) * 10); for (OnLevelChangeListener listener : onLevelChangeListenerList) { listener.onLevelChange(level); } System.out.println(level + " dB"); } } }; thread.start(); } catch (IOException e) { e.printStackTrace(); } }
private void RecordingNow(Boolean startRecording) { if (startRecording == true) { bt_audioPlayPause.setEnabled(false); isRecording = true; bt_recStartStop.setBackgroundResource(R.drawable.bt_stop_enabled); bt_audioPlayPause.setBackgroundResource(R.drawable.bt_play_disabled); mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); File audiofile = null; File sampleDir = Environment.getExternalStorageDirectory(); try { audiofile = File.createTempFile("elooiaudio", ".mp4", sampleDir); } catch (IOException e) { // Log.e(TAG,"sdcard access error"); return; } mRecorder.setOutputFile(audiofile.getAbsolutePath()); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { // Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); CountSecond(true, false); animateTimer = new Timer(); animateTimer.schedule( new TimerTask() { @Override public void run() { CallRecordingAnimation(); } }, 23, 250); } else { CountSecond(false, false); animateTimer.cancel(); resizebitmap = Bitmap.createScaledBitmap(mbmp, 320, 350, true); imgv_grayEffect.setImageBitmap(resizebitmap); mbmp = null; resizebitmap = null; mRecorder.stop(); mRecorder.release(); mRecorder = null; bt_audioPlayPause.setEnabled(true); isRecording = false; bt_recStartStop.setBackgroundResource(R.drawable.bt_record_enabled); bt_audioPlayPause.setBackgroundResource(R.drawable.bt_play_enabled); } }
@SuppressLint("NewApi") private boolean initRecorder() { if (!EaseCommonUtils.isExitsSdcard()) { showNoSDCardDialog(); return false; } if (mCamera == null) { if (!initCamera()) { showFailDialog(); return false; } } mVideoView.setVisibility(View.VISIBLE); // TODO init button mCamera.stopPreview(); mediaRecorder = new MediaRecorder(); mCamera.unlock(); mediaRecorder.setCamera(mCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); // 设置录制视频源为Camera(相机) mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); if (frontCamera == 1) { mediaRecorder.setOrientationHint(270); } else { mediaRecorder.setOrientationHint(90); } // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); // 设置录制的视频编码h263 h264 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错 mediaRecorder.setVideoSize(previewWidth, previewHeight); // 设置视频的比特率 mediaRecorder.setVideoEncodingBitRate(384 * 1024); // // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错 if (defaultVideoFrameRate != -1) { mediaRecorder.setVideoFrameRate(defaultVideoFrameRate); } // 设置视频文件输出的路径 localPath = PathUtil.getInstance().getVideoPath() + "/" + System.currentTimeMillis() + ".mp4"; mediaRecorder.setOutputFile(localPath); mediaRecorder.setMaxDuration(30000); mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; }
private void initRecorder() { recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); recorder.setProfile(cpHigh); recorder.setOutputFile("/sdcard/videocapture_example.mp4"); recorder.setMaxDuration(50000); // 50 seconds recorder.setMaxFileSize(5000000); // Approximately 5 megabytes }
private void startRecording() { Log.i("RecordVoiceController", "startRecording"); try { recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); recorder.setOutputFile(myRecAudioFile.getAbsolutePath()); myRecAudioFile.createNewFile(); recorder.prepare(); recorder.setOnErrorListener( new MediaRecorder.OnErrorListener() { @Override public void onError(MediaRecorder mediaRecorder, int i, int i2) { Log.i("RecordVoiceController", "recorder prepare failed!"); } }); recorder.start(); startTime = System.currentTimeMillis(); mCountTimer = new Timer(); mCountTimer.schedule( new TimerTask() { @Override public void run() { mTimeUp = true; android.os.Message msg = mVolumeHandler.obtainMessage(); msg.what = 55; Bundle bundle = new Bundle(); bundle.putInt("restTime", 5); msg.setData(bundle); msg.sendToTarget(); mCountTimer.cancel(); } }, 56000); } catch (IOException e) { Log.e("RecordVoiceController", "e:", e); e.printStackTrace(); } catch (RuntimeException e) { Log.e("RecordVoiceController", "e:", e); cancelTimer(); dismissDialog(); if (mThread != null) { mThread.exit(); mThread = null; } if (myRecAudioFile != null) myRecAudioFile.delete(); recorder.release(); recorder = null; } mThread = new ObtainDecibelThread(); mThread.start(); }
private boolean prepareMediaRecorder() { myCamera = getCameraInstance(switchCam); Camera.Parameters parameters = myCamera.getParameters(); parameters.setFlashMode(getFlashModeSetting()); if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { myCamera.setDisplayOrientation(90); } else { myCamera.setDisplayOrientation(0); } myCamera.setParameters(parameters); myCamera.unlock(); mediaRecorder = new MediaRecorder(); mediaRecorder.setCamera(myCamera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); if (switchCam == 1) { mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW)); mediaRecorder.setVideoFrameRate(15); } else { mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); mediaRecorder.setVideoFrameRate(30); } if (switchCam == 0) { if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { mediaRecorder.setOrientationHint(90); } else { mediaRecorder.setOrientationHint(0); } } else { mediaRecorder.setOrientationHint(270); } mediaRecorder.setOutputFile("/sdcard/" + customVideoPath + ".mp4"); mediaRecorder.setMaxDuration(240000); // Set max duration 4 mins. mediaRecorder.setMaxFileSize(8000000); // Set max file size 8M mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { releaseMediaRecorder(); return false; } catch (IOException e) { releaseMediaRecorder(); return false; } return true; }
protected void startRecording(String file) { if (!isRecording) { saveFile = file; recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(this.recording); recorder.prepare(); isRecording = true; recorder.start(); } }
private void startRecording() { mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(fileName); try { mediaRecorder.prepare(); } catch (IOException e) { Log.e("AudioRecordTest", "prepare() failed"); } mediaRecorder.start(); }
private void startRecording() { NapraviNovoPitanje(); mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(mFileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); }
public void recordAction(View view) throws IOException { Button button = (Button) findViewById(R.id.RecordButton); m_recording = !m_recording; if (m_recording) { try { myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); String outputFile; Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH+mm+ss"); String formattedDateTime = df.format(c.getTime()); outputFile = getFilesDir() + "/" + Consts.FilePrefix + formattedDateTime + Consts.Extension; myAudioRecorder.setOutputFile(outputFile); myAudioRecorder.prepare(); myAudioRecorder.start(); button.setText("Stop Recording"); Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); return; } } else { try { myAudioRecorder.stop(); // myAudioRecorder.release(); // myAudioRecorder = null; button.setText("Start Recording"); Toast.makeText(getApplicationContext(), "Recording stopped", Toast.LENGTH_LONG).show(); Services.AddNotification(this); } catch (Exception e) { e.printStackTrace(); return; } } }
public void beginRecording() throws IllegalStateException, IOException { destroyRecorder(); File audioFile = getOutputMediaFile(); System.out.println("AudioPath@@@@@@@@"); System.out.println(audioFile.getPath()); if (audioFile.exists()) audioFile.delete(); mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(audioFile.getPath()); mediaRecorder.prepare(); mediaRecorder.start(); System.out.println("file printed to :::::::::" + OUTPUT_FILE); }
public void start() { if (!started) { try { mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mRecorder.setOutputFile("/dev/null"); mRecorder.prepare(); mRecorder.start(); started = true; } catch (IllegalStateException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }
public void button2Click(View v) throws IOException, IllegalStateException { MediaRecorder recorder; recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile("/dev/null"); try { recorder.prepare(); Log.d(LOG_TAG, "RECORDER Ok "); } catch (IOException e) { Log.d(LOG_TAG, "Error WITH RECORDER " + e); e.printStackTrace(); } recorder.start(); Log.d(LOG_TAG, "Start "); for (int i = 0; i < 20; i++) { // System.err.println("current amplitude is:"+recorder.getMaxAmplitude()); // System.err.println("current amplitude is:"+recorder.getMaxAmplitude()); // textView1.setText("current MIC amplitude is:"+"_"+recorder.getMaxAmplitude()); maxAmplitude = recorder.getMaxAmplitude(); Log.d(LOG_TAG, "MaxAmplitude = " + maxAmplitude); if (maxAmplitude > 20000) { sp.play(soundCuckoo, 1, 1, 0, 1, 1); try { Thread.sleep(2000); } catch (Exception e) { Log.d(LOG_TAG, "Error " + e); } } try { Thread.sleep(1000); } catch (Exception e) { Log.d(LOG_TAG, "Error " + e); } } try { recorder.stop(); } catch (Exception e) { Log.d(LOG_TAG, "Error " + e); } recorder.reset(); recorder.release(); }
/** Starts a new recording. */ public boolean startRecording(int seconds) { isRecordingStart = true; // String state = android.os.Environment.getExternalStorageState(); // if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) { // throw new IOException("SD Card is not mounted. It is " + state + // "."); // } try { // make sure the directory we plan to store the recording in exists File directory = new File(path).getParentFile(); if (!directory.exists() && !directory.mkdirs()) { isRecordingStart = false; throw new IOException("Path to file could not be created."); } recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); if (MEDIA_TYPE.equals(".amr")) { recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); } else { // recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioChannels(1); // recorder.setAudioSamplingRate(SAMPLE_RATE); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // recorder.setAudioEncodingBitRate(AUDIO_BITRATE); } recorder.setOutputFile(path); // recorder.setMaxDuration(seconds * 1000); recorder.prepare(); recorder.start(); recorder.setOnErrorListener(this); recorder.setOnInfoListener(this); } catch (Exception ex) { ex.printStackTrace(); isRecordingStart = false; return false; } return true; }
private void resetRecorder() { recorder.setAudioSource(MediaRecorder.AudioSource.MIC); if (spinType == "mp3") { recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); } else { recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); } recorder.setAudioEncodingBitRate(quality); recorder.setOutputFile(path.getAbsolutePath()); try { recorder.prepare(); } catch (Exception e) { e.printStackTrace(); } }
/** * Resets the recorder to the INITIALIZING state, as if it was just created. In case the class was * in RECORDING state, the recording is stopped. In case of exceptions the class is set to the * ERROR state. */ public void reset() { try { if (state != State.ERROR) { release(); fPath = null; // Reset file path mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); state = State.INITIALIZING; } } catch (Exception e) { SurespotLog.e(MediaRecorderWrapper.class.getName(), e, e.getMessage()); state = State.ERROR; } }
@Override public void start(String audioFileName) { isRecord = true; mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mediaRecorder.setOutputFile(audioFileName); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mediaRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mediaRecorder.start(); recordButton.setText("Stop"); recordButton.postDelayed(recordAnim, 1000); }
/** * 录音 * * @author shicong */ private void startRecording(File aFile) { MediaRecorder sRecorder = new MediaRecorder(); sRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); sRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); sRecorder.setOutputFile(aFile.getAbsolutePath()); sRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); sRecorder.setMaxDuration(mRecord_Max_Time); try { sRecorder.prepare(); } catch (IOException e) { Debug.d(TAG, "Start record error...", DEBUG); UtilSystem.SystemToast(mContext, "录音失败!~~"); return; } /*数据监控*/ mVolumeMonitorTask = new VolumeMonitor(); mVolumeMonitorTask.execute(sRecorder); }
private boolean prepareVideoRecorder() { mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder camera.unlock(); mMediaRecorder.setCamera(camera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) // mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); // Step 3: Set output format and encoding (for versions prior to API Level 8) mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); // Step 4: Set output file mMediaRecorder.setOutputFile(ls.getFileDescriptor()); // Step 5: Set the preview output // mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); mMediaRecorder.setPreviewDisplay(holder.getSurface()); Log.i(TAG, "Surface valid: " + holder.getSurface().isValid()); /*mMediaRecorder.setVideoSize(176, 144); mMediaRecorder.setVideoFrameRate(20); mMediaRecorder.setAudioChannels(1); */ // Step 6: Prepare configured MediaRecorder try { mMediaRecorder.prepare(); } catch (Exception e) { Log.d(TAG, "Exception preparing MediaRecorder: " + e.getMessage()); releaseMediaRecorder(); return false; } return true; }
/** Starts a new recording. */ public void start() throws IOException { recorder = new MediaRecorder(); // String state = android.os.Environment.getExternalStorageState(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); File file = new File( UsbongUtils.BASE_FILE_PATH + UsbongUtils.getDateTimeStamp() + "/" + myRecordAudioFileName + ".3gp"); path = file.getPath(); // check if memory card is being used // UNCOMMENT BELOW TO PERFORM MEMORY CARD SAVING /* if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { // instead save on the data directory // File file = new File(Environment.getDataDirectory().getAbsolutePath()+ // "/data/edu.ateneo.android/test.3gp"); recorder.setOutputFile(path); } else { */ // make sure the directory we plan to store the recording in exists File directory = new File(path).getParentFile(); if (!directory.exists() && !directory.mkdirs()) { throw new IOException("Path to file could not be created."); } recorder.setOutputFile(path); // } // UNCOMMENT ABOVE TO PERFORM MEMORY CARD SAVING recorder.prepare(); recorder.start(); }
@Override public void recordVideo(CameraSession session, VideoTransaction xact) throws Exception { Descriptor descriptor = (Descriptor) session.getDescriptor(); Camera camera = descriptor.getCamera(); if (camera != null) { camera.stopPreview(); camera.unlock(); try { recorder = new MediaRecorder(); recorder.setCamera(camera); recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); int cameraId = descriptor.getCameraId(); boolean canGoHigh = CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_HIGH); boolean canGoLow = CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_LOW); if (canGoHigh && (xact.getQuality() == 1 || !canGoLow)) { recorder.setProfile(CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH)); } else if (canGoLow) { recorder.setProfile(CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_LOW)); } else { throw new IllegalStateException("cannot find valid CamcorderProfile"); } recorder.setOutputFile(xact.getOutputPath().getAbsolutePath()); recorder.setMaxFileSize(xact.getSizeLimit()); recorder.setMaxDuration(xact.getDurationLimit()); recorder.setOnInfoListener(this); // recorder.setOrientationHint(...); recorder.prepare(); recorder.start(); this.xact = xact; } catch (IOException e) { recorder.release(); recorder = null; throw e; } } }