jac*_*ism 11 android mediarecorder
以下是我录制视频和音频的工作代码的结构:
问题:1)为什么CamcorderProfile需要?setProfile(...)似乎将尺寸设置为QUALITY_HIGH给出的尺寸,但稍后我设置了我想要的尺寸setVideoSize(...),这将覆盖它.但是,当我删除两个CamcorderProfile行时,应用程序崩溃setVideoSize(...)与LogCat E/MediaRecorder(19526): setVideoSize called in an invalid state: 2.
2)我怎么不录音?文档说明如果setAudioSource(...)未调用,则不会有音轨.但是,当我删除该行时,应用程序setProfile(...)与LogCat 崩溃E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first.
3)如果我删除了两个CamcorderProfile行和该setAudioSource(...)行,它会像1)中那样崩溃.
4)我也试过添加线
recorder.setOutputFormat(OutputFormat.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
而不是CamcorderProfile线.但现在它崩溃了perpare().如果setAudioSource(...)调用LogCat是:E/MediaRecorder(20737): audio source is set, but audio encoder is not set如果没有调用LogCat是:E/MediaRecorder(20544): video source is set, but video encoder is not set
我已经看了整个互联网,我找不到一个正确的方法来设置MediaRecorder的一个很好的例子.这意味着在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 ...
Run Code Online (Sandbox Code Playgroud)
Tho*_* H. 17
我没有很多使用MediaRecorder的经验,但我正在阅读一些相关主题,我会尝试回答你的问题:
1,3和4) CamcorderProfile不仅提供分辨率,还设置输出格式和编码器(用于音频和视频).你所得到的错误,因为你可能需要使用setOutputFormat调用之前setVideoSize,你必须调用setVideoEncoder和setAudioEncoder后,如果您不想使用CamcorderProfile.[根据这个答案 ]
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();
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您不想使用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();
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你.
| 归档时间: |
|
| 查看次数: |
18212 次 |
| 最近记录: |