11

以下は、ビデオとオーディオを記録するための作業コードの構造です。

質問: 1) なぜCamcorderProfile必要なのですか? setProfile(...)ディメンションを QUALITY_HIGH が与えるものに設定するように見えますが、後で で必要なディメンションを設定しsetVideoSize(...)、これをオーバーライドします。ただし、2 つの 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 が呼び出される場合: LogCatE/MediaRecorder(20737): audio source is set, but audio encoder is not setが呼び出されない場合: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 ...
4

1 に答える 1

20

MediaRecorder の経験はあまりありませんが、関連するトピックをいくつか読んでいたので、あなたの質問に答えようと思います。

1、3、4 ) CamcorderProfile は解像度だけでなく、出力形式やエンコーダー (オーディオとビデオの両方) も設定します。CamcorderProfile を使用したくない場合は、おそらくsetOutputFormat呼び出す前に使用する必要がありsetVideoSize、呼び出してから呼び出す必要があるため、エラーが発生しています。[この回答によると]setVideoEncodersetAudioEncoder

2)繰り返しますが、CamcorderProfile はオーディオ プロパティ (Codec、BitRate、SampleRate など) も設定するため、呼び出す前に Audio Source を設定する必要があります。これが原因でアプリがクラッシュしました。オーディオを録音したくない場合は、次のコードを試してください。

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

これがお役に立てば幸いです。

于 2013-07-23T15:50:18.663 に答える