android mediaRecorder.setAudioSource failed

2019-01-07 17:15发布

I have android G1 firmware 1.6, I am trying to record voice from the app with the follow code.

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();  

my manifest.xml has:

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

I got:

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)

how do I record voice properly?

15条回答
Root(大扎)
2楼-- · 2019-01-07 18:07

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-07 18:08

As a Cyanogenmod-User I had to

  1. make sure the permissions are defined in the manifest (android-studio)
  2. make sure the privacy guard allowed the app to access the microphone (on the device)
  3. make sure the android system had set the permissions under Settings --> Apps --> Select your app --> Permissions --> Check all permissions (should be the ones requested by the manifest) (on the device)

After that recoding worked for me.

查看更多
ら.Afraid
4楼-- · 2019-01-07 18:10

Open "AndroidManifest.xml" ->

add

<uses-permission android:name="android.permission.RECORD_AUDIO" />
查看更多
贼婆χ
5楼-- · 2019-01-07 18:12

try this code it helps you a lot and its understandable .....

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;
    }
}
查看更多
够拽才男人
6楼-- · 2019-01-07 18:15

IMPORTANT - during the last few hours I tried to figure out how to check if the MIC is caught by a different application or not. I noticed that if 2 application address to MediaRecorder at the same time a RuntimeException will be tossed and you won't be able to use the mic, unless you restart your device (!!) I don't know if it's the best solution but it worked for me. perhaps it will save some of you a few hours some day..

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;
    }
}
查看更多
Root(大扎)
7楼-- · 2019-01-07 18:15

Change

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

to

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

Worked for me.

查看更多
登录 后发表回答