Quantcast
Channel: Kyogs
Viewing all articles
Browse latest Browse all 18

Video Recording in Background Using service Android

$
0
0
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" />


Viewing all articles
Browse latest Browse all 18

Trending Articles