Wednesday 25 September 2013

Video Recording in Background Using service Android

Here ,There is one Sample activity which records video in background using of service.

MainVideoActivity.class


public class MainVideoActivity extends Activity implements
SurfaceHolder.Callback {

public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mCamera;
public static boolean mPreviewRunning;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_video);

mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Button startVideoButton = (Button) findViewById(R.id.button1);
startVideoButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
startService(new Intent(MainVideoActivity.this,
RecorderService.class));
}
});

Button stopVideoButton = (Button) findViewById(R.id.button2);
stopVideoButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
stopService(new Intent(MainVideoActivity.this,
RecorderService.class));
}
});
}

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

@Override
public void surfaceCreated(SurfaceHolder holder) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}


RecorderService.class (For start and stop  video recording )

public class RecorderService extends Service {
private SurfaceHolder mSurfaceHolder;
private static Camera camera;
public static boolean recordingStatus;
private MediaRecorder mediaRecorder;
private File directory;
private int cameraType = 0;

@Override
public void onCreate() {
recordingStatus = false;
camera = MainVideoActivity.mCamera;
mSurfaceHolder = MainVideoActivity.mSurfaceHolder;

super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (recordingStatus == false) {

startRecording();
}

return START_STICKY;
}

@Override
public void onDestroy() {
stopRecording();
camera.release();
recordingStatus = false;

super.onDestroy();
}

@SuppressLint("NewApi")
public boolean startRecording() {
try {
recordingStatus = true;

camera = Camera.open(cameraType);
camera.setDisplayOrientation(90);
Camera.Parameters params = camera.getParameters();
camera.setParameters(params);
Camera.Parameters p = camera.getParameters();

camera.setParameters(p);

camera.unlock();

mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

directory = new File(Environment.getExternalStorageDirectory()
.toString() + "/videodemo/");
if (!directory.exists())
directory.mkdirs();

long currentTime = System.currentTimeMillis();

String uniqueOutFile = Environment.getExternalStorageDirectory()
.toString()
+ "/videodemo/videooutput"
+ currentTime
+ ".mp4";
File outFile = new File(directory, uniqueOutFile);
if (outFile.exists()) {
outFile.delete();
}

mediaRecorder.setOutputFile(uniqueOutFile);

mediaRecorder.setVideoSize(350, 250);
mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mediaRecorder.setOrientationHint(90);

mediaRecorder.prepare();
mediaRecorder.start();

return true;
} catch (IllegalStateException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

// Stop service
public void stopRecording() {
mediaRecorder.stop();
mediaRecorder.release();
}
}

Add below permission in manifest file.

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

define service.
 <service android:name="com.videodemo.RecorderService" />

8 comments:

  1. Android Services don't run in a background thread, they run in the UI thread. So your code example is recording video in the UI thread, just as if you had kept all that code in the Activity. To get a service to run asynchronously you'd need to extend IntentService, or implement a Handler that you could hand off tasks to. Anyhow, I don't think background video recording is even possible on Android. You need a reference to a preview holder, which you couldn't get in a background thread.

    ReplyDelete
    Replies
    1. Yes I agree this works, I'm just saying the video recording is not actually running in a background thread.

      Delete
    2. You are using SurfaceView from the Main UI Thread and using it inside Service. Even if you write your code in Service inside a new thread, the recording will fail as soon as you close the Main Thread UI App....

      Delete
  2. Not working on kitkat........ check this code.

    ReplyDelete
  3. not working app crashes as soon as the button is clicked...
    Please help

    ReplyDelete

Copy and share your useful data from Google chrome browser to your application in android.

Here In this blog I explain that how to copy and share your useful data from chrome browser in android. 1) How to show your applic...