Question

I am trying to make a simple video recorder app in which I specify the maximum file size as a given number. The application as such works fine but I am not able to display a toast informing the user that the video clip has been saved, once the recording is complete. This is my code:

package com.example.videocapture2;

import java.io.IOException;

import android.app.Activity; 
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements  SurfaceHolder.Callback,android.media.MediaRecorder.OnInfoListener{
    MediaRecorder recorder;
    SurfaceHolder holder;
    Button Rec = null;
    boolean recording = false;
    int count =1;
    @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);

    recorder = new MediaRecorder();
    initRecorder();
    setContentView(R.layout.activity_main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.videoview);

    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    //recorder.start();

    //cameraView.setClickable(true);
   // cameraView.setOnClickListener(this);

    Rec = (Button)findViewById(R.id.mybutton);
    Rec.setOnClickListener(new View.OnClickListener() {

        @Override
         public void onClick(View v) {
            if (recording) {
                recorder.stop();
                recording = false;

                // Let's initRecorder so we can record again
                initRecorder();
                prepareRecorder();
            } else {
                recording = true;
                recorder.start();
            }
        }
    });
}

private void initRecorder() {
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    CamcorderProfile cpHigh = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setOutputFile("/sdcard/videocapture_example"+count+".mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(2*1048576); // Approximately 2 megabytes
    count++;
}

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}



public void surfaceCreated(SurfaceHolder holder) {
    prepareRecorder();
}

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

public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    //Toast.makeText(getApplicationContext(), "Video is saved", Toast.LENGTH_LONG).show();
    //finish();
}

@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
    // TODO Auto-generated method stub
    System.out.println("Reached onInfoListener");
    if(what==android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)
    {
        Toast.makeText(getApplicationContext(), "Video clip recorded", Toast.LENGTH_LONG).show();
    }
}


}

I found out that due to the

recorder.setMaxFileSize(2*1048576); // Approximately 2 megabytes

in initRecorder() method, once the specified file size is reached, notification is sent to the OnInfoListener with a particular what code. So I overrode the method in my MainActivity, however, the toast doesn't display after the video file is completely recorded, though I am able to access the video clip and play it.

What should I do display this toast in the above mentioned conditions?

Was it helpful?

Solution

I think you haven't assigned the onInfoListener. i.e. you need to:

     recorder.setOnInfoListener(this);

since your activity extends MediaRecorder.OnInfoListener

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top