My app records sound and plays it back. I have implemented a touch button for the recording part, and a double state click button for the audio reproduction. So far, so good.
If the user touches the recordBtn for an extended amount of time (couple of seconds), the MediaPlayer successfully records audio, and the playBtn will start reproducing the audio on click, while it will stop playing with a second click (unless the clip finishes and it will revert to its original state on its own with the onCompletionListener that is in place).
Now here is the problem:
if the user clicks on the touch button (recordBtn) instead of prolonged touching (about 2 seconds at least), then the MediaRecorder will fail to record, (that would normally crash the app but it will not due to the IOException that is in place).
The MediaRecorder is now null, if the user attempts to play audio via the playBtn, then the entire app becomes unresponsive until the user clicks on the playBtn again.
Here is the code:
I have tried using if statement checking "if(mPlayer != null){ ..." even though the app accesses the content of the if statement on run time, it is like completely ignoring the "else if" scenario.
//Play button actions
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mPlayer.release();
mPlayer = null;
playBtn.setEnabled(true);
recordBtn.setEnabled(true);
isPlaying = false;
} else {
recordBtn.setEnabled(false);
playBtn.setEnabled(true);
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
if(mPlayer != null) {
mPlayer.prepare();
mPlayer.start();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
isPlaying = false;
playBtn.setEnabled(true);
recordBtn.setEnabled(true);
}
});
}else if (mPlayer == null){
isPlaying = false;
mPlayer.release();
playBtn.setEnabled(true);
recordBtn.setEnabled(true);
}
} catch (IOException e) {
e.printStackTrace();
}
isPlaying = true;
}
}
});
//record button actions
recordBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if(isRecording) {
playBtn.setEnabled(true);
stopRecording();
isRecording = false;
}
}
else if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
if(!isRecording) {
playBtn.setEnabled(false);
startRecording();
isRecording = true;
}
}
return false;
}
});
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}
private void stopRecording() {
try {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
} catch(Exception e) {
e.printStackTrace();
}
}
logcat taken the moment playBtn is clicked and the app becomes unresponsive:
line 131 is the "mPlayer.prepare();"
2019-05-24 17:16:10.035 13373-13373/tk.gandriks.gaaudiotransform D/ViewRootImpl@ed74148[MainActivity]: ViewPostIme pointer 0
2019-05-24 17:16:10.092 13373-13373/tk.gandriks.gaaudiotransform D/ViewRootImpl@ed74148[MainActivity]: ViewPostIme pointer 1
2019-05-24 17:16:10.105 13373-13373/tk.gandriks.gaaudiotransform I/MediaPlayer: Need to enable context aware info
2019-05-24 17:16:10.105 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayer-JNI: native_setup
2019-05-24 17:16:10.105 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: constructor
2019-05-24 17:16:10.110 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: setListener
2019-05-24 17:16:10.115 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayer-JNI: setDataSourceFD: fd 89
2019-05-24 17:16:10.115 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: setDataSource(89, 0, 576460752303423487)
2019-05-24 17:16:10.121 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: setVideoSurfaceTexture
2019-05-24 17:16:10.122 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: prepare
2019-05-24 17:16:10.134 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: message received msg=300, ext1=0, ext2=0
2019-05-24 17:16:10.134 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: Received SEC_MM_PLAYER_CONTEXT_AWARE
2019-05-24 17:16:10.134 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: callback application
2019-05-24 17:16:10.134 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: back from callback
2019-05-24 17:16:10.135 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: message received msg=100, ext1=1, ext2=-2147483648
2019-05-24 17:16:10.135 13373-13442/tk.gandriks.gaaudiotransform E/MediaPlayerNative: error (1, -2147483648)
2019-05-24 17:16:10.135 13373-13442/tk.gandriks.gaaudiotransform V/MediaPlayerNative: signal application thread
2019-05-24 17:16:10.135 13373-13373/tk.gandriks.gaaudiotransform V/MediaPlayerNative: prepare complete - status=1
2019-05-24 17:16:10.135 13373-13373/tk.gandriks.gaaudiotransform W/System.err: java.io.IOException: Prepare failed.: status=0x1
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.media.MediaPlayer._prepare(Native Method)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.media.MediaPlayer.prepare(MediaPlayer.java:1441)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at tk.gandriks.gaaudiotransform.MainActivity$2.onClick(MainActivity.java:131)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.view.View.performClick(View.java:6897)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.widget.TextView.performClick(TextView.java:12693)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.view.View$PerformClick.run(View.java:26101)
2019-05-24 17:16:10.136 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.os.Looper.loop(Looper.java:164)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6944)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
2019-05-24 17:16:10.137 13373-13373/tk.gandriks.gaaudiotransform W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)