我的应用程序记录声音并播放。我为录音部分实现了触摸按钮,为音频再现实现了双重状态点击按钮。到目前为止,一切都很好。
如果用户长时间(几秒钟)触摸recordBtn,则MediaPlayer成功录制音频,并且playBtn将在单击时开始再现音频,而第二次单击时将停止播放(除非剪辑完成,它将通过适当的onCompletionListener自行恢复到原始状态。
现在这是问题所在:
如果用户单击触摸按钮(recordBtn)而不是长时间触摸(至少大约2秒),则MediaRecorder将无法记录(通常会导致应用程序崩溃,但不会由于IOException而导致到位)。
MediaRecorder现在为空,如果用户尝试通过playBtn播放音频,则整个应用程序将变得无响应,直到用户再次单击playBtn。
代码如下:
我尝试使用if语句检查“ if(mPlayer!= null){...” 即使应用程序在运行时访问if语句的内容,也就像完全忽略了“ else if”场景一样。
//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();
}
}
在单击playBtn时应用的logcat,应用变得无响应:
第131行是“ 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)