采用Android MediaRecorder [英] Using android MediaRecorder

查看:1244
本文介绍了采用Android MediaRecorder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的工作code结构,录制视频和音频:

问题: 1)为什么 CamcorderProfile 需要的? setProfile(...)显示设置方面的任何QUALITY_HIGH给人,但后来我把我想要的尺寸与 setVideoSize(...) ,这将覆盖这一点。然而,当我卸下两个CamcorderProfile线,应用程序崩溃的 setVideoSize(...)与LogCat中 E / MediaRecorder(19526):setVideoSize称为处于无效状态:2

2)我怎么没录音?该文件指出,如果 setAudioSource(...)不叫,不会有任何音轨。然而,当我删除该行应用程序崩溃的 setProfile(...)与LogCat中 E / MediaRecorder(19946):尝试设置音频EN codeR未首先设置音频源

3)如果我删除这两个CamcorderProfile线和 setAudioSource(...)行,它崩溃为1)。

4)我也尝试添加该行

  recorder.setOutputFormat(OutputFormat.DEFAULT);
 

而不是CamcorderProfile线。但现在它崩溃的 perpare()。如果 setAudioSource(...)被称为LogCat中是: E / MediaRecorder(20737):音频信号源设置,但音频连接codeR未设置,如果它不叫LogCat中是: E / MediaRecorder(20544):视频源设置,但视频连接codeR未设置

我已经看过印花布互联网,我无法找到正确的方式来设置MediaRecorder一个很好的例子。 这里的这意味着以后的API 8,你应该使用CamcorderProfile类,但在我看来,这是造成问题。

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

code(当它运行它的工作原理是在下面):

 记录=新MediaRecorder();
recorder.setCamera(LT;<相机>>);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile轮廓= CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(资料);

recorder.setOutputFile(小于&其中;路径字符串>&GT);
recorder.setVideoSize(小于&其中;宽度>>中所述&;&其中;身高>&GT);

recorder.set previewDisplay(LT;<表面>>);

recorder.setOrientationHint(0);
recorder.setMaxDuration(10000);
recorder.setOnInfoListener(本);

尝试
{
    。录音机prepare();
    recorder.start();
} 抓住 ...
 

解决方案

我没有很多与MediaRecorder经验,但我读了一些相关的话题,我会尽量回答您的问题:

1,3和4) CamcorderProfile设置不仅仅是分辨率,还设置事物的输出格式和连接codeRS(用于音频和视频)。你得到的错误,因为你可能需要使用 setOutputFormat 之前调用 setVideoSize ,你得叫 setVideoEn codeR setAudioEn codeR 后,如果您不想使用CamcorderProfile。 [根据本回答]

2)此外,CamcorderProfile还设置音频属性(如codeC,比特率,采样率,...),所以你需要调用它之前设置的音频源,这就是为什么应用程序崩溃。如果您不想录制音频尝试下code:(我没有测试它,所以我真的不知道,如果它的工作原理,但我pretty的肯定它)

  recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(宽度,高度);
recorder.setVideoEn codeR(MediaRecorder.VideoEn coder.DEFAULT);
recorder.setOutputFile(PATH);
recorder.set previewDisplay(面);

。录音机prepare();
recorder.start();
 

另外要注意,如果你不想使用CamcorderProfile(这意味着你要录制的音频或视频)您可能需要设置其他参数,以确保你有你想要的品质。看看下面的例子code:

 记录=新MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

//关注code不一样的得到一个CamcorderProfile(但可定制)
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//视频设置
recorder.setVideoSize(宽度,高度);
recorder.setVideoFrameRate(帧频);
recorder.setVideoEn codeR(MediaRecorder.VideoEn coder.DEFAULT);
recorder.setVideoEncodingBitRate(VIDEO_BITRATE);
//音频设置
recorder.setAudioChannels(AUDIO_CHANNELS);
recorder.setAudioSamplingRate(SAMPLE_RATE);
recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.DEFAULT);
recorder.setAudioEncodingBitRate(AUDIO_BITRATE);

//自定义设置,如:
// recorder.setOutputFile(PATH);
// recorder.set previewDisplay(面);
//   等等...

// prepare并使用MediaRecorder
。录音机prepare();
recorder.start();
...
recorder.stop();
recorder.reset();
recorder.release();
 

我希望这可以帮助你。

Below is the structure of my working code to record video and audio:

Questions: 1) Why is the CamcorderProfile needed? setProfile(...) appears to set the dimensions to whatever QUALITY_HIGH gives, but later I set the dimensions I want with setVideoSize(...), which overrides this. However, when I remove the two CamcorderProfile lines, the app crashes at setVideoSize(...) with LogCat E/MediaRecorder(19526): setVideoSize called in an invalid state: 2.

2) How do I not record audio? The documentation states that if setAudioSource(...) is not called, there will be no audio track. However, when I remove that line the app crashes at setProfile(...) with LogCat E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first.

3) If I remove both CamcorderProfile lines and the setAudioSource(...) line, it crashes as in 1).

4) I have also tried adding the line

recorder.setOutputFormat(OutputFormat.DEFAULT);

instead of the CamcorderProfile lines. But now it crashes at perpare(). If setAudioSource(...) is called the LogCat is: E/MediaRecorder(20737): audio source is set, but audio encoder is not set if it isn't called the LogCat is: E/MediaRecorder(20544): video source is set, but video encoder is not set

I have looked allover the internet and I can't find a good example of the correct way to setup the MediaRecorder. Here it implies after API 8 you should use the CamcorderProfile class, but it seems to me that it is causing problems.

Any help would be great! Thanks!

Code (which works when run as it is below):

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 ...

解决方案

I don't have a lot of experience with MediaRecorder but I was reading some related topics and I will try to answer your questions:

1, 3 and 4) CamcorderProfile sets more than just the resolution, it also sets things as output format and encoders (for both audio and video). You are getting the error because you probably need to use setOutputFormat before calling setVideoSize and you have to call setVideoEncoder and setAudioEncoder after it, if you do not want to use CamcorderProfile. [According to this answer]

2) Again, CamcorderProfile also sets audio properties (such as Codec, BitRate, SampleRate,...) so you need to set an Audio Source before calling it, that's why the app crashed. If you don't want to record audio try the next code: (I didn't test it so I don't actually know if it works, but I am pretty sure it does)

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();

Also be aware that if you do not want to use CamcorderProfile (meaning that you want to record audio or video only) you may have to set additional parameters to assure you have the quality you want. Take a look at the following example code:

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();

I hope this helps you.

这篇关于采用Android MediaRecorder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆