For several days I have been trying to figure out an obscure problem
related to saving video. I have narrowed it down to an issue with
MediaRecorder. The problem is when you record many (15-30) videos in a
row. What happens is that at some point (>10) the BEEP sound that
occurs when you start recording (i.e. MediaRecorder.start()) stops. In
LogCat there will be an error from AudioFlinger "ERROR/AudioTrack:
AudioFlinger could not create track, status: -12". Once this has
happen you can still record as many videos as you want without
problem, BUT if you press the phones volume keys the phone will CRASH.

The reason that I think it is related specifically to saving video is
that I can comment out all of the video related setup for the
MediaRecorder and then record only audio without any problem and
nothing else about the code/program is different.

I have included code that creates the same issue on my phone (Samsung
Captivate running 2.2). Note that my actually video recorder code is
different and that this code is simply to share and focus on the
MediaRecorder video save issue.

If you have experienced a similar issue please respond, if you test
this code on your phone and you don't have any problems please let me
know as I have started to think it is a phone/firmware issue. If you
have code that works and can share please do.

Thanks,


public class Camcorder extends Activity implements
SurfaceHolder.Callback {
                MediaRecorder mRecorder;
                SurfaceHolder mHolder;
                SurfaceView mSurfaceView;
                String mOutputFileRoot = "/sdcard/Avid_";
                String mOutputFile;
                String mFileExt = ".3gp";
                Integer cnt = 0;
             private boolean mRecording = false;

             /** Called when the activity is first created. */
             @Override
             public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  requestWindowFeature(Window.FEATURE_NO_TITLE);
        
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                        
WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                  setContentView(R.layout.camcorder_preview);

                  mSurfaceView =
((SurfaceView)findViewById(R.id.camera_preview));
                  mHolder = mSurfaceView.getHolder();
                  mHolder.addCallback(this);
                          
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

             }

         @Override
         public boolean onKeyDown(int keyCode, KeyEvent event)
         {
             if (keyCode == KeyEvent.KEYCODE_SEARCH)
             {
                  if (mRecording) {
                                stopRecording();
                    //finish();
                         if(mRecorder == null){
                                initMediaRecorder();
                                prepareMediaRecorder();
                                }
                                mRecording = false;
                } else {
                    mRecording = true;

                    startRecording();
                }
                 return true;
             }
             return super.onKeyDown(keyCode, event);
         }

         public void surfaceCreated(SurfaceHolder holder) {
                 mHolder = holder;
                 initMediaRecorder();
                 prepareMediaRecorder();

        }

        public void surfaceChanged(SurfaceHolder holder, int format,
int width,
                        int height) {
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
                if(mHolder != null) mHolder = null;
                if(mSurfaceView != null) mSurfaceView = null;
        }

        public void initMediaRecorder(){

                mRecorder = new MediaRecorder();

                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
                mRecorder.setPreviewDisplay(mHolder.getSurface());
                mOutputFile = mOutputFileRoot + cnt.toString() + mFileExt;
                cnt += 1;
                mRecorder.setOutputFile(mOutputFile);

        }

        private void prepareMediaRecorder(){
                if (mRecorder != null) {
                        try {
                                mRecorder.prepare();
                        } catch (IllegalStateException e) {
                                Log.e("IllegalStateException", e.toString());
                        } catch (IOException e) {
                                Log.e("IOException", e.toString());
                        }
                }
        }

         public void startRecording()
         {
                mRecorder.start();
         }

         public void stopRecording()
         {
                mRecorder.stop();
                mRecorder.release();
                mRecorder = null;
         }

}

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.testing.camcorder"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name"
      android:debuggable="true">
        <activity android:name=".Camcorder"
                  android:label="@string/app_name"
                  android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-
permission>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"></uses-
permission>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
</manifest>


xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
     android:orientation="vertical"
android:layout_width="fill_parent"
     android:layout_height="fill_parent">

<FrameLayout

    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

             <SurfaceView android:id="@+id/camera_preview"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:clickable="true" />

 </FrameLayout>
</LinearLayout>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to