2
我想開發一個簡單的android錄像機。爲此我創建了一個自定義SurfaceView
與MediaRecoder
來處理錄音。一切都很好,它也可以在Android設備上正常運行。但是當我打電話給startRecording()
時,它給我IllegalStateException
。下面是代碼:Android MediaRecorder拋出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的輸出
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: [email protected]
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編程。
你能告訴我們你的logcat嗎? – prolink007
看起來你是在我打電話準備之前打電話給我的。 –
不,我打電話後準備開始。 –