Video Record

All your problems with Audio, Video and Images.

Video Record

Postby undying1983 » Fri Dec 10, 2010 3:44 pm

Hello everyone, I have got a problem, when I try to record a video from my camera I receive a warning and an error respectively when I start the recording and when I stop it.

The code below:

Code: Select all
package com.adelchi.prova;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.hardware.Camera;
import android.hardware.Camera.Size;

import android.media.MediaRecorder;
import android.os.Bundle;

import android.util.Log;

import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class prova extends Activity implements SurfaceHolder.Callback{
   
   private SurfaceView mSurfaceView;
   private SurfaceHolder mSurfaceHolder;
   private Camera mCamera;
   private boolean mPreviewRunning;
   
   private static final String TAG = "RECORD";
   
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
       
        mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
        mSurfaceHolder = mSurfaceView.getHolder();      //recupero l'holder della surfaceview
        mSurfaceHolder.addCallback(this);       //faccio la bind alla nostra activity
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        //tipo di surface, suggerito nei tutorial ufficiali
       
        final ImageButton buttonPictureR = (ImageButton) findViewById(R.id.camera_surface_buttonR);
        final ImageButton buttonPictureS = (ImageButton) findViewById(R.id.camera_surface_buttonS);
                buttonPictureR.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {
                           
                           startRecording();
                           buttonPictureR.setVisibility(4);
                         buttonPictureS.setVisibility(0);
                           
                           
                        }
                });
               
               
                   buttonPictureS.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {
                           
                              stopRecording();
                               buttonPictureR.setVisibility(0);
                             buttonPictureS.setVisibility(4);
                              
                        }
                });               
               
   }
   
   public void surfaceCreated(SurfaceHolder holder) {
      
       mCamera = Camera.open();

    }
   
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (mPreviewRunning)
                mCamera.stopPreview();
       
        //setto le preferenze
        Camera.Parameters p = mCamera.getParameters();  //prendo le preferenze della camera
       
        p.setPreviewSize(arg2, arg3);
        //p.setPreviewFormat(PixelFormat.JPEG);
        ArrayList<Size> list = (ArrayList<Size>) p.getSupportedPictureSizes();  //recuepro le risoluzioni supportate dalla camera
        List<String> list2 = p.getSupportedFocusModes();  //recuepro le risoluzioni supportate dalla camera
        int picture_width = list.get(0).width;
        int picture_height = list.get(0).height;
        p.setFocusMode(list2.get(0));
       
        p.setPictureSize(picture_width, picture_height);
       
        // salvo le pref
        mCamera.setParameters(p);
        try {
           
           System.out.println("sono fuori");
           
           //lancio la preview
           mCamera.setPreviewDisplay(arg0);       
            mCamera.startPreview();
            mPreviewRunning = true;
        } catch (IOException e) {
           System.out.println("sono dentro");
                //gestione errore
        }
       
   }
   
   public void surfaceDestroyed(SurfaceHolder holder) {
       
       mCamera.stopPreview();
       mPreviewRunning = false;
       mCamera.release();
           
    }
   
   private MediaRecorder mediaRecorder;
   private final int MAX_TIME = 20000;

   public boolean startRecording(){
      try {

         mediaRecorder = new MediaRecorder();
         mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
         mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
         mediaRecorder.setMaxDuration((int) MAX_TIME);
         mediaRecorder.setVideoSize(320, 240);
         mediaRecorder.setVideoFrameRate(20);
         mediaRecorder.setOutputFile("/sdcard/recordvideooutput.3gp");
         
         mediaRecorder.prepare();
         mediaRecorder.start();

         return true;
      } catch (IllegalStateException e) {
         Log.e(TAG,e.getMessage());
         e.printStackTrace();
         return false;
      } catch (IOException e) {
         Log.e(TAG,e.getMessage());
         e.printStackTrace();
         return false;
      }
   }
   
   public void stopRecording(){
      mediaRecorder.stop();
      mediaRecorder.release(); // Now the object cannot be reused
   }
}


And the first warning

12-10 15:01:17.197: WARN/System.err(9068): java.io.IOException: prepare failed.

12-10 15:01:17.220: WARN/System.err(9068): at android.media.MediaRecorder._prepare(Native Method)
12-10 15:01:17.220: WARN/System.err(9068): at android.media.MediaRecorder.prepare(MediaRecorder. java:530)
12-10 15:01:17.220: WARN/System.err(9068): at com.adelchi.prova.prova.startRecording(prova.java: 136)
12-10 15:01:17.220: WARN/System.err(9068): at com.adelchi.prova.prova$1.onClick(prova.java:50)
12-10
15:01:17.220: WARN/System.err(9068): at android.view.View.performClick(View.java:2461)
12-10
15:01:17.220: WARN/System.err(9068): at android.view.View$PerformClick.run(View.java:8890)
12-10
15:01:17.220: WARN/System.err(9068): at android.os.Handler.handleCallback(Handler.java:587 )
12-10
15:01:17.220: WARN/System.err(9068): at android.os.Handler.dispatchMessage(Handler.java:92 )
12-10
15:01:17.224: WARN/System.err(9068): at android.os.Looper.loop(Looper.java:123)
12-10 15:01:17.224: WARN/System.err(9068): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
12-10
15:01:17.224: WARN/System.err(9068): at java.lang.reflect.Method.invokeNative(Native Method)
12-10
15:01:17.224: WARN/System.err(9068): at java.lang.reflect.Method.invoke(Method.java:521)
12-10
15:01:17.224: WARN/System.err(9068): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:871)
12-10
15:01:17.224: WARN/System.err(9068): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:629)
12-10 15:01:17.224: WARN/System.err(9068): at dalvik.system.NativeStart.main(Native Method)

And the error

12-10 15:01:22.529: ERROR/AndroidRuntime(9068): FATAL EXCEPTION: main
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): java.lang.IllegalStateException
12-10
15:01:22.529: ERROR/AndroidRuntime(9068): at android.media.MediaRecorder.stop(Native Method)
12-10
15:01:22.529: ERROR/AndroidRuntime(9068): at com.adelchi.prova.prova.stopRecording(prova.java:1 52)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at com.adelchi.prova.prova$2.onClick(prova.java:62)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at android.view.View.performClick(View.java:2461)
12-10
15:01:22.529: ERROR/AndroidRuntime(9068): at android.view.View$PerformClick.run(View.java:8890)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at android.os.Handler.handleCallback(Handler.java:587 )
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at android.os.Handler.dispatchMessage(Handler.java:92 )
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at android.os.Looper.loop(Looper.java:123)
12-10
15:01:22.529: ERROR/AndroidRuntime(9068): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at java.lang.reflect.Method.invoke(Method.java:521)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:871)
12-10
15:01:22.529: ERROR/AndroidRuntime(9068): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:629)
12-10 15:01:22.529: ERROR/AndroidRuntime(9068): at dalvik.system.NativeStart.main(Native Method)
12-10
15:01:22.552: WARN/ActivityManager(2501): Force finishing activity com.adelchi.prova/.prova

If somebody can help me, thank you in advance.
undying1983
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 10, 2010 3:32 pm

Top

Re: Video Record

Postby underminedsk » Mon Dec 13, 2010 12:25 am

The Video Recording Library on Android is tricky to use - try reading the documentation page very carefully - you have to call all the methods to set up the video recorder in a specific order, otherwise you will get exceptions when you try to start recording.

http://developer.android.com/reference/ ... order.html
underminedsk
Developer
Developer
 
Posts: 26
Joined: Tue Mar 02, 2010 3:36 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 7 guests