Java Question MediaRecorder - setCamera called in an invalid state


Well-Known Member
Licensed User
Morning chaps and lasses,

I'm trying to create a library to utilize androids' MediaRecorder functionality.

Upon trying to setup a preview I keep getting an error.
setCamera called in an invalid state(4)
Here is the code snippet that the error occurs on

    public void StartPreview() {
        if(mState == STATE_UNINITIALIZED) {
            BA.Log("***MediaRecorder(" + mEventName + ") NOT Initialized***");
        else if(mFileSet != true) {
            BA.Log("***MediaRecorder(" + mEventName + ") OutputFile NOT Set***");
        else if(mState == STATE_STOPPED || mState == STATE_NONE) {
            BA.Log("Setting up preview");
            //Default to back Camera if not set
            if(mCamera != null){

                BA.Log("Camera Unlocked");
                ((MediaRecorder)getObject()).setCamera(mCamera); //error occurs on this line
                BA.Log("Camera Set to " + mCamera.toString());               
                //Default Camcorder Profile
                if (cProfile < 0) {
                    cProfile = CAMCORDER_PROFILE_QUALITY_HIGH;
                    BA.Log("Setting default Profile");
                BA.Log("Starting adding sv");
                mPanel.addView(sv, LP);
                mHolder = sv.getHolder();
                mHolder.setFixedSize(mPanel.getLayoutParams().width, mPanel.getLayoutParams().height);
                BA.Log("sv all added - " + LP.toString());
from looking at the unfiltered logs I would guess the camera Unlock()s ok

Setting up preview
Qint android::recording_enabled(camera_device*): E
recordingEnabled: (0)
Camera Unlocked
setCamera called in an invalid state(4)
main_activity_create (B4A line: 47)
    at Method)
As you can see I have added numerous 'logging' points to keep track of how it goes.
I have tried all sorts of combinations. Re-ordering the way other parameters are set like sources and encoders. At one point I had the invalid state @ (1) not (4), mostly it is 4 though. Any ideas?

If I don't add a camera or video source/encoder then the preview seems to start ok as though I could record audio.This camera seems to be the tricky bit.

You can see the entire code in necessary.



Well-Known Member
Licensed User
no takers? anybody had camera invalid state?


Staff member
Licensed User
Do you want to record video?

AudioStreamer object from the Audio library is based on MediaRecorder (and AudioTrack). However I haven't looked into video recording with MediaRecorder yet.

It will probably be quite complicated to build a good library for video recording. The Camera library (v2.0) is one of the more complicated libraries.


Well-Known Member
Licensed User
The Media Recorder can do audio and/or video. I wish to allow the full set of features

I have looked at many tutorials and the official docs, and tried all the documented 'order of commands'.

Apparently, all but the right one