69

我有 android G1 固件 1.6,我正在尝试使用以下代码从应用程序录制语音。

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

我的 manifest.xml 有:

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

我有:

09-23 14:41:05.531:错误/AndroidRuntime(1718):未捕获的处理程序:线程主因未捕获的异常而退出
09-23 14:41:05.551: 错误/AndroidRuntime(1718): java.lang.RuntimeException: setAudioSource 失败。
09-23 14:41:05.551:错误/AndroidRuntime(1718):在 android.media.MediaRecorder.setAudioSource(本机方法)

如何正确录制语音?

4

17 回答 17

126

打开“ AndroidManifest.xml”->

添加

<uses-permission android:name="android.permission.RECORD_AUDIO" />
于 2012-12-28T06:19:43.493 回答
41

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

于 2015-11-18T00:10:27.737 回答
13

如果您在 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
    }
}
于 2015-11-29T03:04:35.457 回答
6

<uses-permission android:name="android.permission.RECORD_AUDIO" />对于 API 23+,检查 Manifest.xml和 Java 代码中的录音权限

if (ActivityCompat.checkSelfPermission(cnt, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(cnt, arrayOf(
                Manifest.permission.RECORD_AUDIO
        ),0)
        return
    }
于 2017-10-19T17:57:05.500 回答
4

重要 - 在过去的几个小时里,我试图弄清楚如何检查 MIC 是否被其他应用程序捕获。我注意到,如果同时向 MediaRecorder 发送 2 个应用程序地址,则会抛出 RuntimeException 并且您将无法使用麦克风,除非您重新启动设备(!!)我不知道这是否是最好的解决方案,但是它对我有用。也许有一天它会为你们中的一些人节省几个小时..

private void validateMicAvailability() throws MicUnaccessibleException {
    AudioRecord recorder =
        new AudioRecord(AudioSource.MIC, 44100,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_DEFAULT, 44100);
    try{
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
            throw new MicUnaccessibleException("Mic didn't successfully initialized");
        }

        recorder.startRecording();
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
            recorder.stop();
            throw new MicUnaccessibleException("Mic is in use and can't be accessed");
        }
        recorder.stop();
    } finally{
        recorder.release();
        recorder = null;
    }
}
于 2012-05-20T15:49:56.880 回答
4

我有这个确切的问题,这为我解决了它:

在您用于调试的设备上,转到设置 -> 应用程序 -> {您的应用程序}。然后确保允许所有应用程序的权限(在本例中为音频录制和写入外部存储)。

显然,在不通过 Google Play 商店安装应用程序时,您不会被要求授予权限,并且在某些手机上,这将导致您的调试应用程序无法获得某些权限。

即使清单文件中的某些权限被识别(例如互联网访问),也要检查这一点,因为对我来说有些工作有效,有些被自动阻止。

希望这可以帮助。

于 2015-11-02T16:50:10.247 回答
4

对于 API 23+,您需要请求读/写权限,即使它们已经在您的清单中。

// Check for permissions
int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

// If we don't have permissions, ask user for permissions
if (permission != PackageManager.PERMISSION_GRANTED) {
    String[] PERMISSIONS_STORAGE = {
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    }; 
    int REQUEST_EXTERNAL_STORAGE = 1;

    ActivityCompat.requestPermissions(
        getActivity(),
        PERMISSIONS_STORAGE,
        REQUEST_EXTERNAL_STORAGE
    );
}
于 2016-08-15T11:22:21.797 回答
2

Manifest.permission.RECORD_AUDIO动态请求 Marshmallow 及以上版本的权限。

于 2016-08-01T12:21:56.853 回答
1

这看起来是正确的。确保其他其他应用程序尚未使用 MIC。也许让手机重新启动,然后再试一次。

于 2010-09-24T14:12:22.420 回答
1

这段代码对我有用

 mRecorder = new MediaRecorder();
 mRecorder.reset();
 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
于 2014-04-21T05:58:17.353 回答
1

试试这段代码,它对你有很大帮助,而且可以理解......

public class MainActivity extends Activity {

    MediaRecorder mRecorder;
    MediaPlayer mediaPlayer;
    Button start, stop, play,stop_play;
    String FileName="";
    File outfile ;

    public static final int request_code = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        start=(Button)findViewById(R.id.btn_start_main);
        stop=(Button)findViewById(R.id.btn_stop_main);
        play=(Button)findViewById(R.id.btn_play_main);
        stop_play=(Button)findViewById(R.id.btn_playstop_main);

        if (checkPermissionFromDevice()){

            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FileName= Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+
                            UUID.randomUUID()+"AudioFile.3gp";

                    SetupMediaRecorder();

                    try {
                        mRecorder.prepare();
                        mRecorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    start.setEnabled(false);
                    stop.setEnabled(true);
                    play.setEnabled(false);
                    stop_play.setEnabled(false);

                    Toast.makeText(getApplicationContext(),"recording....",Toast.LENGTH_SHORT).show();
                }
            });
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRecorder.stop();
                    play.setEnabled(true);
                    start.setEnabled(true);
                    stop.setEnabled(false);
                    stop_play.setEnabled(false);
                    Toast.makeText(getApplicationContext(),"recording stoped....",Toast.LENGTH_SHORT).show();
                }
            });
            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer=new MediaPlayer();
                    try {
                        mediaPlayer.setDataSource(FileName);
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                    start.setEnabled(false);
                    stop.setEnabled(false);
                    play.setEnabled(false);
                    stop_play.setEnabled(true);
                    Toast.makeText(getApplicationContext(),"playing..",Toast.LENGTH_SHORT).show();
                }
            });
            stop_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer.stop();
                    SetupMediaRecorder();
                    start.setEnabled(true);
                    stop_play.setEnabled(false);
                    play.setEnabled(false);
                }
            });

        }
        else {

            requestPermissionFromDevice();
        }





    }

    private void SetupMediaRecorder() {
        mRecorder=new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(FileName);
    }

    private void requestPermissionFromDevice() {
        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO},
                request_code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case request_code:
            {
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ){
                    Toast.makeText(getApplicationContext(),"permission granted...",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"permission denied...",Toast.LENGTH_SHORT).show();
                }
            }
                break;
        }
    }

    private boolean checkPermissionFromDevice() {
        int storage_permission= ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int recorder_permssion=ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
    return storage_permission == PackageManager.PERMISSION_GRANTED && recorder_permssion == PackageManager.PERMISSION_GRANTED;
    }
}
于 2018-02-28T07:29:21.777 回答
1

我把它移到另一个线程。一切正常。

这是一个代码:

new Thread(() -> {
    mediaRecorder.reset();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setOutputFile("/audiorecordtest.3gp");
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            //do something here
            mediaRecorder.stop();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("Audio","unable to prepare");
        }

}).start();
于 2018-03-06T16:11:10.653 回答
1

作为 Cyanogenmod 用户,我必须

  1. 确保在清单中定义了权限(android-studio)
  2. 确保隐私卫士允许应用访问麦克风(在设备上)
  3. 确保 android 系统已在Settings--> Apps--> 选择您的应用 --> Permissions--> 检查所有权限(应该是清单请求的权限)下设置了权限(在设备上)

在那之后,重新编码对我有用。

于 2016-10-01T14:21:02.653 回答
0

My suggestion might look stupid, but it worked for me :)

Try giving permission as a nested tag. I believe there is an xml parsing issue somewhere with android packaging library.

于 2011-01-17T05:27:40.807 回答
0

它对我有用:-

在 setContentView(R.layout.activity_main) 之后请求许可

val 权限 = arrayOf(Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE) ActivityCompat.requestPermissions(this, permissions,0)

于 2021-09-21T20:12:16.447 回答
-1

确保 android 系统已经在设备上设置了权限,应该是清单请求的权限:

  • 在设置->应用程序->选择您的应用程序->权限->检查所有权限
于 2019-02-26T17:58:12.140 回答
-16

改变

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

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

为我工作。

于 2013-03-23T02:26:22.427 回答