Android相机预览回调和MediaRecorder录制视频

qkf9rpyu  于 2023-05-12  发布在  Android
关注(0)|答案(4)|浏览(244)

我想创建一个应用程序,必须录制视频(使用媒体录像机)和录制的视频需要格式(使用相机)。
我创建了如下所示的示例代码,但是当从菜单中按下开始录制按钮时会显示错误。这显示了强制关闭错误。但是预览回调没有错误。我的代码如下

package buffer.video;
    import android.app.Activity;
    import android.os.Bundle;
    import java.io.IOException;  
    import android.app.Activity;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.Toast;

    public class VofoVideoToBufferActivity extends Activity implements SurfaceHolder.Callback,
    Camera.AutoFocusCallback {

    private SurfaceView preview;
    private SurfaceHolder previewHolder;

    private MediaRecorder mRecorder;
    private Camera mCamera;
    private boolean mPreviewRunning = false;
    private boolean mCaptureFrame = false;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("", "Begin onCreate");
    setContentView(R.layout.main);

    preview = (SurfaceView) findViewById(R.id.surfaceView1);
    previewHolder = preview.getHolder();
    previewHolder.addCallback(this);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    public void onResume() {
    super.onResume();
    }

    public void onPause() {
    super.onPause();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
    }

    public void startRecording() {
    Log.e("", "Begin StartRecording");
    mCaptureFrame = true;

    if(mRecorder!=null)
    {
        mRecorder.stop();
        mRecorder.release();
    }
    mRecorder = new MediaRecorder();
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setVideoSize(176, 144);
    mRecorder.setVideoFrameRate(15);
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    mRecorder.setMaxDuration(30000);

    mRecorder.setPreviewDisplay(previewHolder.getSurface());
    mRecorder.setOutputFile("/sdcard/videotest2.3gp");
    try {
        mRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mRecorder.start();
    }

    public void stopRecording() {
    Log.e("", "Begin StopChange");
    mRecorder.stop();
    }

    public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.startRecording:
       startRecording();

        return true;
    case R.id.stopRecording:
        stopRecording();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
    }

    public void surfaceCreated(SurfaceHolder holder) {
    Log.e("", "Begin surfaceDestroy");
    mCamera = Camera.open();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();
    mPreviewRunning = false;
    mCamera.release();

    mRecorder.reset();
    mRecorder.release();
    }

    public void onAutoFocus(boolean success, Camera camera) {

    }

    PreviewCallback previewCallback = new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {
        Log.e("", "onPreviewFrame pass");
        if (mCaptureFrame) {
        Toast.makeText(getParent(),"halooo",Toast.LENGTH_LONG).show();

        }
    }
    };

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    Log.e("", "Begin SurfaceChange");


    if (mPreviewRunning)
        mCamera.stopPreview();

    Camera.Parameters p = mCamera.getParameters();
     p.setPreviewSize(170,240);

    mCamera.setParameters(p);

    try {
        mCamera.setPreviewDisplay(holder);
    } catch (IOException e) {
        e.printStackTrace();
    }

    mCamera.setPreviewCallback(previewCallback);

    mCamera.startPreview();
    mPreviewRunning = true;

    }

    }

清单文件显示如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="vofo.streaming"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Logcat视图如下所示。仅复制了Logcat的最后一部分。

07-19 12:42:42.229: DEBUG/dalvikvm(8599): GC_FOR_MALLOC freed 7 objects / 408168 bytes in 236ms
07-19 12:42:42.259: ERROR/(8599): onPreviewFrame pass
07-19 12:42:42.339: ERROR/(8599): onPreviewFrame pass
07-19 12:42:42.469: ERROR/(8599): onPreviewFrame pass
07-19 12:42:42.589: ERROR/(8599): onPreviewFrame pass
07-19 12:42:42.709: ERROR/(8599): onPreviewFrame pass
07-19 12:42:43.089: DEBUG/dalvikvm(8599): GC_FOR_MALLOC freed 7 objects / 408168 bytes in 258ms
07-19 12:42:43.099: ERROR/(8599): onPreviewFrame pass
07-19 12:42:43.169: ERROR/(8599): onPreviewFrame pass
07-19 12:42:43.289: ERROR/(8599): onPreviewFrame pass
07-19 12:42:43.419: ERROR/(8599): onPreviewFrame pass
07-19 12:42:43.439: INFO/ActivityManager(67): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=vofo.streaming/.MainActivity }
07-19 12:42:43.629: DEBUG/AndroidRuntime(8802): Shutting down VM
07-19 12:42:43.679: DEBUG/dalvikvm(8802): Debugger has detached; object registry had 1 entries
07-19 12:42:43.809: INFO/AndroidRuntime(8802): NOTE: attach of thread 'Binder Thread #3' failed
07-19 12:42:43.841: INFO/ActivityManager(67): Start proc vofo.streaming for activity vofo.streaming/.MainActivity: pid=8811 uid=10036 gids={1015, 1006, 3003}
07-19 12:42:43.850: ERROR/(8599): onPreviewFrame pass
07-19 12:42:44.479: DEBUG/dalvikvm(8599): GC_FOR_MALLOC freed 15 objects / 408680 bytes in 566ms
07-19 12:42:44.499: ERROR/(8599): onPreviewFrame pass
07-19 12:42:44.589: ERROR/(8599): onPreviewFrame pass
07-19 12:42:44.719: ERROR/(8599): onPreviewFrame pass
07-19 12:42:44.859: ERROR/(8599): onPreviewFrame pass
07-19 12:42:45.859: VERBOSE/RecordVideo(8811): Width x Height = 176x144
07-19 12:42:45.949: INFO/ActivityManager(67): Displayed activity vofo.streaming/.MainActivity: 2317 ms (total 2317 ms)
07-19 12:42:46.599: DEBUG/AndroidRuntime(8599): Shutting down VM
07-19 12:42:46.599: WARN/dalvikvm(8599): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599): FATAL EXCEPTION: main
07-19 12:42:46.669: ERROR/AndroidRuntime(8599): java.lang.NullPointerException
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at buffer.video.VofoVideoToBufferActivity.surfaceDestroyed(VofoVideoToBufferActivity.java:128)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:568)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.SurfaceView.updateWindow(SurfaceView.java:472)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.os.Looper.loop(Looper.java:123)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at java.lang.reflect.Method.invoke(Method.java:521)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-19 12:42:46.669: ERROR/AndroidRuntime(8599):     at dalvik.system.NativeStart.main(Native Method)
07-19 12:42:48.049: DEBUG/dalvikvm(181): GC_EXPLICIT freed 191 objects / 13008 bytes in 8398ms
07-19 12:42:52.690: INFO/Process(8599): Sending signal. PID: 8599 SIG: 9
07-19 12:42:52.750: INFO/ActivityManager(67): Process buffer.video (pid 8599) has died.
07-19 12:42:52.759: INFO/WindowManager(67): WIN DEATH: Window{4509e720 SurfaceView paused=false}
07-19 12:42:52.790: INFO/WindowManager(67): WIN DEATH: Window{4507e048 buffer.video/buffer.video.VofoVideoToBufferActivity paused=false}
07-19 12:42:52.880: WARN/InputManagerService(67): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45061a50
07-19 12:42:56.730: DEBUG/dalvikvm(262): GC_EXPLICIT freed 31 objects / 1576 bytes in 169ms
07-19 12:43:00.439: ERROR/CameraInput(34): Unsupported parameter(x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value)
07-19 12:43:00.439: ERROR/CameraInput(34): VerifiyAndSetParameter failed on parameter #0
07-19 12:43:00.460: ERROR/audio_input(34): unsupported parameter: x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value
07-19 12:43:00.469: ERROR/audio_input(34): VerifyAndSetParameter failed
07-19 12:43:00.539: WARN/AuthorDriver(34): Video encoding bit rate is set to 192000 bps
07-19 12:43:00.589: INFO/MediaRecorderJNI(8811): prepare: surface=0x20fc70 (id=1)
07-19 12:43:00.649: ERROR/PVOMXEncNode(34): PVMFOMXEncNode-Audio_AMRNB::DoPrepare(): Got Component OMX.PV.amrencnb handle 
07-19 12:43:00.699: DEBUG/CameraHardwareStub(34): initHeapLocked: preview size=320x240
07-19 12:43:00.759: DEBUG/CameraInput(34): Intended mFrameWidth=176, mFrameHeight=144 
07-19 12:43:00.759: DEBUG/CameraHardwareStub(34): initHeapLocked: preview size=176x144
07-19 12:43:00.779: DEBUG/CameraInput(34): Actual mFrameWidth=176, mFrameHeight=144 
07-19 12:43:00.799: ERROR/AuthorDriver(34): Command 13 completed with error -17
07-19 12:43:00.799: ERROR/MediaRecorder(8811): prepare failed: -17
07-19 12:43:00.799: ERROR/RecordVideo(8811): java.io.IOException: prepare failed.
07-19 12:43:00.799: WARN/System.err(8811): java.io.IOException: prepare failed.
07-19 12:43:00.850: WARN/System.err(8811):     at android.media.MediaRecorder._prepare(Native Method)
07-19 12:43:00.859: WARN/System.err(8811):     at android.media.MediaRecorder.prepare(MediaRecorder.java:503)
07-19 12:43:00.869: WARN/System.err(8811):     at vofo.streaming.MainActivity.beginRecording(MainActivity.java:191)
07-19 12:43:00.869: WARN/System.err(8811):     at vofo.streaming.MainActivity.access$0(MainActivity.java:162)
07-19 12:43:00.899: WARN/System.err(8811):     at vofo.streaming.MainActivity$1.onClick(MainActivity.java:74)
07-19 12:43:00.899: WARN/System.err(8811):     at android.view.View.performClick(View.java:2408)
07-19 12:43:00.899: WARN/System.err(8811):     at android.view.View$PerformClick.run(View.java:8816)
07-19 12:43:00.899: WARN/System.err(8811):     at android.os.Handler.handleCallback(Handler.java:587)
07-19 12:43:00.919: WARN/System.err(8811):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 12:43:00.919: WARN/System.err(8811):     at android.os.Looper.loop(Looper.java:123)
07-19 12:43:00.959: WARN/System.err(8811):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-19 12:43:00.959: WARN/System.err(8811):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 12:43:00.969: WARN/System.err(8811):     at java.lang.reflect.Method.invoke(Method.java:521)
07-19 12:43:00.969: WARN/System.err(8811):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-19 12:43:00.969: WARN/System.err(8811):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-19 12:43:00.989: WARN/System.err(8811):     at dalvik.system.NativeStart.main(Native Method)

请帮帮我

6ojccjat

6ojccjat1#

mCameramRecorder在方法onSurfaceDestroyed中为null。检查是否为null,并仅在它不为null时才对这些对象执行操作。

q35jwt9p

q35jwt9p2#

删除以下行:

mRecorder.setVideoSize(176, 144);
bvjxkvbb

bvjxkvbb3#

你应该调用mRecorder.setPreviewDisplay(previewHolder.getSurface());在创建曲面之后。public void surfaceCreated(SurfaceHolder holder){ Log.e(“",“开始surfaceDestroy”); mCamera = Camera.open(); mRecorder.setPreviewDisplay(previewHolder.getSurface());}

rmbxnbpk

rmbxnbpk4#

解决方案a:你可以将方法startrecording()改为:

private void startrecording(){

    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();

    }
    mMediaRecorder.start();
}

相关问题