我正在尝试使用以下代码录制音频。
android-audio-recording-tutorial
当我打电话给开始记录时,它工作正常。
但是,当我打电话给停止记录时,它会抛出错误的信息,就像昏倒一样。
..............................................................
11-14 18:03:28.361: V/tag(23349): outgoing call ended
11-14 18:03:28.361: V/tag(23349): record.111.android.media.MediaRecorder@4052de80
11-14 18:03:28.371: E/MediaRecorder(23349): stop called in an invalid state: 1
11-14 18:03:28.371: D/AndroidRuntime(23349): Shutting down VM
11-14 18:03:28.371: W/dalvikvm(23349): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 18:03:28.381: E/AndroidRuntime(23349): FATAL EXCEPTION: main
11-14 18:03:28.381: E/AndroidRuntime(23349): java.lang.RuntimeException: Unable to start receiver com.example.Audio.CallsBrodcastReceiver: java.lang.IllegalStateException
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.os.Looper.loop(Looper.java:123)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 18:03:28.381: E/AndroidRuntime(23349): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 18:03:28.381: E/AndroidRuntime(23349): at java.lang.reflect.Method.invoke(Method.java:507)
11-14 18:03:28.381: E/AndroidRuntime(23349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 18:03:28.381: E/AndroidRuntime(23349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 18:03:28.381: E/AndroidRuntime(23349): at dalvik.system.NativeStart.main(Native Method)
11-14 18:03:28.381: E/AndroidRuntime(23349): Caused by: java.lang.IllegalStateException
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.media.MediaRecorder.stop(Native Method)
11-14 18:03:28.381: E/AndroidRuntime(23349): at com.example.Audio.AudioRecorder.stop(AudioRecorder.java:64)
11-14 18:03:28.381: E/AndroidRuntime(23349): at com.example.Audio.CallsBrodcastReceiver.onReceive(CallsBrodcastReceiver.java:147)
11-14 18:03:28.381: E/AndroidRuntime(23349): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
11-14 18:03:28.381: E/AndroidRuntime(23349): ... 10 more
如果有人知道解决方案,请帮助我。
提前致谢。
答案 0 :(得分:2)
try this code for start and stop recording of sounds in android.
public class SoundRecordingActivity extends Activity {
MediaRecorder recorder;
File audiofile = null;
private static final String TAG = "SoundRecordingActivity";
private View startButton;
private View stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = findViewById(R.id.start);
stopButton = findViewById(R.id.stop);
}
public void startRecording(View view) throws IOException {
startButton.setEnabled(false);
stopButton.setEnabled(true);
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
}
public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Add this permissions in Manifest And Let me know the status?