Я работаю над этим приложением и хочу записывать звук при нажатии кнопки и сохранять этот звук в хранилище устройства. Мое приложение аварийно завершает работу, когда я пытаюсь записать звук, хотя каталог во внутреннем хранилище записывается для того места, где сохраняется звук. Это мой код:
public class Recording extends AppCompatActivity { //<--- line 20
private Button mRecord;
private MediaRecorder mRecorder;
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private String mFileName = null;
private static final String LOG_TAG = "Recorder";
private TextView mrecord_text;
private File file;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
mRecord = (Button)findViewById(R.id.record);
mrecord_text = (TextView)findViewById(R.id.record_text);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/RecorderfX");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
mFileName = "/VM"+ n +"rscx.3gpp";
file = new File (myDir, mFileName);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
mRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getAction() == motionEvent.ACTION_DOWN)
{
startRecording(); //<-- line 79
mrecord_text.setText("Recording started...");
}
else if(motionEvent.getAction() == MotionEvent.ACTION_UP)
{
stopRecording();
mrecord_text.setText("Recording stopped...");
}
return false;
}
});
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start(); //<-- line 111
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
Это моя трассировка стека, и когда я смотрю на строки, предположительно ошибочные, все выглядит просто отлично. Что я могу делать не так?
05-12 11:39:41.937 4240-4240/com.recorderfx.recorderfx E/
AndroidRuntime:
FATAL EXCEPTION: main
Process: com.recorderfx.recorderfx, PID: 4240
java.lang.IllegalStateException
at
android.media.MediaRecorder.start(Native Method)
at
com.recorderfx.recorderfx.Recording.startRecording(Recording.java:111)
at
com.recorderfx.recorderfx.Recording.access$000(Recording.java:20)
at
com.recorderfx.recorderfx.Recording$1.onTouch(Recording.java:79)
at
android.view.View.dispatchTouchEvent(View.java:8802)
at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2196)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2607)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1779)
at android.app.Activity.dispatchTouchEvent(Activity.java:2846)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2568)
at android.view.View.dispatchPointerEvent(View.java:9003)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4209)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4072)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3763)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5910)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5884)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5855)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6000)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)