Android MediaRecorder 抛出 IllegalStateException
我正在尝试开发一个简单的 Android 录像机。为此,我使用 MediaRecoder
创建了一个自定义 SurfaceView
来处理录制。一切都构建得很好,并且在 Android 设备上也运行良好。但是当我调用 startRecording()
时,它给出了 IllegalStateException
。这是代码:
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();
}
}
编辑
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}
我无法找出问题,因为我是 Android 编程新手。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能出在您传递到预览表面的参数上。在该行中:
尝试传递其他参数,例如。 (320, 240)
另外,在调用 setPreviewSurface 之前,请确保预览表面可访问。
The problem can be with the parameters you are passing to your preview surface. In the line:
Try passing other parameters eg. (320, 240)
Also, make sure that the preview surface is accessible, before the call to setPreviewSurface is made.
尝试一下,
可能是设备不支持音频格式
try
may be device doesn't support audio format