-2

每当我单击按钮 1 时,应用程序就会崩溃(强制关闭)。我无法弄清楚原因。将目录更改为 Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.3gp" 也不起作用。代码是:

package com.example.newaa;
import java.io.IOException;
import android.os.Environment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

import android.os.Bundle;

public class Record extends Activity{
    Button btn1,btn2;
    MediaRecorder recorder;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.record);
            btn1=(Button)findViewById(R.id.button1);
            btn2=(Button)findViewById(R.id.button2);
            btn1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    recorder = new MediaRecorder();

                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.3gp");
                    //recorder.setOutputFile("file:///sdcard/sample.3gp");

                //  recorder.setOnErrorListener(errorListener);
                    //recorder.setOnInfoListener(infoListener);

                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    recorder.stop();
                    recorder.reset();
                    recorder.release();

                    recorder = null;
                }
            });
     }

}

日志猫是

07-05 11:36:35.050: E/AndroidRuntime(220): Uncaught handler: thread main exiting due to 
uncaught exception
07-05 11:36:35.070: E/AndroidRuntime(220): java.lang.RuntimeException: setAudioSource failed.
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.media.MediaRecorder.setAudioSource(Native Method)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.example.newaa.Record$1.onClick(Record.java:56)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.View.performClick(View.java:2364)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.View.onTouchEvent(View.java:4179)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.View.dispatchTouchEvent(View.java:3709)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.os.Looper.loop(Looper.java:123)
07-05 11:36:35.070: E/AndroidRuntime(220):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 11:36:35.070: E/AndroidRuntime(220):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 11:36:35.070: E/AndroidRuntime(220):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 11:36:35.070: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 11:36:35.070: E/AndroidRuntime(220):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

您是否在模拟器上尝试过,如果是,我建议您在真实设备上尝试一下。可能只是因为模拟器。因为我很久以前在模拟器上遇到过这样的问题,但在电话上它工作正常

清单中缺少权限>>音频权限可能是另一个原因。

于 2012-07-05T06:15:59.803 回答
0

我明白了原因。我想这太容易了:P

我错过了清单文件中的权限

于 2012-07-05T06:19:23.047 回答