MediaRecorder启动失败-19,启动失败,尝试删除损坏的文件

时间:2014-07-13 18:27:05

标签: android android-mediarecorder

我在我的应用程序中有一个表面视图,工作正常,但在屏幕上有视频预览,我也想同时录制视频。要做到这一点,我有:

声明变量等:

/************************ GLOBAL VARIABLES FOR VIDEO PREVIEW ********************/

/* For the surface view */
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;

/* For saving the video to the device */
MediaRecorder mediaRecorder;
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

/* For both */
Button buttonStartCameraPreview, buttonStopCameraPreview;
Camera camera;
boolean previewing = false;

然后在onCreate方法()中启动录像机,设置开始/停止按钮,设置表面视图,然后编码以按按钮按下激活表面视图/视频捕获:

// Set up the media recorder
initMediaRecorder();

buttonStartCameraPreview = (Button) findViewById(R.id.startCameraPreview);
buttonStopCameraPreview = (Button) findViewById(R.id.stopCameraPreview);

surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
// Set the surfaceView so it appears on top, not behind the view
surfaceView.setZOrderOnTop(true);
// Set the surfaceView background colour to magenta
surfaceView.setBackgroundColor(Color.MAGENTA);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);

buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        if(!previewing)
        {
            camera = Camera.open();
            if (camera != null)
            {
                try 
                {
                    camera.setPreviewDisplay(surfaceHolder);
                    camera.startPreview();
                    mediaRecorder.start();
                    previewing = true;
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    }
});
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        if(camera != null && previewing)
        {
            camera.stopPreview();
            camera.release();
            mediaRecorder.stop();
            mediaRecorder.release();
            finish();
            camera = null;
            previewing = false;
        }
    }
});

其他相关方法是:

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

/************************ SETTING UP MEDIARECORDER METHODS ************************/

public void initMediaRecorder()
{
    mediaRecorder = new MediaRecorder();

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // DEFAULT
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //DEFAULT
    //CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    //mediaRecorder.setProfile(camcorderProfile_HQ);
    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    //mediaRecorder.setOutputFile("/sdcard/" + System.nanoTime() + "_video.mp4");
    mediaRecorder.setOutputFile(getOutputMediaFile().toString());
    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}

public void prepareMediaRecorder()
{
     mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
     try 
     {
         mediaRecorder.prepare();
     } 
     catch (IllegalStateException e) 
     {
         e.printStackTrace();
     } 
     catch (IOException e) 
     {
         e.printStackTrace();
     }
}

public static File getOutputMediaFile()
{
    // External sd card location
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) 
    {
        if (!mediaStorageDir.mkdirs()) 
        {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }
    File mediaFile;
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "VID_" + timeStamp + ".mp4");
    return mediaFile;
}

应用程序加载正常,但是当我按下按钮开始表面视图/录制“app已停止消息”时(表面视图仍然在后台运行)。

我认为问题与创建/覆盖目标文件有关(根据LogCat)以保存输出,但我不能把手指放在上面。 CatLog如下:

07-13 19:12:14.608: E/MediaRecorder(25372): start failed: -19
07-13 19:12:14.608: E/MediaRecorder(25372): start failed.
07-13 19:12:14.608: E/MediaRecorder(25372): try to delete broken file:    /storage/emulated/0/Pictures/Hello Camera/VID_20140713_191211.mp4

我也有清单文件:

<!-- Accessing camera hardware -->
<uses-feature android:name="android.hardware.camera"
              android:required="true" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

感谢。

0 个答案:

没有答案