views:

18

answers:

0

I need to record video & audio. when click the start record button.The video does not store on the sd card and when i click on the stop recording it shoe the error unexpectedly stopped. I am new to android please help me. Advanced thanks to you.

Here is video and audio recording code

public class audio extends Activity {

public MediaRecorder mrec = null;
private Button startRecording = null;
private Button stopRecording = null;
private static final String TAG = "SoundRecordingDemo";
File audiofile;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mrec = new MediaRecorder();
    startRecording = (Button)findViewById(R.id.startrecording);
    stopRecording = (Button)findViewById(R.id.stoprecording);
    startRecording.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
       try
       {    
           Context appContext = getApplicationContext();
           startRecording.setEnabled(false);
           stopRecording.setEnabled(true);
           stopRecording.requestFocus();
           startRecording();
       }catch (Exception ee)
       {
           Log.e(TAG,"Caught io exception " + ee.getMessage());
       }

      }
    });

    stopRecording.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
          startRecording.setEnabled(true);
          stopRecording.setEnabled(false);
          startRecording.requestFocus();
          stopRecording();
          processaudiofile();
      }

    });

   stopRecording.setEnabled(false);
   startRecording.setEnabled(true);

}
protected void processaudiofile() {
    ContentValues values = new ContentValues(4);
    long current = System.currentTimeMillis();
    values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
    values.put(MediaStore.Video.Media.TITLE, "video" + audiofile.getName());
    values.put(MediaStore.Video.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Video.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Video.Media.DATA, audiofile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);

    // this does not always seem to work cleanly....
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
  }


protected void startRecording() throws IOException 
{
    mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
    mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mrec.setMaxDuration(DEFAULT_KEYS_SEARCH_GLOBAL);
   // mrec.setOnInfoListener(listener);
    mrec.setVideoSize(320, 240);
    mrec.setVideoFrameRate(15);

   // mRecorder.setOutputFile("/sdcard/yousuck2.3gp");
    if (audiofile == null) {
          File sampleDir = Environment.getExternalStorageDirectory();

          try { 
              audiofile = File.createTempFile("ibm", ".3gp", sampleDir);
          } 
          catch (IOException e)
          {
              Log.e(TAG,"sdcard access error");
              return;
          }
    }

    mrec.setOutputFile(audiofile.getAbsolutePath());

    mrec.prepare();
    mrec.start();

}

protected void stopRecording() {
    mrec.stop();
    mrec.release();
  }

}