gpt4 book ai didi

android - 使用安卓 MediaRecorder

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:58:01 25 4
gpt4 key购买 nike

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

问题:1) 为什么需要 CamcorderProfilesetProfile(...) 似乎将尺寸设置为 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);

而不是 CamcorderProfile 行。但现在它在 perpare() 处崩溃。如果 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 并且您必须调用 setVideoEncodersetAudioEncoder 之后,如果您不想使用 CamcorderProfile。 [根据这个answer ]

2) 同样,CamcorderProfile 还会设置音频属性(例如编解码器、比特率、采样率等),因此您需要在调用它之前设置音频源,这就是应用程序崩溃的原因。如果您不想录制音频,请尝试下一个代码:(我没有测试它,所以我实际上不知道它是否有效,但我很确定它有效)

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

希望对你有帮助

关于android - 使用安卓 MediaRecorder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17813614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com