Java Question MediaRecorder - setCamera called in an invalid state

barx

Well-Known Member
Licensed User
Morning chaps and lasses,

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

http://developer.android.com/reference/android/media/MediaRecorder.html

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

B4X:
    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){

                mCamera.unlock();
                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");
                }               
   
                ((MediaRecorder)getObject()).setProfile(CamcorderProfile.get(cProfile));
           
                BA.Log("Starting adding sv");
                mPanel.addView(sv, LP);
                mHolder = sv.getHolder();
                mHolder.setFixedSize(mPanel.getLayoutParams().width, mPanel.getLayoutParams().height);
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                //mCamera.startPreview();
                BA.Log("sv all added - " + LP.toString());
            }
        }
    }
from looking at the unfiltered logs I would guess the camera Unlock()s ok

B4X:
Setting up preview
recordingEnabled
Qint android::recording_enabled(camera_device*): E
recordingEnabled: (0)
Camera Unlocked
setCamera called in an invalid state(4)
main_activity_create (B4A line: 47)
MR.StartPreview()
java.lang.IllegalStateException
    at android.media.MediaRecorder.setCamera(Native 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.

Thanks
 

barx

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

Erel

Administrator
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.
 

barx

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
 
Top