录像时调用MediaRecorder的start()时发生start failed: -19错误

时间:2022-09-15 20:53:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
     
private void start() {
        if (isRecording)
        {
            // 停止录像并释放myFrontFacingCamera
            myVideoRecorder.stop();
            releaseVideoRecorder();
            /*myFrontFacingCamera.lock();*/ // 从Android4.0开始,这个方法由系统自动调用
             
            // TODO:通知用户录像已停止,设置停止按钮为不可用
             
            isRecording = false;
        } else {
            // 初始化myFrontFacingCamera
            if (prepareVideoRecorder()) {
                // myFrontFacingCamera已可用并解锁,VideoRecorder已就绪
                // 现在可以开始录像
                /*myFrontFacingCamera.startPreview();*/
                myVideoRecorder.start();
                 
                // TODO:通知用户录像已开始,设置开始按钮为不可用
                 
                isRecording = true;
            } else {
                // 准备未能完成,释放myFrontFacingCamera
                releaseVideoRecorder();
                // TODO:通知用户
            }
        }
    }
 
private boolean prepareVideoRecorder()
    {
        /*myFrontFacingCamera = getFrontFacingCamera();*/ // 在onCreate()中已经创建了myFrontFacingCamera对象
        myVideoRecorder = new MediaRecorder();
         
        // 解锁并将摄像头指向myVideoRecorder
        /*myFrontFacingCamera.unlock();*/ //在Android4.0之后这个方法由系统自动调用
        myVideoRecorder.setCamera(myFrontFacingCamera);
         
        // TODO:指定源,如果声音源不行,就用麦克作为声音源
        myVideoRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        myVideoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
         
        // 指定CamcorderProfile(需要API Level 8以上版本),这里的Profile包括了输出格式,声音编码类型和视频编码类型
        myVideoRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
         
        myVideoRecorder.setVideoSize(100, 100);
         
        // 指定输出文件
        myVideoRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
         
        // 指定预览输出
        myVideoRecorder.setPreviewDisplay(myVideoPreview.getHolder().getSurface());
         
        // 根据以上配置准备myVideoRecorder
        try {
            myVideoRecorder.prepare();
        catch (IllegalStateException e) {
            Log.d("VideoActivity:prepareVideoRecorder""准备myVideoRecorder时发生IllegalStateException错误" "\n" + e.getMessage());
            releaseVideoRecorder();
            return false;
        catch (IOException e) {
            Log.d("VideoActivity:prepareVideoRecorder""准备myVideoRecorder时发生IOException错误" "\n" + e.getMessage());
            releaseVideoRecorder();
            return false;
        }
         
        return true;
         
         
         
    }

解决方案:

  myVideoRecorder.setVideoSize(100, 100); 这句话注释掉就行了