尝试在 setOnTouchListener 上录制音频时应用程序崩溃
App is crashing when trying to record audio on setOnTouchListener
这个问题已经有人问过,但我找不到我的解决方案。
我正在做的是,OnTouchListener
的 MotionEvent.ACTION_DOWN
我开始录制音频,在 MotionEvent.ACTION_UP
我停止了音频录制。
在长按该图标期间,它工作正常,但 onclick
应用程序崩溃了。
这是我的
audio.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(Chat.this, "You Have to hold the Button for audio recording.", Toast.LENGTH_LONG).show();
}
});
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (mediaRecorder != null) {
mediaRecorder.stop();
}
}
return true;
}
});
public void MediaRecorderReady() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
这是我的问题,当我单击该按钮时,应用程序崩溃了
喜欢下面。
private MediaRecorder myAudioRecorder;
private MediaPlayer mPlayer;
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initializeRecorder();
recordFile();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
}
return true;
}
});
private void initializeRecorder() {
File cacheDir = new File(Utils.SD_CARD, Utils.RECORDING_CACHE);
if (!(cacheDir.exists())) {
cacheDir.mkdirs();
}
outputFile = cacheDir + "/" + String.format("%s.m4a", System.currentTimeMillis());
mPlayer = new MediaPlayer();
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//original
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
private void recordFile() {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording(){
myAudioRecorder.stop();
myAudioRecorder.release();
}
检查这个答案
Android mediarecorder stop failed
并阅读有关 mediaRecorder 及其状态的官方文档。
https://developer.android.com/reference/android/media/MediaRecorder.html
崩溃是因为你停止媒体播放器和媒体播放器不在录制状态..
非录音状态不要直接调用stop
try {
mRecorder.stop();
}
catch(RuntimeException e) {
}
finally {
mRecorder.release();
mRecorder = null;
}
这个问题已经有人问过,但我找不到我的解决方案。
我正在做的是,OnTouchListener
的 MotionEvent.ACTION_DOWN
我开始录制音频,在 MotionEvent.ACTION_UP
我停止了音频录制。
在长按该图标期间,它工作正常,但 onclick
应用程序崩溃了。
这是我的
audio.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(Chat.this, "You Have to hold the Button for audio recording.", Toast.LENGTH_LONG).show();
}
});
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (mediaRecorder != null) {
mediaRecorder.stop();
}
}
return true;
}
});
public void MediaRecorderReady() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
这是我的问题,当我单击该按钮时,应用程序崩溃了
喜欢下面。
private MediaRecorder myAudioRecorder;
private MediaPlayer mPlayer;
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
initializeRecorder();
recordFile();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
}
return true;
}
});
private void initializeRecorder() {
File cacheDir = new File(Utils.SD_CARD, Utils.RECORDING_CACHE);
if (!(cacheDir.exists())) {
cacheDir.mkdirs();
}
outputFile = cacheDir + "/" + String.format("%s.m4a", System.currentTimeMillis());
mPlayer = new MediaPlayer();
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//original
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
private void recordFile() {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording(){
myAudioRecorder.stop();
myAudioRecorder.release();
}
检查这个答案 Android mediarecorder stop failed 并阅读有关 mediaRecorder 及其状态的官方文档。 https://developer.android.com/reference/android/media/MediaRecorder.html 崩溃是因为你停止媒体播放器和媒体播放器不在录制状态.. 非录音状态不要直接调用stop
try {
mRecorder.stop();
}
catch(RuntimeException e) {
}
finally {
mRecorder.release();
mRecorder = null;
}