开发者

MediaRecorder save video problem

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,

[code]

    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;   
     }

     @Override
     public void onPause(){
         if(mRecorder != null){
             mRecorder.reset();
             mRecorder.release();
             mRecorder = null; 
         }
         super.onPause();
     }
    }

[/code]

manifest

    <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"></user-permission>
   </manifest> 

xml layout

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


Appears to be a problem specific to the phone or operating system. I have now tested the above code and my more complex video recorder code on two different phone and I did not get the above issue. Unfortunately I do not know if it was phone or os.

After more testing it seems to be a problem with Android 2.2. On two different phones (Samsung Vibrant and Captivate) running 2.2 the problem occurs. I have tested it on a separate phone (Samsung Vibrant) running 2.1 and 2.1-update1 and an HTC running 2.3.1 and there was no problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜