MediaRecorder error: java.io.IOException: prepare failed

All your problems with Audio, Video and Images.

MediaRecorder error: java.io.IOException: prepare failed

Postby preetam_pict » Tue Sep 08, 2009 8:33 am

hi all

i have written a sample program to record the video (camcorder application).
when i run the program using the emulator (i have android-sdk-windows-1.5_r3) i get
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. java.io.IOException: prepare failed.
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
error as i call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MediaRecorder.prepare()
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
from
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. CamcorderActivity.surfaceCreated()
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

above errors are seen in logcat output...

BUT i can see the activity launched and camera preview ... again the preview is seen only in half of the screen !
its weird!!!

further when i do
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. recorder.start()
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
i get
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. java.lang.IllegalStateException
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
... this is obvious since the prepare() has failed. but why prepare() has failed ?

can someone please help me ... to understand whats happening here !

btw ... i have attached the logs and the screen shot !

thanks in advance
~pp

My code ...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.graphics.PixelFormat;
  13.  
  14. import android.media.MediaRecorder;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.util.Log;
  19.  
  20. import android.view.KeyEvent;
  21.  
  22. import android.view.SurfaceHolder;
  23.  
  24. import android.view.SurfaceView;
  25.  
  26.  
  27.  
  28. public class CamcorderActivity extends Activity implements
  29.  
  30.         SurfaceHolder.Callback
  31.  
  32. {
  33.  
  34.     private MediaRecorder recorder;
  35.  
  36.     private SurfaceView   surfaceView;
  37.  
  38.     private SurfaceHolder surfaceHolder;
  39.  
  40.  
  41.  
  42.     private boolean       recording = false;
  43.  
  44.  
  45.  
  46.     /** Called when the activity is first created. */
  47.  
  48.     @Override
  49.  
  50.     public void onCreate(Bundle savedInstanceState)
  51.  
  52.     {
  53.  
  54.         super.onCreate(savedInstanceState);
  55.  
  56.  
  57.  
  58.         // configure the surface
  59.  
  60.         getWindow().setFormat(PixelFormat.TRANSLUCENT);
  61.  
  62.         setContentView(R.layout.main);
  63.  
  64.         surfaceView = (SurfaceView) findViewById(R.id.camcordersurface);
  65.  
  66.         surfaceHolder = surfaceView.getHolder();
  67.  
  68.         surfaceHolder.addCallback(this);
  69.  
  70.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  71.  
  72.         configureRecorder();
  73.  
  74.     }
  75.  
  76.  
  77.  
  78.     private void configureRecorder()
  79.  
  80.     {
  81.  
  82.         // configure media recorder
  83.  
  84.         recorder = new MediaRecorder();
  85.  
  86.         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  87.  
  88.         recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  89.  
  90.         recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  91.  
  92.         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  93.  
  94.         recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
  95.  
  96.     }
  97.  
  98.  
  99.  
  100.     private void stopRecorder()
  101.  
  102.     {
  103.  
  104.         try
  105.  
  106.         {
  107.  
  108.             if (recorder == null)
  109.  
  110.             {
  111.  
  112.                 return;
  113.  
  114.             }
  115.  
  116.             recorder.stop();
  117.  
  118.             recorder.reset();
  119.  
  120.             recorder.release();
  121.  
  122.             recording = false;
  123.  
  124.             recorder = null;
  125.  
  126.         }
  127.  
  128.         finally
  129.  
  130.         {
  131.  
  132.             if (recorder != null)
  133.  
  134.             {
  135.  
  136.                 recorder.release();
  137.  
  138.             }
  139.  
  140.         }
  141.  
  142.     }
  143.  
  144.  
  145.  
  146.     private void startRecorder()
  147.  
  148.     {
  149.  
  150.         recorder.start();
  151.  
  152.         recording = true;
  153.  
  154.     }
  155.  
  156.  
  157.  
  158.     @Override
  159.  
  160.     public boolean onKeyDown(int keyCode, KeyEvent event)
  161.  
  162.     {
  163.  
  164.         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
  165.  
  166.         {
  167.  
  168.             // if not recording then start
  169.  
  170.             if (!recording)
  171.  
  172.             {
  173.  
  174.                 startRecorder();
  175.  
  176.             }
  177.  
  178.             else
  179.  
  180.             {
  181.  
  182.                 // if already recording then stop
  183.  
  184.                 stopRecorder();
  185.  
  186.                 finish();
  187.  
  188.             }
  189.  
  190.             return true;
  191.  
  192.         }
  193.  
  194.         return super.onKeyDown(keyCode, event);
  195.  
  196.     }
  197.  
  198.  
  199.  
  200.     @Override
  201.  
  202.     public void surfaceChanged(SurfaceHolder holder, int format, int width,
  203.  
  204.             int height)
  205.  
  206.     {
  207.  
  208.         // do nothing
  209.  
  210.     }
  211.  
  212.  
  213.  
  214.     @Override
  215.  
  216.     public void surfaceCreated(SurfaceHolder holder)
  217.  
  218.     {
  219.  
  220.         recorder.setOutputFile("/sdcard/test" + System.currentTimeMillis()
  221.  
  222.                 + ".mp4");
  223.  
  224.         recorder.setPreviewDisplay(holder.getSurface());
  225.  
  226.         try
  227.  
  228.         {
  229.  
  230.             recorder.prepare();
  231.  
  232.         }
  233.  
  234.         catch (IOException e)
  235.  
  236.         {
  237.  
  238.             Log.e("error -- ", e.toString(), e);
  239.  
  240.             // TODO:
  241.  
  242.             // show error message
  243.  
  244.         }
  245.  
  246.     }
  247.  
  248.  
  249.  
  250.     @Override
  251.  
  252.     public void surfaceDestroyed(SurfaceHolder holder)
  253.  
  254.     {
  255.  
  256.         stopRecorder();
  257.  
  258.     }
  259.  
  260. }
  261.  
  262.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical" android:layout_width="fill_parent"
  7.  
  8.         android:layout_height="fill_parent">
  9.  
  10.         <SurfaceView android:id="@+id/camcordersurface"
  11.  
  12.                 android:layout_width="fill_parent" android:layout_height="10dip"
  13.  
  14.                 android:layout_weight="1">
  15.  
  16.         </SurfaceView>
  17.  
  18. </LinearLayout>
  19.  
  20.  
  21.  
  22.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Attachments
camcorder.JPG
observe the camera view is only in half of the screen
camcorder.JPG (31.35 KiB) Viewed 1917 times
log.txt
logcat output (observe the exceptions)
(10.69 KiB) Downloaded 72 times
preetam_pict
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 25, 2009 2:52 pm
Location: India

Top

Postby preetam_pict » Tue Sep 08, 2009 11:00 am

well i am seeing following in logs ...

09-08 12:30:26.174: ERROR/MediaPlayer(554): Unable to to create media player
09-08 12:30:26.183: ERROR/CameraService(554): Failed to load CameraService sounds.
09-08 12:30:26.203: ERROR/MediaPlayer(554): Unable to to create media player
09-08 12:30:26.203: ERROR/CameraService(554): Failed to load CameraService sounds.


can someone please help me figure out this ?

thanks !
preetam_pict
Freshman
Freshman
 
Posts: 5
Joined: Tue Aug 25, 2009 2:52 pm
Location: India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: TurnitinBot [Bot] and 12 guests