I am trying to record mpeg2-ts video in order to transfer it to server / socket using the Android MediaRecorder class as described here ...
The code is as follows:
public class MediaRecorderDemo extends Activity { private final static String TAG = "MediaRecorderDemo"; Camera camera; CameraPreview cameraPreview; MediaRecorder mediaRecorder; File outputFile = new File(Environment.getExternalStorageDirectory().getPath() + "/out1.ts"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media_recorder_demo_layout); camera = getCameraInstance(); cameraPreview = new CameraPreview(this); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(cameraPreview); } private Camera getCameraInstance() { final String FUNCTION = "getCameraInstance"; Camera c = null; try { c = Camera.open(); } catch(Exception e) { Log.e(TAG, FUNCTION + " : " + e.getMessage()); } return c; } void initMediaRecorder() { final String FUNCTION = "initMediaRecorder"; FileDescriptor outputFileFD = null; try { outputFile.createNewFile(); outputFileFD = new FileOutputStream(outputFile).getFD(); } catch(Exception e) { Log.e(TAG, FUNCTION + " : " + e.getMessage()); } mediaRecorder = new MediaRecorder(); mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() { final static String TAG = "MediaRecorder.onErrorListener"; @Override public void onError(MediaRecorder mr, int what, int extra) { Log.e(TAG, "Error : " + what + " " + extra); } }); camera.unlock(); mediaRecorder.setPreviewDisplay(cameraPreview.getHolder().getSurface()); mediaRecorder.setCamera(camera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.prepare() and mediaRecorder.start() is called without any errors and the camera preview is displayed ... But after a while, the preview is turned off and then the screen freezes and an empty file is created on the output path ... This problem also reported in Android Issue List but not yet fixed ...
I tried to run the same application on the Galaxy Note N7000 with Android ICS ROM, as well as on the Samsung Galaxy Tab 2 P3100, but with a custom Android 4.2 ROM ... So this doesnβt look like something related to ROM or a specific hardware configuration ...
If there is something that is missing or something is wrong, it would be nice and, of course, easier to know that ...?
Thanks...
source share