Trouble trying to get MediaRecorder to work correc

2019-08-14 04:55发布

问题:

I'm trying to write a simple video recorder and sometimes it works and sometimes it doesn't. So I stripped it all out and started again, but this time almost the same code my HTC Desire HD and a Samsung Galaxy phone both throw some weird errors. Heres the code

public class VideoRecord extends Activity implements SurfaceHolder.Callback {

// Camera objects
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private boolean previewRunning;

// Objects for recording
private MediaRecorder mediaRecorder;

// App state
private boolean recordingInMotion;

// Video files
private File folder;

// Message queue
private Handler handler;

// Android preferences
private SharedPreferences prefs;

private Resources res;
private TextView statusIndicator;

private Button btnRecord;

private boolean recording = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    res = getResources();

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    setContentView(R.layout.activity_videorecord);
    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);

    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    handler = new Handler();
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    getElements();
    setRecordButton();

}

private void getElements() {

    btnRecord = (Button) findViewById(R.id.button_record);

}

private void setRecordButton() {

    btnRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (recording == false) {
                startRecording(); 
            } else {
                stopRecording();                    
            }
        }
    });

}   

@Override
public void onResume() {

    super.onResume();

}

@Override
public void onPause() {

    super.onDestroy();

    if (mediaRecorder != null) {
        if (recordingInMotion) {
            stopRecording();
        }
        mediaRecorder.release();
    }

}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    // Check camera isnt null for some reason
    if (camera == null) {
        return;
    }

    if (previewRunning) {
        camera.stopPreview();
    }
    // Set parameters
    Camera.Parameters p = camera.getParameters();
    p.setPreviewSize(320, 240);
    p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

    try {
        camera.setParameters(p);
    } catch (RuntimeException e) {
        e.printStackTrace();
    }

    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        previewRunning = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera = Camera.open();
    } catch (RuntimeException e) {
        e.printStackTrace();
        camera = null;
    }

    if (camera != null) {
        Camera.Parameters params = camera.getParameters();
        camera.setParameters(params);
    } else {
        Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
        finish();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {

    previewRunning = false;
    if (camera != null) {
        camera.stopPreview();
        camera.release();
    } else {
        Log.e(TAG, "surfaceDestroyed: camera is null!");
    }

}

public boolean startRecording() {

    if (camera == null) {
        Log.e(TAG, "startRecording: camera is null!");
        return false;
    }

    try {
        camera.unlock();
    } catch (RuntimeException e) {
        e.printStackTrace();
        camera = null;
        Log.e(TAG, "startRecording: unlock failed!");
        return false;
    }

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFile("/sdcard/temp.mp4");

    CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mediaRecorder.setProfile(mProfile);
    mediaRecorder.setVideoSize(mProfile.videoFrameWidth, mProfile.videoFrameHeight);

    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

    try {

        mediaRecorder.prepare();
        mediaRecorder.start();

        return true;

    } catch (IllegalStateException e) {
        Log.e(TAG, "Illegal State Exception:" + e.getMessage());
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        Log.e(TAG, "IOException:" + e.getMessage());
        e.printStackTrace();
        return false;
    } catch (RuntimeException re) {
        Log.e(TAG, "RuntimeException:" + re.getMessage());
        re.printStackTrace();
        return false;
    }

}

public void stopRecording() {

    mediaRecorder.stop();
    camera.lock();
    recordingInMotion = false;

}

}

When I hit record it works for slight second and then crashes. Well doesn't crash. Basically the screen pauses and does no preview any more and chucks this out.

06-30 21:11:23.660: ERROR/StagefrightRecorder(12174): Failed to set frame rate to 27 fps. The actual frame rate is 15
06-30 21:11:23.820: ERROR/HTC Acoustic(12174): cannot found indicated effect mode
06-30 21:11:23.830: ERROR/HTC Acoustic(12174): cannot found indicated effect mode
06-30 21:11:23.920: ERROR/QualcommCameraHardware(12174): frames in busy Q = 0
06-30 21:11:23.920: ERROR/QualcommCameraHardware(12174): frames in busy Q = 0 after deq and add to freeQ
06-30 21:11:23.920: ERROR/OMXCodec(12174): Buffer count/size less than minimum required
06-30 21:11:23.920: ERROR/OMXCodec(12174): [ 06-30 21:11:23.920 12174:0x2fa2 F/OMXCodec ]
06-30 21:11:23.920: ERROR/OMXCodec(12174): frameworks/base/media/libstagefright/OMXCodec.cpp:1859 err != OK
06-30 21:11:24.850: ERROR/Camera(12118): Error 100
06-30 21:11:26.530: ERROR/AudioService(1321): Media server died.
06-30 21:11:30.400: ERROR/HTC Acoustic(12204): ioctl ACOUSTIC_UPDATE_ADIE failed -1
06-30 21:11:30.730: ERROR/HTC Acoustic(12204): failed to open A2051 CSV files /system/etc/TPA2051_CFG.csv.
06-30 21:11:30.730: ERROR/AudioHardwareMSM7X30(12204): set_tpa2051_parameters fail
06-30 21:11:30.760: ERROR/HTC Acoustic(12204): (Original_Phone_REC,/system/etc/soundimage/Sound_Dualmic.txt)
06-30 21:11:30.770: ERROR/HTC Acoustic(12204): (Original_Phone_SPK,/system/etc/soundimage/Sound_Dualmic.txt)
06-30 21:11:30.780: ERROR/HTC Acoustic(12204): (Original_Phone,/system/etc/soundimage/Sound_Dualmic.txt)
06-30 21:11:30.790: ERROR/HTC Acoustic(12204): (Original,/system/etc/soundimage/Sound_Original.txt)
06-30 21:11:30.790: ERROR/HTC Acoustic(12204): (Original_SPK,/system/etc/soundimage/Sound_Original_SPK.txt)
06-30 21:11:30.790: ERROR/HTC Acoustic(12204): (dolby_a_spk,/system/etc/soundimage/Sound_Dolby_A_SPK.txt)
06-30 21:11:30.810: ERROR/HTC Acoustic(12204): (dolby_a_hp,/system/etc/soundimage/Sound_Dolby_A_HP.txt)
06-30 21:11:30.830: ERROR/HTC Acoustic(12204): (dolby_v_spk,/system/etc/soundimage/Sound_Dolby_V_SPK.txt)
06-30 21:11:30.860: ERROR/HTC Acoustic(12204): (dolby_v_hp,/system/etc/soundimage/Sound_Dolby_V_HP.txt)
06-30 21:11:30.870: ERROR/HTC Acoustic(12204): (Srs_a_hp,/system/etc/soundimage/Sound_SRS_A_HP.txt)
06-30 21:11:30.890: ERROR/HTC Acoustic(12204): (Srs_a_spk,/system/etc/soundimage/Sound_SRS_A_SPK.txt)
06-30 21:11:30.900: ERROR/HTC Acoustic(12204): (Srs_v_hp,/system/etc/soundimage/Sound_SRS_V_HP.txt)
06-30 21:11:30.920: ERROR/HTC Acoustic(12204): (Srs_v_spk,/system/etc/soundimage/Sound_SRS_V_SPK.txt)
06-30 21:11:30.930: ERROR/HTC Acoustic(12204): (Bass Booster,/system/etc/soundimage/Sound_Bass_Booster.txt)
06-30 21:11:30.930: ERROR/HTC Acoustic(12204): (Blues,/system/etc/soundimage/Sound_Blues.txt)
06-30 21:11:30.940: ERROR/HTC Acoustic(12204): (Classical,/system/etc/soundimage/Sound_Classical.txt)
06-30 21:11:30.950: ERROR/HTC Acoustic(12204): (Country,/system/etc/soundimage/Sound_Country.txt)
06-30 21:11:30.950: ERROR/HTC Acoustic(12204): (Jazz,/system/etc/soundimage/Sound_Jazz.txt)
06-30 21:11:30.960: ERROR/HTC Acoustic(12204): (Latin,/system/etc/soundimage/Sound_Latin.txt)
06-30 21:11:30.970: ERROR/HTC Acoustic(12204): (New age,/system/etc/soundimage/Sound_New_Age.txt)
06-30 21:11:30.970: ERROR/HTC Acoustic(12204): (Piano,/system/etc/soundimage/Sound_Piano.txt)
06-30 21:11:30.980: ERROR/HTC Acoustic(12204): (Pop,/system/etc/soundimage/Sound_Pop.txt)
06-30 21:11:30.990: ERROR/HTC Acoustic(12204): (R&B,/system/etc/soundimage/Sound_R_B.txt)
06-30 21:11:30.990: ERROR/HTC Acoustic(12204): (Rock,/system/etc/soundimage/Sound_Rock.txt)
06-30 21:11:31.000: ERROR/HTC Acoustic(12204): (Treble Booster,/system/etc/soundimage/Sound_Treble_Booster.txt)
06-30 21:11:31.010: ERROR/HTC Acoustic(12204): (Vocal Booster,/system/etc/soundimage/Sound_Vocal_Booster.txt)
06-30 21:11:31.020: ERROR/AudioPolicyManagerBase(12204): mSupportBacMic = 0
06-30 21:11:31.020: ERROR/TDAS Acoustic(12204): Fail to open /data/dolby/DM2_TDAS_CONFIG.csv
06-30 21:11:32.039: ERROR/AudioService(1321): Media server started.

Really not understanding this MediaRecorder. Seems to me to very touchy. Can any one help? Please i'm going nuts here.