2013-04-21 118 views
6

有人可以指出我正在做的錯誤嗎?我試圖錄制一段短片,但不知何故,它發現了一種崩潰的方式。Android視頻記錄錯誤

源代碼:

public class Start_recording extends Activity implements SurfaceHolder.Callback 
{ 
    MediaRecorder recorder; 
    SurfaceHolder surfaceHolder; 
    SurfaceView myVideoView; 
    Camera camera; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
     camera = Camera.open(); 
     camera.unlock(); 
     recorder = new MediaRecorder(); 
     setContentView(R.layout.activity_main_rst); 
     myVideoView = (SurfaceView)findViewById(R.id.surface_camera); 
     surfaceHolder = myVideoView.getHolder(); 
     surfaceHolder.addCallback(this); 
     initMediaRecorder(); 
     boolean exists = (new File(android.os.Environment.getExternalStorageDirectory() + "/Record/")).exists(); 

     if (!exists) 
     { 
      new File(android.os.Environment.getExternalStorageDirectory() + "/Record/").mkdirs(); 
     } 

     try 
     { 
      recorder.prepare(); // This is the line of error. 
      recorder.start(); 
      Thread.sleep(36000); 
      recorder.stop(); 
      setupActionBar(); 
     } 
     catch(Exception e) 
     { 
      Log.v("Error is there : ",e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public void initMediaRecorder() 
    { 
     try 
     { 


      recorder.setPreviewDisplay(surfaceHolder.getSurface()); 
      recorder.setCamera(camera); 
      recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
      recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
      recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
      recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp"); 
      recorder.setMaxDuration(300000); 
     } 
     catch(Exception f) 
     { 
      Log.v("Exception here : ",f.toString()); 
      f.printStackTrace(); 
     } 
    } 
} 

錯誤:

E/MediaRecorderJNI(1575): Application lost the surface 
V/Error is there :(1575): java.io.IOException: invalid preview surface 
W/System.err(1575): java.io.IOException: invalid preview surface 
W/System.err(1575):  at android.media.MediaRecorder._prepare(Native Method) 
W/System.err(1575):  at android.media.MediaRecorder.prepare(MediaRecorder.java:666) 
W/System.err(1575):  at com.example.project.Start_recording.onCreate(Start_recording.java:51) 

我已經在清單文件正確添加的權限,並設置setPreviewDisplay()。但是我的預覽表面無效,請糾正我。

回答

0

必須使用SurfaceHolder.Callback才能在真正創建表面之後纔開始記錄。