MediaRecorder start called in invalid state: 4

。_饼干妹妹 提交于 2021-02-05 09:38:49

问题


I've just started getting into android/Glass development and I was trying to add basic video recording functionality. Currently Glass limits you to 10 seconds unless you tap/press button again and I wanted to bypass this and just have an open ended video recorder.

I followed both the Camera API walk-through:

And referenced another project:

So far my code looks like this:

private void startRecording()
{
    try{
    camera = Camera.open();
    mediaRecorder = new MediaRecorder();
    surfaceView = new CamSurfaceView(this, camera);

    try {
        camera.setPreviewDisplay(surfaceView.getHolder());
    } catch (IOException e1) {

    }
    camera.startPreview();
    camera.unlock();
    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setProfile(CamcorderProfile.get(MediaRecorder.OutputFormat.MPEG_4));
    mediaRecorder.setOutputFile(getOutputMediaFile().toString());
    mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

    try{
    mediaRecorder.prepare();
    }
    catch(IllegalStateException e){

    }
    catch(IOException e){

    }
    mediaRecorder.start();  //Code failure occurs here.
    }
    catch(Exception e)
    {
        if(mediaRecorder != null)
            mediaRecorder.release();
        if(camera != null)
            camera.release();
    }
}
private Uri getOutputMediaFile()
{
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+File.separator+"DCIM/Camera");

    File[] files =mediaStorageDir.listFiles();

    if(!mediaStorageDir.exists())
    {
        if(!mediaStorageDir.mkdirs())
        {
            Log.d("KarmaCam","Failed to create directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File file = new File(mediaStorageDir.getPath() + File.separator+ "VID_"+timeStamp+".mp4");
    return Uri.fromFile(file);

}

My SurfaceView:

public class KarmaCamSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mHolder;
private Camera mCamera;

public CamSurfaceView(Context context, Camera camera) {
    super(context);
    mCamera = camera;
    mHolder = getHolder();
    mHolder.addCallback(this);      
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight, int height) {

    if(mHolder.getSurface() == null)
        return;     
    try
    {
        mCamera.stopPreview();  
    }
    catch(Exception e)  {}

    try
    {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    }
    catch(Exception e){}        
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try
    {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    }
    catch(Exception e){}

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

I've seen others with similar problems/questions and they are usually happening around improper output file name formats. As far as I can tell mine would be fine, but I'm curious if the directory path is valid. This is where the built in Glass videos get stored.


回答1:


Try add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to your AndroidManifest.xml




回答2:


Open AndroidManifest file and inside application tag add this

android:requestLegacyExternalStorage="true"



回答3:


Change your location to:

 Environment.getExternalStorageDirectory() + File.separator 
            + Environment.DIRECTORY_DCIM + File.separator + "FILE_NAME";

This worked for me. Hope it helps!




回答4:


I ran into the same issue on some Android devices. I was using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) as the output for the MediaRecorder instance. After reading is3av's answer, I changed the output path to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) and the problem is solved.

Seems like some phones don't allow files to be written to Environment.DIRECTORY_MOVIES path or that path doesn't exist.



来源:https://stackoverflow.com/questions/24236620/mediarecorder-start-called-in-invalid-state-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!