Эта проблема связана с мультимедийной платформой,
Я использую Sony Ericsson Xperia X8 (E15i) с Android 2.1.
Я столкнулся с серьезной проблемой, связанной с записью видео.Моя конкретная цель - получить кадры из видео в режиме реального времени.
Когда я пытаюсь открыть приложение, оно вылетает.
Я разместил здесь свой код.
Помогите мне с этим вопросом.
МОЙ КОД :::::::::::::::::::::::::
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());
}
}
}