I am trying to develop a simple android video recorder. For this I have created a custom SurfaceView with MediaRecoder to handle the recording. Everything builds fine and it also runs fine on the android device. But when I call startRecording(), its giving me IllegalStateException. Here is the code:
package my.android.video;
import android.media.MediaRecorder;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.util.Log;
import java.lang.IllegalArgumentException;
import java.lang.IllegalStateException;
import java.io.IOException;
import android.content.Context;
public class RecorderView extends SurfaceView implements SurfaceHolder.Callback {
private MediaRecorder mediaRecorder;
public RecorderView(Context context)
{
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mediaRecorder == null) {
try {
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4");
mediaRecorder.setPreviewDisplay(holder.getSurface());
mediaRecorder.prepare();
} catch (IllegalArgumentException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
} catch (IllegalStateException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
} catch (IOException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaRecorder.release();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO
}
public void startRecording()
{
mediaRecorder.start();
}
public void stopRecording()
{
mediaRecorder.stop();
}
}
EDIT
LOGCAT OUTPUT
I/ActivityManager( 107): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=my.android.video/.VideoRecorder }
I/ActivityManager( 107): Start proc my.android.video for activity my.android.video/.VideoRecorder: pid=1534 uid=10098 gids={1015}
I/WindowManager( 107): Setting rotation to 1, animFlags=1
I/ActivityManager( 107): Config changed: { scale=1.0 imsi=310/120 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=18}
W/InputManagerService( 107): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4620e0d8
I/WindowManager( 107): Setting rotation to 0, animFlags=0
I/ActivityManager( 107): Config changed: { scale=1.0 imsi=310/120 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uiMode=17 seq=19}
I/UsageStats( 107): Unexpected resume of my.android.video while already resumed in my.android.video
I/WindowManager( 107): Setting rotation to 1, animFlags=0
I/ActivityManager( 107): Config changed: { scale=1.0 imsi=310/120 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=20}
I/UsageStats( 107): Unexpected resume of my.android.video while already resumed in my.android.video
E/AndroidRuntime( 1534): FATAL EXCEPTION: main
E/AndroidRuntime( 1534): java.lang.IllegalStateException
E/AndroidRuntime( 1534): at android.media.MediaRecorder.start(Native Method)
E/AndroidRuntime( 1534): at my.android.video.RecorderView.startRecording(RecorderView.java:62)
E/AndroidRuntime( 1534): at my.android.video.VideoRecorder$1.onClick(VideoRecorder.java:40)
E/AndroidRuntime( 1534): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime( 1534): at android.view.View$PerformClick.run(View.java:8817)
E/AndroidRuntime( 1534): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1534): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1534): at android.os.Looper.loop(Looper.java:144)
E/AndroidRuntime( 1534): at android.app.ActivityThread.main(ActivityThread.java:4937)
E/AndroidRuntime( 1534): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1534): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 1534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 1534): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 107): Force finishing activity my.android.video/.VideoRecorder
W/ActivityManager( 107): Activity pause timeout for HistoryRecord{462016f0 my.android.video/.VideoRecorder}
I/WindowManager( 107): Setting rotation to 0, animFlags=1
I/ActivityManager( 107): Config changed: { scale=1.0 imsi=310/120 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uiMode=17 seq=21}
D/PendingIntent( 236): PendingIntent getBroadcast Intent String =com.htc.htccalendarwidgets.MyWidgetView4110120
D/PendingIntent( 236): PendingIntent getBroadcast returnPendingIntent packageName =com.htc.launcher
V/AlarmManager( 107): Adding Alarm{4614d4d0 type 0 com.htc.launcher} Feb 24 12:00:00 am
I/WindowManager( 107): WIN DEATH: Window{462ec3b0 my.android.video/my.android.video.VideoRecorder paused=false}
I/ActivityManager( 107): Process my.android.video (pid 1534) has died.
W/ActivityManager( 107): Activity destroy timeout for HistoryRecord{462016f0 my.android.video/.VideoRecorder}
I am not able to figure out the problem as i am new to android programming.
The problem can be with the parameters you are passing to your preview surface. In the line:
holder.setFixedSize(400,300)
Try passing other parameters eg. (320, 240)
Also, make sure that the preview surface is accessible, before the call to setPreviewSurface is made.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With