无法让MediaRecorder正常工作

时间:2011-06-30 09:14:49

标签: android video camera

我正在尝试编写一个简单的录像机,有时它可以正常工作,有时也不会。所以我把它全部剥离并重新开始,但这次几乎相同的代码我的HTC Desire HD和三星Galaxy手机都抛出了一些奇怪的错误。继承人的代码

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;

}

}

当我点击记录时,它会轻微地工作,然后崩溃。好吧不会崩溃。基本上屏幕暂停,不再预览,然后把它扔掉。

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.

真的不了解这个MediaRecorder。在我看来非常敏感。任何人都可以帮忙吗?拜托,我要疯了。

0 个答案:

没有答案
相关问题