official Android documentation for MediaRecorder.prepare()
說:Android MediaRecorder.prepare()是做什麼的?
準備記錄器開始捕獲和編碼數據。在設置所需的音頻和視頻源,編碼器,文件格式等之後,但在啓動()之前,必須調用此方法。
正是這樣做有什麼,爲什麼我們需要調用它之前,我們開始捕捉媒體數據?
official Android documentation for MediaRecorder.prepare()
說:Android MediaRecorder.prepare()是做什麼的?
準備記錄器開始捕獲和編碼數據。在設置所需的音頻和視頻源,編碼器,文件格式等之後,但在啓動()之前,必須調用此方法。
正是這樣做有什麼,爲什麼我們需要調用它之前,我們開始捕捉媒體數據?
該函數(prepare())將創建一個隨機訪問文件流,以便從具有指定名稱的文件中讀取數據,並可選擇寫入文件。 這是流處理所必需的。除非使用此函數,否則流程將不會啓動。 see this doc.
您可能需要閱讀源代碼在這裏:MediaRecorder.java:833
代碼一瞥:
/**
* Prepares the recorder to begin capturing and encoding data. This method
* must be called after setting up the desired audio and video sources,
* encoders, file format, etc., but before start().
*
* @throws IllegalStateException if it is called after
* start() or before setOutputFormat().
* @throws IOException if prepare fails otherwise.
*/
public void prepare() throws IllegalStateException, IOException
{
if (mPath != null) {
RandomAccessFile file = new RandomAccessFile(mPath, "rws");
try {
_setOutputFile(file.getFD(), 0, 0);
} finally {
file.close();
}
} else if (mFd != null) {
_setOutputFile(mFd, 0, 0);
} else {
throw new IOException("No valid output file");
}
_prepare();
}
這裏的代碼的本地部分:mediarecorder.cpp
status_t MediaRecorder::prepare()
{
ALOGV("prepare");
if (mMediaRecorder == NULL) {
ALOGE("media recorder is not initialized yet");
return INVALID_OPERATION;
}
if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) {
ALOGE("prepare called in an invalid state: %d", mCurrentState);
return INVALID_OPERATION;
}
if (mIsAudioSourceSet != mIsAudioEncoderSet) {
if (mIsAudioSourceSet) {
ALOGE("audio source is set, but audio encoder is not set");
} else { // must not happen, since setAudioEncoder checks this already
ALOGE("audio encoder is set, but audio source is not set");
}
return INVALID_OPERATION;
}
if (mIsVideoSourceSet != mIsVideoEncoderSet) {
if (mIsVideoSourceSet) {
ALOGE("video source is set, but video encoder is not set");
} else { // must not happen, since setVideoEncoder checks this already
ALOGE("video encoder is set, but video source is not set");
}
return INVALID_OPERATION;
}
status_t ret = mMediaRecorder->prepare();
if (OK != ret) {
ALOGE("prepare failed: %d", ret);
mCurrentState = MEDIA_RECORDER_ERROR;
return ret;
}
mCurrentState = MEDIA_RECORDER_PREPARED;
return ret;
}
非常有幫助,謝謝:) –
簡短而準確的答案,謝謝:) –