MediaRecorder以无效状态调用:4

时间:2014-06-16 04:20:57

标签: android video google-glass

我刚开始进入android / Glass开发,我试图添加基本的视频录制功能。目前,Glass除非你再次点击/按下按钮,否则你将限制为10秒,我想绕过这个并且只有一个开放式视频录像机。

我跟着相机API走过:

引用了另一个project

到目前为止,我的代码看起来像这样:

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);

}

我的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

}

我见过其他有类似问题/问题的人,他们经常发生不正确的输出文件名格式。据我所知,我会很好,但我很好奇目录路径是否有效。这是内置Glass视频的存储位置。

4 个答案:

答案 0 :(得分:2)

尝试将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到AndroidManifest.xml

答案 1 :(得分:2)

打开AndroidManifest文件,并在应用程序标记中添加此

android:requestLegacyExternalStorage="true"

答案 2 :(得分:0)

将您的位置更改为:

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

这对我有用。希望它有所帮助!

答案 3 :(得分:0)

我在某些Android设备上遇到了同样的问题。我正在使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) 作为MediaRecorder实例的输出。读完is3av的答案后,我将输出路径更改为Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),问题解决了。

似乎有些手机不允许将文件写入Environment.DIRECTORY_MOVIES路径或该路径不存在。