android mediaRecorder.setAudioSource失败

我有Android G1固件1.6,我试图用以下代码记录应用程序的声音。

MediaRecorder recorder = new MediaRecorder();
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 recorder.setOutputFile(PATH_NAME);
 recorder.prepare();
 recorder.start();  

我的manifest.xml有:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

我有:

09-23 14:41:05.531: ERROR/AndroidRuntime(1718): Uncaught handler: thread main exiting due to uncaught exception
09-23 14:41:05.551: ERROR/AndroidRuntime(1718): java.lang.RuntimeException: setAudioSource failed.
09-23 14:41:05.551: ERROR/AndroidRuntime(1718):     at android.media.MediaRecorder.setAudioSource(Native Method)

如何正确录制语音?


打开“ AndroidManifest.xml ” - >

<uses-permission android:name="android.permission.RECORD_AUDIO" />

我知道这是一个非常古老的问题,但在Android Marshmallow中,您必须继续“设置>应用程序>您的应用程序>权限”并且可以使用麦克风权限。


如果您在Android M上运行,那么您需要申请权限才能在第一次运行时录制音频。 要做到这一点,请询问用户是否可以在应用程序启动时录制音频:

private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 29;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (mContext.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        Log.d("Home", "Already granted access");
        initializeView(v);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("Home", "Permission Granted");
                initializeView(v);
            } else {
                Log.d("Home", "Permission Failed");
                Toast.makeText(getActivity().getBaseContext(), "You must allow permission record audio to your mobile device.", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
        }
        // Add additional cases for other permissions you may have asked for
    }
}
链接地址: http://www.djcxy.com/p/5255.html

上一篇: android mediaRecorder.setAudioSource failed

下一篇: Recording Live OpenCV Processing on Android