我正在编写一个相机应用程序,但遇到了 S3 的问题。每当我开始录制时,显示就会变成垃圾(参见下面的屏幕截图)。然后,当我停止录制时,我得到一个异常:
10-02 13:36:31.647: E/MediaRecorder(24283): stop failed: -1007
10-02 13:36:31.647: D/AndroidRuntime(24283): Shutting down VM
10-02 13:36:31.647: W/dalvikvm(24283): threadid=1: thread exiting with uncaught exception (group=0x40c49a68)
10-02 13:36:31.647: E/AndroidRuntime(24283): FATAL EXCEPTION: main
10-02 13:36:31.647: E/AndroidRuntime(24283): java.lang.RuntimeException: stop failed.
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.media.MediaRecorder.native_stop(Native Method)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.media.MediaRecorder.stop(MediaRecorder.java:742)
10-02 13:36:31.647: E/AndroidRuntime(24283): at com.myapp.android.ui.camera.NewCameraActivity.stopRecording(NewCameraActivity.java:178)
10-02 13:36:31.647: E/AndroidRuntime(24283): at com.myapp.android.ui.camera.NewCameraActivity.toggleRecording(NewCameraActivity.java:189)
10-02 13:36:31.647: E/AndroidRuntime(24283): at com.myapp.android.ui.camera.NewCameraActivity.onClick(NewCameraActivity.java:97)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.view.View.performClick(View.java:3565)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.view.View$PerformClick.run(View.java:14165)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.os.Handler.handleCallback(Handler.java:605)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.os.Handler.dispatchMessage(Handler.java:92)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.os.Looper.loop(Looper.java:137)
10-02 13:36:31.647: E/AndroidRuntime(24283): at android.app.ActivityThread.main(ActivityThread.java:4514)
10-02 13:36:31.647: E/AndroidRuntime(24283): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 13:36:31.647: E/AndroidRuntime(24283): at java.lang.reflect.Method.invoke(Method.java:511)
10-02 13:36:31.647: E/AndroidRuntime(24283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-02 13:36:31.647: E/AndroidRuntime(24283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-02 13:36:31.647: E/AndroidRuntime(24283): at dalvik.system.NativeStart.main(Native Method)
我已经在 Galaxy Nexus (4.1)、Galaxy S2、Nexus S 和 Galaxy Tab 10.1 上测试了我的应用程序。他们都工作正常。我遵循了视频录制的开发指南。我不明白为什么这个设备与其他设备如此不同。这是我在设备上看到的。第一张照片是在我开始录制之前。第二张照片是我开始录制后发生的事情。
这是我准备和启动MediaRecorder
对象的代码:
@Override
public void onClick( View v ) {
switch (v.getId()) {
case R.id.camera_action_ImageView:
int mode = getMode();
if ( mode == MODE_PHOTO ) {
focusThenTakePicture();
}
else if ( mode == MODE_VIDEO ) {
toggleRecording();
}
break;
}
}
private void startRecording() {
if ( prepareRecorder() ) {
getRecorder().start();
setRecording( true );
}
}
@TargetApi( 9 )
private boolean prepareRecorder() {
Camera camera = getCamera();
camera.unlock();
MediaRecorder recorder = new MediaRecorder();
setRecorder( recorder );
recorder.setCamera( camera );
recorder.setAudioSource( MediaRecorder.AudioSource.CAMCORDER );
recorder.setVideoSource( MediaRecorder.VideoSource.CAMERA );
CamcorderProfile profile;
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD ) {
profile = CamcorderProfile.get( CamcorderProfile.QUALITY_HIGH );
}
else {
profile = CamcorderProfile.get( getCameraId(), CamcorderProfile.QUALITY_HIGH );
}
recorder.setProfile( profile );
File outputFile = LocalMediaUtil.getOutputMediaFile( LocalMediaUtil.MEDIA_TYPE_VIDEO );
setRecorderOutputFile( outputFile );
recorder.setOutputFile( outputFile.toString() );
recorder.setPreviewDisplay( getPreview().getHolder().getSurface() );
try {
recorder.prepare();
}
catch (Exception e) {
camera.lock();
setRecorder( null );
return false;
}
return true;
}
private void stopRecording() {
MediaRecorder recorder = getRecorder();
recorder.stop();
releaseRecorder();
setRecording( false );
LocalMediaUtil.scanMedia( this, getRecorderOutputFile().toString(), 90 );
setRecorderOutputFile( null );
}
private void toggleRecording() {
if ( isRecording() ) {
stopRecording();
}
else {
startRecording();
}
}
private void releaseRecorder() {
MediaRecorder recorder = getRecorder();
if ( recorder != null ) {
recorder.reset();
recorder.release();
setRecorder( null );
getCamera().lock();
}
}
编辑:所以这与CamcorderProfile
设置有关。我将其更改为CamcorderProfile.QUALITY_LOW
并且效果很好。那么如何才能获得没有乱码输出的高分辨率视频呢?
Edit2:所以有了CamcorderProfile.QUALITY_LOW
设置,我使用录像机没有错误。但是,输出视频看起来与上面发布的乱码截图非常相似。那么给了什么?