1. Download our Official Android App: Forums for Android!

Android Mutimedia Framework bug Issue

Discussion in 'Music & Media' started by NilayMistry, Mar 31, 2011.

  1. NilayMistry

    NilayMistry Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Mar 31, 2011

    Mar 31, 2011
    1
    0
    5
    Hello All,
    This issue is related with multimedia framework,
    I am using Sony Ericsson Xperia X8 (E15i) with Android 2.1.
    I face a serious problem related with Video recording. My specific goal is to fetch frames from Video at real time.
    While I try to open an application it crashes.
    I posted my code here.
    Help me for this issue.
    MY CODE:::::::::::::::::::::::::


    package com.tcs.video;

    import java.io.IOException;

    import dalvik.system.TemporaryDirectory;
    import android.app.Activity;
    import android.app.Application;
    import android.content.Context;
    import android.content.pm.ActivityInfo;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.graphics.*;
    //import android.view.Window;
    import android.graphics.BitmapFactory.Options;

    public class VideoRecorder extends Activity{

    //Create objects of MediaRecorder and Preview class
    private MediaRecorder recorder;
    private Preview mPreview;

    boolean flag=false;
    boolean startedRecording=false;
    boolean stoppedRecording=false;

    // In this method, create an object of MediaRecorder class. Create an object of
    // RecorderPreview class(Customized View). Add RecorderPreview class object
    // as content of UI.
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    recorder = new MediaRecorder();
    //recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setCamera(Camera.open());
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mPreview = new Preview(VideoRecorder.this,recorder);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(mPreview);
    recorder.setPreviewDisplay(mPreview.getSurface());

    }

    /*!
    <p>
    Initialize the contents of the Activity's standard options menu. Menu items are to be placed in to menu.
    This is called on each press of menu button. In this options to start and stop recording are provided.
    Option for start recording has group id 0 and option to stop recording is 1.
    (first parameter of menu.add method). Start and stop have different group id, if recording is already
    started then it shows stop option else it shows start option.
    </p>*/
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    super.onPrepareOptionsMenu(menu);
    menu.clear();
    menu.add(0, 0, 0, "Start Recording");
    menu.add(1, 1, 0, "Stop Recording");

    menu.setGroupVisible(0, false);
    menu.setGroupVisible(1, false);

    if(startedRecording==false)
    menu.setGroupVisible(0, true);
    else if(startedRecording==true&&stoppedRecording==false)
    menu.setGroupVisible(1, true);

    return true;
    }


    /*!
    <p>
    This method receives control when Item in menu option is selected. It contains implementations
    to be performed on selection of menu item.
    </p>*/

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    switch (item.getItemId())
    {
    case 0:
    /*
    try {
    recorder.prepare();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    */
    //start the recorder
    recorder.start();
    startedRecording=true;

    break;

    case 1:
    //stop the recorder
    recorder.stop();
    recorder.release();
    recorder = null;
    stoppedRecording=true;
    break;


    default:
    break;
    }
    return super.onOptionsItemSelected(item);
    }

    class Preview extends SurfaceView implements SurfaceHolder.Callback//,PreviewCallback
    {
    //Create objects for MediaRecorder and SurfaceHolder.
    SurfaceHolder mHolder;
    MediaRecorder tempRecorder;
    Camera mCamera;
    Bitmap currentprev;

    //Create constructor of Preview Class. In this, get an object of
    //surfaceHolder class by calling getHolder() method. After that add
    //callback to the surfaceHolder. The callback will inform when surface is
    //created/changed/destroyed. Also set surface not to have its own buffers.
    public Preview(Context context,MediaRecorder recorder) {
    super(context);
    tempRecorder=recorder;
    mHolder=getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    // TODO Auto-generated constructor stub

    Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()
    {
    public void onPreviewFrame( byte[] data, Camera Cam ) {
    Log.d("CombineTestActivity", "Preview started");
    Log.d("CombineTestActivity", "Data length = " + data.length );
    //currentprev.recycle();
    /*
    currentprev = BitmapFactory.decodeByteArray( data, 0, data.length );

    if( currentprev == null )
    Log.d("CombineTestActivity", "currentprev is null" );

    Log.d("CombineTestActivity", "Preview Finished" );
    */

    }
    };

    mCamera =Camera.open();
    Camera.Parameters camParam = mCamera.getParameters();
    camParam.setPreviewFormat(PixelFormat.YCbCr_420_SP );
    camParam.setPreviewFrameRate(2);
    mCamera.setParameters( camParam );
    mCamera.setPreviewCallback( mPrevCallback );

    }

    public Surface getSurface()
    {
    return mHolder.getSurface();
    }

    // Implement the methods of SurfaceHolder.Callback interface

    // SurfaceCreated : This method gets called when surface is created.
    // In this, initialize all parameters of MediaRecorder object.
    //The output file will be stored in SD Card.

    public void surfaceCreated(SurfaceHolder holder){
    //tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");

    tempRecorder.setOutputFile("/sdcard/recordvideooutput.mp4");

    tempRecorder.setPreviewDisplay(mHolder.getSurface());

    try{
    //mCamera.release();
    //mCamera.unlock();
    //mCamera = Camera.open();
    //Thread.sleep(1000);
    mCamera.setPreviewDisplay(mHolder);
    mCamera.startPreview();
    //Thread.sleep(1000);
    //mCamera.unlock();
    tempRecorder.setCamera(Camera.open());
    //tempRecorder.prepare();
    } catch (Exception e) {
    String message = e.getMessage().toString();
    Log.e("Surface created :Error", message);
    tempRecorder.release();
    tempRecorder = null;
    }

    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
    if(tempRecorder!=null)
    {
    tempRecorder.release();
    tempRecorder = null;
    }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
    {
    Log.v("surfaceChanged method", "");
    mCamera.startPreview();

    }


    public void onPreviewFrame(byte [] data, Camera camera)
    {
    Log.v("onpreviewframe method", "data:"+data.toString());
    }
    }
    }
     

    Advertisement

Share This Page

Loading...