2

I'm trying to record a sound in java in android but the app crashes with this exception:

E/AndroidRuntime: FATAL EXCEPTION: Timer-0 Process: com.example.mathieu.telefony1, PID: 31353 java.lang.RuntimeException: setAudioSource failed.

But it is very strange, because I added it to the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application...

and here is my code to record a sound:

MediaRecorder mr = new MediaRecorder();
File file = new File(OUTPUT_FILE);
try {

    mr.setOutputFile(OUTPUT_FILE);
    mr.setAudioSource(MediaRecorder.AudioSource.MIC);
    mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mr.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mr.prepare();
    mr.start();
    Thread.sleep(1000);
    mr.stop();


} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}
  • Your device Os version ? – Nouman Ghaffar Feb 07 '17 at 11:54
  • Which Android version are you running this on, and what is your `targetSdkVersion`? – Michael Feb 07 '17 at 11:54
  • 1
    See this [duplicate](http://stackoverflow.com/a/33978961/4391450) if you should use a runtime permission – AxelH Feb 07 '17 at 11:54
  • Possible duplicate of [android mediaRecorder.setAudioSource failed](http://stackoverflow.com/questions/3782786/android-mediarecorder-setaudiosource-failed) – AxelH Feb 07 '17 at 11:54
  • Possible duplicate of [How to check Grants Permissions at Run-Time?](http://stackoverflow.com/questions/30549561/how-to-check-grants-permissions-at-run-time) – Nithinlal Feb 07 '17 at 11:56
  • i have Android 6.0.1 on Sony Xperia Z3 – Johnny Haliday Feb 07 '17 at 11:58
  • Ah I see, permission with Marshmallow and onward is tricky now, Please see for runtime permissions (as recommended by others). Declaring uses-permission is not merely enough. – A.B. Feb 07 '17 at 12:04
  • @JohnnyHaliday then check the two duplicates provided. You need to learn some thinks about Android permission ;) – AxelH Feb 07 '17 at 12:04
  • try this simplied permission library by Google https://github.com/googlesamples/easypermissions – Khaledonia Feb 07 '17 at 12:08

3 Answers3

2

Above API level 23 you will be given permission pragmatically like:

 private static final int PERMISSION_REQUEST_CODE = 1;

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

    if (checkSelfPermission(Manifest.permission.SEND_SMS)
            == PackageManager.PERMISSION_DENIED) {

        Log.d("permission", "permission denied to SEND_SMS - requesting it");
        String[] permissions = {Manifest.permission.SEND_SMS};

        requestPermissions(permissions, PERMISSION_REQUEST_CODE);

    }
}
user7176550
  • 1,501
  • 14
  • 19
  • Please use this call back on your activity *onRequestPermissionsResult* to check if you have received the permission or not. You have to design a fallback mechanism if the user denies the permission – A.B. Feb 07 '17 at 12:06
  • This is incomplet. You should check this [answer](http://stackoverflow.com/a/30549756/4391450) too ;) – AxelH Feb 07 '17 at 12:07
  • i know but you can give permission using this code. – user7176550 Feb 07 '17 at 12:12
  • Ask for permission ;) yes but you should have precise that this use a callback method to get the answer of the user – AxelH Feb 08 '17 at 12:26
0

Check The For Run Time Permission

With Devices Higher OS than Android L Some Extra Features Are Available For Security Purpose. You Have To Check Some Risky Permission Again At Run time If They Are Not Given By User Then You Have To Ask To Grant It.

For More Look At Requesting Permission At Run Time

Abhishek
  • 3,348
  • 3
  • 15
  • 34
0

Officially from Android documentation :

Caution: You must either catch or pass IllegalArgumentException and IOException when using setDataSource(), because the file you are referencing might not exist

Make sure that the audio file exist and both Runtime Permission (For Api 23+) and Manifest permission is declared.

Basu
  • 763
  • 8
  • 27