我正在嘗試使用媒體錄製器錄製語音。 現在我創建新的介質記錄,然後我嘗試使用準備和啓動都無濟於事,我總是收到以下錯誤:爲什麼mediaRecorder在嘗試錄製語音時無法準備
09-04 09:27:36.335: E/MediaRecorder(26320): prepare failed: -17
09-04 09:27:36.335: W/System.err(26320): java.io.IOException: prepare failed.
09-04 09:27:36.350: W/System.err(26320): at android.media.MediaRecorder._prepare(Native Method)
09-04 09:27:36.350: W/System.err(26320): at android.media.MediaRecorder.prepare(MediaRecorder.java:595)
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.AudioRecorder.start(AudioRecorder.java:57)
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.recordCall(ServiceFloatingButton.java:321)
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.handlePressAction(ServiceFloatingButton.java:254)
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton.access$1(ServiceFloatingButton.java:217)
09-04 09:27:36.350: W/System.err(26320): at com.abg.quickies.ServiceFloatingButton$1.onTouch(ServiceFloatingButton.java:173)
09-04 09:27:36.350: W/System.err(26320): at android.view.View.dispatchTouchEvent(View.java:3934)
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2218)
09-04 09:27:36.355: W/System.err(26320): at android.view.ViewRoot.handleMessage(ViewRoot.java:1889)
09-04 09:27:36.360: W/System.err(26320): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 09:27:36.360: W/System.err(26320): at android.os.Looper.loop(Looper.java:123)
09-04 09:27:36.360: W/System.err(26320): at android.app.ActivityThread.main(ActivityThread.java:3691)
09-04 09:27:36.360: W/System.err(26320): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 09:27:36.365: W/System.err(26320): at java.lang.reflect.Method.invoke(Method.java:507)
09-04 09:27:36.365: W/System.err(26320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
09-04 09:27:36.365: W/System.err(26320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
09-04 09:27:36.370: W/System.err(26320): at dalvik.system.NativeStart.main(Native Method)
的代碼如下:
private void recordCall()
{
Parameters params=getMyApplication().get_parameters();
Date date=new Date();
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String fileName=formater.format(date);
String path=params.getRecordsPath()+fileName;
//record
set_recorder(new AudioRecorder(path));
try {
get_recorder().start();
set_isRecording(true);
showRecorderNotification();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
錄音機:
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
private int _audioSource;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
* default audio source is voicecall
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
set_audioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
Log.i("GABI", "AudioRecorder- start()");
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(get_audioSource());
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
Log.i("GABI", "AudioRecorder- stop()");
recorder.stop();
recorder.reset();
recorder.release();
}
public int get_audioSource() {
return _audioSource;
}
public void set_audioSource(int _audioSource) {
this._audioSource = _audioSource;
}
}
這是爲什麼?
<使用許可權的android:NAME = 「android.permission.WRITE_EXTERNAL_STORAGE」/> />和<使用許可權的android:NAME = 「android.permission.RECORD_AUDIO」/>在你的Manifest.xml完成 – jenuine
@jenuine是的,在那裏,正在創建一個文件,但它總是空的。 –
檢查你到達這裏的路徑。 'recorder.setOutputFile(path);' –