如何在android3.1中使用媒体记录器解决录像机中的非法状态异常问题

时间:2011-10-21 06:42:46

标签: android

我想使用自定义录像机录制视频,以便在Android中使用媒体录制器保存mp4视频。

任何人都可以提供代码吗?我完成了代码(如下所示),但它显示非法异常,任何人都能说出什么是问题吗?

import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

public class CustomCameraRecordActivity extends Activity implements OnClickListener, SurfaceHolder.Callback 
{     
    MediaRecorder recorder;     
    SurfaceHolder holder;   
    boolean recording = false;
    @Override 
    public void onCreate(Bundle savedInstanceState) 
     {    
    super.onCreate(savedInstanceState);     
    requestWindowFeature(Window.FEATURE_NO_TITLE);     
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
    setContentView(R.layout.main);     
    recorder = new MediaRecorder();    
    initRecorder();     
    SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);    
    holder = cameraView.getHolder();    
    holder.addCallback(this);    
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    
    cameraView.setClickable(true);    
    cameraView.setOnClickListener(this); }
private void initRecorder() 
{     
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);   
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);   
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);    
    recorder.setProfile(cpHigh);    
    recorder.setOutputFile(Environment.getExternalStorageDirectory()+"VideoGcm"+System.currentTimeMillis()+".mp4");   
    recorder.setMaxDuration(50000); // 50 seconds     recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }  
private void prepareRecorder() 
{    
    recorder.setPreviewDisplay(holder.getSurface());    
    try
    {       
        recorder.prepare();    
    }
    catch (IllegalStateException e)
             {         
        e.printStackTrace();         
finish();     
} catch (IOException e)
{         e.printStackTrace();     
finish();   
} 
}  

public void onClick(View v)
{   
    if (recording)
    {      
    recorder.stop();         
    recording = false;          
    // Let's initRecorder so we can record again    
    initRecorder();        
    prepareRecorder();     
    } 
else 
{         
    recording = true;    
    recorder.start();   
    } 
}  
public void surfaceCreated(SurfaceHolder holder)
{   
    prepareRecorder(); 
    }  
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) 
{ 
}  

public void surfaceDestroyed(SurfaceHolder holder) 
{     if (recording)
{         
    recorder.stop();      
    recording = false;   
    }     
recorder.release();   
finish(); 
}


}

由于

10-20 19:19:37.540: ERROR/AndroidRuntime(2718): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hcl.camera/com.hcl.camera.CustomCameraRecordActivity}: java.lang.IllegalStateException
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.os.Looper.loop(Looper.java:132)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread.main(ActivityThread.java:4025)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at java.lang.reflect.Method.invoke(Method.java:491)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at dalvik.system.NativeStart.main(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): Caused by: java.lang.IllegalStateException
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.media.MediaRecorder.setOutputFormat(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.media.MediaRecorder.setProfile(MediaRecorder.java:295)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at com.hcl.camera.CustomCameraRecordActivity.initRecorder(CustomCameraRecordActivity.java:45)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at com.hcl.camera.CustomCameraRecordActivity.onCreate(CustomCameraRecordActivity.java:32)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718):     ... 11 more
10-20 19:20:40.720: ERROR/MediaRecorder(2759): start called in an invalid state: 4
10-20 19:20:40.730: ERROR/AndroidRuntime(2759): FATAL EXCEPTION: main

1 个答案:

答案 0 :(得分:2)

public void surfaceCreated(SurfaceHolder holder)
{   
    prepareRecorder(); 
}  

我认为上面的代码会在initRecorder()

之前调用

来自文档:setOutputFormat()

  

设置录制期间生成的输出文件的格式。在setAudioSource()/ setVideoSource()之后但在prepare()之前调用它。