1

我是先进技术的新手,例如 android 中的视频录制。我正在尝试创建一个简单的应用程序来录制视频。

这是以下代码,我用来录制视频。

    private PrintWriter out = null;
    private final MediaRecorder mediaRecorder = new MediaRecorder();
    private final Camera camera = getCameraInstance();
    private CameraPreview surfaceView = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = new CameraPreview(this, camera);
        FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
        frameLayout.addView(surfaceView);

        Parameters params = camera.getParameters();
        List<Size> sizes = params.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, 200, 200);
        params.setPreviewSize(optimalSize.width, optimalSize.height);

        mediaRecorder.setCamera(camera);
        mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);
        mediaRecorder.setOutputFile("/sdcard/test.mpg");
    }

我在按钮的 onClick() 方法中调用 start() 方法

public void startRecording(View view){
        try {
            mediaRecorder.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            releaseMediaRecorder();
        } catch(Exception e){
            e.printStackTrace();
            releaseMediaRecorder();
        }
    }

这是我的 SurfaceView 代码

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private final Camera mCamera;
    private final Context context;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        this.context = context;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(this.getClass().getName(), "Error setting camera preview: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if(context instanceof MainActivity){
            ((MainActivity)context).stopMediaPlayer();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        if (mHolder.getSurface() == null){
          return;
        }
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
            if(context instanceof MainActivity){
                ((MainActivity)context).prepareMediaPlayer();
            }
        } catch (Exception e){
            Log.d(this.getClass().getName(), "Error starting camera preview: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

我收到以下错误

08-01 16:17:39.121: I/MediaRecorderJNI(29423): prepare: surface=0x202790 (identity=1681)
08-01 16:17:44.911: E/MediaRecorder(29423): start failed: -16
08-01 16:17:44.911: W/System.err(29423): java.lang.RuntimeException: start failed.
08-01 16:17:44.911: W/System.err(29423):    at android.media.MediaRecorder.start(Native Method)
08-01 16:17:44.911: W/System.err(29423):    at com.sample.recorder.MainActivity.startRecording(MainActivity.java:149)
08-01 16:17:44.911: W/System.err(29423):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:17:44.911: W/System.err(29423):    at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:17:44.911: W/System.err(29423):    at android.view.View$1.onClick(View.java:2163)
08-01 16:17:44.911: W/System.err(29423):    at android.view.View.performClick(View.java:2552)
08-01 16:17:44.911: W/System.err(29423):    at android.view.View$PerformClick.run(View.java:9229)
08-01 16:17:44.911: W/System.err(29423):    at android.os.Handler.handleCallback(Handler.java:587)
08-01 16:17:44.911: W/System.err(29423):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 16:17:44.911: W/System.err(29423):    at android.os.Looper.loop(Looper.java:130)
08-01 16:17:44.911: W/System.err(29423):    at android.app.ActivityThread.main(ActivityThread.java:3701)
08-01 16:17:44.911: W/System.err(29423):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:17:44.911: W/System.err(29423):    at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:17:44.911: W/System.err(29423):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
08-01 16:17:44.911: W/System.err(29423):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
08-01 16:17:44.911: W/System.err(29423):    at dalvik.system.NativeStart.main(Native Method)
08-01 16:17:44.921: I/com.sample.recorder.MainActivity(29423): Media Recorder released successfully.

在该错误代码中,第一行在加载活动时打印,其余行在点击内部调用 startRecording() 函数的开始按钮时产生

AndroidManifestFile 中使用的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.STORAGE" />
4

2 回答 2

3

好的,所以我设法通过删除来解决这个错误

mediaRecorder.setVideoSize(width, height)

在 android 版本低于 3.0 (HoneyComb) 的设备上

这是我的代码:

        mMediaRecorder.setCamera(mCamera);

        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

        } else {

            if (! isFrontCamera) {
                CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
                mMediaRecorder.setProfile(camcorderProfile);

            } else {
                CamcorderProfile camcorderProfile = CamcorderProfile.get(mFrontCamId, CamcorderProfile.QUALITY_LOW);
                mMediaRecorder.setProfile(camcorderProfile);
            }

            mMediaRecorder.setVideoSize(720, 480);
        }

        mMediaRecorder.setOutputFile(filePath);
        mMediaRecorder.setMaxDuration(60000);
        mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            releaseMediaRecorder();

        } catch (IOException e) {
            releaseMediaRecorder();

        }

在此更改之后,它适用于我拥有的所有测试设备。

希望这对您也有帮助。

于 2013-08-30T12:51:32.903 回答
0

尝试改变它:

private PrintWriter out = null;
private final MediaRecorder mediaRecorder;
private final Camera camera;
private CameraPreview surfaceView = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mediaRecorder = new MediaRecorder();
    camera = getCameraInstance();}
于 2013-08-01T10:57:26.747 回答