3

我正在尝试开发一个简单的安卓录像机。为此,我创建了一个自定义来SurfaceView处理MediaRecoder录音。一切都很好,并且在 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 输出

    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 编程新手。

4

2 回答 2

1

问题可能出在您传递给预览图面的参数上。在行中:

holder.setFixedSize(400,300)

尝试传递其他参数,例如。(320, 240)

此外,请确保在调用 setPreviewSurface 之前可以访问预览表面。

于 2012-10-16T06:56:05.923 回答
0

尝试

mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

可能是设备不支持音频格式

于 2018-06-26T12:56:42.843 回答