user3188402
user3188402

Reputation: 205

cannot set the MediaRecorder profile using setProfile() it returns a null pointer exception

whenever I try to set the profile of my MediaRecorder object to camcorderProfile it gives me a NullPointerException which means that my camcorderProfile is somehow null but I have set it. Here is my entire function that prepares the recorder and camera for running.

  private void prepareRecorder()
  {
    recorder = new MediaRecorder();
    recorder.setPreviewDisplay(holder.getSurface());
    camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);

    if (usecamera)
    {
        camera.unlock();
        recorder.setCamera(camera);
    }
    recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setProfile(camcorderProfile);

   String fileName = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

    if(camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP)
    {
        fileExtension = ".3gp";
    }

    if(camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4)
    {
        fileExtension = ".mp4";
    }


    try
    {
        FileBuffer = File.createTempFile(fileName, fileExtension, Environment.getExternalStorageDirectory());
        recorder.setOutputFile(FileBuffer.getAbsolutePath());
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

    recorder.setMaxDuration(30000); // 30 seconds

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

    } catch (IOException e) {
        e.printStackTrace();

    }

   }

Upvotes: 0

Views: 810

Answers (1)

Santhosh
Santhosh

Reputation: 158

CamcorderProfile can be null if the requested quality is not supported by camera. In your case its back camera since you are not passing camera ID while querying for profile.

So its better to check for the support using API:

public static boolean hasProfile (int quality)

Upvotes: 1

Related Questions