如何使用Android Media Player录制视频和音频

时间:2015-09-17 13:13:56

标签: android audio video xamarin mediarecorder

我想在我的Android应用中录制视频,包括音频。到目前为止,我已经能够使用本网站其他地方的代码录制视频,但我无法包含任何音频。我该怎么记录? 我正在使用的代码(Xamarin C#)设置媒体记录器如下。现在有点粗糙,准备就绪。

    private void InitRecorder(Surface surface)
    {
        // It is very important to unlock the camera before doing setCamera
        // or it will results in a black preview
        if (mCamera == null)
        {
            mCamera = Camera.Open();
            mCamera.Unlock();
        }

        if (mMediaRecorder == null)
            mMediaRecorder = new MediaRecorder();

        mMediaRecorder.SetPreviewDisplay(surface);
        mMediaRecorder.SetCamera(mCamera);

        mMediaRecorder.SetVideoSource(VideoSource.Default);
        mMediaRecorder.SetOutputFormat(OutputFormat.Mpeg4);
        mMediaRecorder.SetVideoEncoder(VideoEncoder.H264);
        mMediaRecorder.SetVideoEncodingBitRate(512 * 1000);
        mMediaRecorder.SetVideoFrameRate(30);
        mMediaRecorder.SetVideoSize(640, 480);
        mMediaRecorder.SetOutputFile(VIDEO_PATH_NAME);

        try
        {
            mMediaRecorder.Prepare();
        }
        catch (Exception ex)
        {
            // This is thrown if the previous calls are not called with the 
            // proper order

        }

        mInitSuccesful = true;
    }

权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera.autofocus" />

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要设置音频源。 将以下代码添加到InitRecorder()方法。

mMediaRecorder.SetAudioSource(AudioSource.Mic);
mMediaRecorder.SetAudioEncoder(AudioEncoder.AmrNb);