android


What does Android MediaRecorder.prepare() do?


The official Android documentation for MediaRecorder.prepare() says:
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().
What exactly does it do, and why do we need to call it before we start to capture media data?
this function (prepare()) will be creates a random-access file stream to read from a file with the specified name and optionally write to a file.
This is required for stream processing. The stream process will not be started unless this function is used.
see this doc.
You might want to read the source code here: MediaRecorder.java:833
A glimpse of the code:
/**
* 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();
}
And the native part of the code here: 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;
}

Related Links

Android design practice - switch view
Error on the super.onActivityResult() why?
Way to get ParseObjects to custom Search suggestions in Android
Camera preview doesn't start and doesn't show any error
Internal Error by start from IDE
WebView difference in android 4.3 to 4.4 [closed]
Zoom and Panning ImageView Android oncreate call
How to add multiple contents in view pager?
Convert frame layout into image and save it [closed]
Select images from Galley and add animation
The Broadcast Receiver didn't start after starting the activity class
Emoji symbols look different on the keyboard key label and textView
playing rtsp and loading images android phonegap (access origin )
How to make images look good on both nexus and nexus 10?
How to detect multiple gestures in android?
content must have a ListView whose id attribute is 'android.R.id.list' [duplicate]

Categories

HOME
windows
scikit-learn
deep-learning
gdb
developer-tools
rdbms
spring-data-mongodb
entity-framework-core
carousel
permissions
gembox-spreadsheet
regular-language
mvc5
elasticsearch-5
project
jquery-ui-sortable
pcl-crypto
startup
mxgraph
datastage
dynatrace
eip
easyphp
soci
swagger-2.0
drive
user-experience
undertow
imagemap
angular-routing
exploit
roundup
beta
data.stackexchange.com
interbase
jackson-modules
nstimer
send
aws-codecommit
jbutton
papaparse
github3.py
android-maps-extensions
instantiation
dynamic-jasper
processwire
railstutorial.org
opensmpp
odoo-website
levenshtein-distance
softmax
install.packages
flask-restful
jboss-4.2.x
xbmc
qtranslate
ngmaterial
avx
reachability
slackware
redistributable
centrifuge
vulcanize
nikeplus-api
xdoclet
query-by-example
dbamp
grgit
avisynth
uvc
addressing-mode
deform
trdion2011
android-audiorecord
syndicationfeed
light
gd-graph
scrypt
hidapi
time.h
onload-event
robotlegs
convention
flex-mobile
ikimagebrowserview
umra
unattended-processing
tmx
twitterizer
photolibrary
ruby-1.8
aspmenu
alsb
pbcopy
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App