微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用android MediaRecorder

以下是我录制视频和音频的工作代码的结构:

问题:
1)为什么需要CamcorderProfile? setProfile(…)似乎将尺寸设置为任何QUALITY_HIGH给出的尺寸,但后来我使用setVideoSize(…)设置了我想要的尺寸,它覆盖了这一点.但是,当我删除两个CamcorderProfile行时,应用程序在使用LogCat E / MediaRecorder(19526)的setVideoSize(…)中崩溃:setVideoSize被调用为无效状态:2.

2)如何不录音?文档说明如果没有调用setAudioSource(…),则不会有音轨.但是,当我删除该行时,应用程序在使用LogCat E / MediaRecorder(19946)的setProfile(…)中崩溃:尝试设置音频编码器,而不必首先设置音频源.

3)如果我删除了CamcorderProfile行和setAudioSource(…)行,它会像1)中一样崩溃.

4)我也试过添加

recorder.setoutputFormat(OutputFormat.DEFAULT);

而不是CamcorderProfile行.但是现在它在劫持()中崩溃了.如果setAudioSource(…)被称为LogCat,则为:E / MediaRecorder(20737):音频源设置,但如果未调用LogCat,音频编码器未设置为:E / MediaRecorder(20544):视频源设置,但视频编码器未设置

我已经看到互联网,我找不到一个正确的方式设置MediaRecorder的一个很好的例子. Here它意味着在API 8之后你应该使用CamcorderProfile类,但在我看来,它是造成问题.

任何帮助将是伟大的!谢谢!

代码(在下面运行时工作):

recorder = new MediaRecorder();
recorder.setCamera(<<camera>>);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);

recorder.setoutputFile(<<Path String>>);
recorder.setVideoSize(<<Width>>,<<Height>>);

recorder.setPreviewdisplay(<<Surface>>);

recorder.setorientationHint(0); 
recorder.setMaxDuration(10000);
recorder.setonInfoListener(this);

try
{
    recorder.prepare();
    recorder.start();
} catch ...

解决方法

我没有很多MediaRecorder的经验,但我正在阅读一些相关的主题,我会尽量回答你的问题:

1,3和4)CamcorderProfile设置的不仅仅是分辨率,它还将输出格式和编码器(音频和视频)设置为一体.您会收到错误,因为您可能需要在调用setVideoSize之前使用setoutputFormat,如果不想使用CamcorderProfile,则必须先调用setVideoEncoder和setAudioEncoder. [根据这answer]

2)再次,CamcorderProfile还设置音频属性(如Codec,BitRate,SampleRate,…),因此您需要在调用音频源之前设置音频源,这就是应用程序崩溃的原因.如果您不想录制音频,请尝试下一个代码:(我没有测试它,所以我实际上不知道它是否工作,但我很确定它)

recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setoutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(WIDTH,HEIGHT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setoutputFile(PATH);
recorder.setPreviewdisplay(SURFACE);

recorder.prepare();
recorder.start();

还要注意,如果您不想使用CamcorderProfile(意思是要仅录制音频或视频),则可能需要设置其他参数,以确保您拥有所需的质量.看看下面的代码示例:

recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

// Following code does the same as getting a CamcorderProfile (but customizable)
recorder.setoutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// Video Settings 
recorder.setVideoSize(WIDTH,HEIGHT);
recorder.setVideoFrameRate(FRAME_RATE);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setVideoEncodingBitRate(VIDEO_BITRATE);
// Audio Settings
recorder.setAudioChannels(AUdio_CHANNELS);
recorder.setAudioSamplingRate(SAMPLE_RATE);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
recorder.setAudioEncodingBitRate(AUdio_BITRATE);

// Customizable Settings such as:
//   recorder.setoutputFile(PATH);
//   recorder.setPreviewdisplay(SURFACE);
//   etc...

// Prepare and use the MediaRecorder
recorder.prepare();
recorder.start();
...
recorder.stop();
recorder.reset();
recorder.release();

我希望这可以帮助你.

原文地址:https://www.jb51.cc/android/313706.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐