I am having a couple of problems recording sound on a device. The code I am using if from the android dev site (Site Link) and is as follows:
public void onClickStart(View v) throws IllegalStateException, IOException{
startRecord();
}
public void onClickStop(View v) throws IllegalStateException, IOException{
stopRecord();
}
private void startRecord() throws IllegalStateException, IOException{
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator?
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/test.3gpp");
recorder.prepare();
recorder.start();
}
private void stopRecord(){
recorder.stop();
// recorder.release();
}
With 2 buttons in the main layout which it turn both stop and start the recording (in theory that is).
But from LogCat when trying this out on my device (really cant be bothered with trying on the emulator) I get the following errors:
Error 1:
ERROR/MediaRecorder(14541): start called in an invalid state: 4
java.lan开发者_高级运维g.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.reflect.InvocationTargetException
Error 2:
Caused by: java.io.FileNotFoundException: /sdcard/test.3gpp (Permission denied)
And I also have the following permissions set in my Manifest.xml file:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Silly Error here sorry, it didn't work because I had the phone mounted on my laptop so it couldn't actually reach the sdcard.
For error 2: try to use getExternalStorageDirectory()
instead of "sdcard" statically, maybe that path is not valid/reachable on your device.
To others that stumble upon this, the exception does not always tell you that the FileNotFoundException is associated with a denial of permission. Check that the permission WRITE_EXTERNAL_STORAGE exists on your AndroidManifest.xml. In the OP's case, it did.
Even if you create an external SD-Card file (using the mksdcard command) and associate it with your phone, the call to getExternalStorageDirectory() may still point to /sdcard (since the method's contract does not guarantee that it will return a non-internal storage location ). You can always use the 'adb shell' command to check if sdcard does exist. If you want to copy files to / from the sdcard, use the 'adb pull' / 'adb push' commands.
References:
http://developer.android.com/guide/developing/devices/emulator.html#sdcard
How to copy and edit files in Android shell?
writing to sdcard not working
Don't hardcore /sdcard/test.3gpp
in output file, you should use getExternalStorage()
use follow code:
boolean exists = (new File(android.os.Environment.getExternalStorageDirectory() + "/Record/")).exists();
if (!exists) {
new File(android.os.Environment.getExternalStorageDirectory() + "/Record/").mkdirs();
}
and set this path to output file
recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp");
It's works for me
I'll bet some people are missing write permissions in the manifest.
Within the main manifest tag, please ensure you have the following permissions :
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
精彩评论