2014-02-07 36 views
5

我有以下代码:setOutputFormat称为处于无效状态:4(为什么)

Log.i("xx","A"); 
       media_recorder = new MediaRecorder(); 
Log.i("xx","B"); 
       media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
Log.i("xx","C"); 
       media_recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
Log.i("xx","D"); 
       media_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
Log.i("xx","E"); 
       media_recorder.setVideoSize(320, 240); 
Log.i("xx","F"); 
       media_recorder.setVideoFrameRate(15); 
Log.i("xx","G"); 
       CamcorderProfile profile = CamcorderProfile.get(CameraInfo.CAMERA_FACING_FRONT,CamcorderProfile.QUALITY_LOW); 
Log.i("xx","H"); 
       media_recorder.setProfile(profile); 
Log.i("xx","I"); 
       media_recorder.setOutputFile(fname); 

当执行的代码,我看到在我的日志以下;

02-07 16:12:47.628: I/xx(15436): A 
02-07 16:12:47.628: I/xx(15436): B 
02-07 16:12:47.638: I/xx(15436): C 
02-07 16:12:47.638: I/xx(15436): D 
02-07 16:12:47.638: I/xx(15436): E 
02-07 16:12:47.638: I/xx(15436): F 
02-07 16:12:47.638: I/xx(15436): G 
02-07 16:12:47.638: I/xx(15436): H 
02-07 16:12:47.638: E/MediaRecorder(15436): setOutputFormat called in an invalid state: 4 

这混淆了我,因为到setOutputFormat呼叫被“C”和“d”之间进行的,但错误的报告显示H(从来没有达到“I”)后,立即。所以现在我不知道是什么导致了错误,而且我对发生错误的位置感到困惑。

编辑:我刚刚在调试器中加入代码 - 并确信在调用setProfile(profile)期间发生错误...因此,看起来对setOutputFormat(在“C” &“D”)肯定已经工作了,但是然后setProfile必须自己再次调用setOutputFormat,然后失败......这是怎么回事?

编辑:什么是无效状态4实际上是什么意思?是否有某个列表可以告诉您每个可能的无效状态编号1,2,3,4 ...等的含义?

回答

7

这里是setProfile方法的源代码:

public void setProfile(CamcorderProfile profile) { 
    setOutputFormat(profile.fileFormat); 
    setVideoFrameRate(profile.videoFrameRate); 
    setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); 
    setVideoEncodingBitRate(profile.videoBitRate); 
    setVideoEncoder(profile.videoCodec); 
    if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW && 
     profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) { 
    // Nothing needs to be done. Call to setCaptureRate() enables 
    // time lapse video recording. 
    } else { 
    setAudioEncodingBitRate(profile.audioBitRate); 
    setAudioChannels(profile.audioChannels); 
    setAudioSamplingRate(profile.audioSampleRate); 
    setAudioEncoder(profile.audioCodec); 
    } 
} 

例如它将outputFormat,videoSize,encoder和frameRate设置为配置文件中的值。所以你的代码:

Log.i("xx","C"); 
media_recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
Log.i("xx","D"); 
media_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
Log.i("xx","E"); 
media_recorder.setVideoSize(320, 240); 
Log.i("xx","F"); 
media_recorder.setVideoFrameRate(15); 

至少是无用的,也许在这些调用期间它改变状态。尝试没有它。

2

发生这种情况的原因是您无法在不调用reset()的情况下更改MediaRecorder中的值,因此您必须在MediaRecorder本身上设置所有内容(实际上,您必须在API级别8,Android 2.2之前执行此操作)。

或者,您可以创建基于例如自定义摄像机配置文件的自定义摄像机配置文件。你想要的,进行相应的修改,并设置轮廓上MediaRecorder,就像这样:

// establish the media recorder 
    MediaRecorder media_recorder = new MediaRecorder(); 
    media_recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

    // Customise your profile based on a pre-existing profile 
    CamcorderProfile profile = CamcorderProfile.get(CameraInfo.CAMERA_FACING_FRONT,CamcorderProfile.QUALITY_LOW); 
    profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4; 
    profile.videoCodec = MediaRecorder.VideoEncoder.MPEG_4_SP; 
    profile.videoFrameHeight = 240; 
    profile.videoFrameWidth = 320; 
    profile.videoBitRate = 15; 

    // Apply to MediaRecorder 
    media_recorder.setProfile(profile); 
3

在你上面的代码media_recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)被调用了两次。 1日发生的在你的代码你media_recorder.setProfile(profile);方法即Log.i("xx","C"); & Log.i("xx","D");和第二occurence在MediaRecorder内部调用之间调用,Log.i("xx","H"); & Log.i("xx","I");之间。

因为MediaRecorder对象应遵循的生命周期象下面这样: enter image description here

参考setProfile在MediaRecoder类()方法click here &再次比较你的代码。

相关问题