Android 第二次停止录制时应用程序崩溃
当我第一次录制时,在录制中工作此工作正常,但第二次开始录制,当停止录制时,它崩溃并给出java.lang.IllegalStateException 我不明白为什么 我的代码是Android 第二次停止录制时应用程序崩溃,android,Android,当我第一次录制时,在录制中工作此工作正常,但第二次开始录制,当停止录制时,它崩溃并给出java.lang.IllegalStateException 我不明白为什么 我的代码是 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); session = new SessionM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
String name = user.get(SessionManager.KEY_NAME);
String email = user.get(SessionManager.KEY_EMAIL);
// outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
_Start = (Button)findViewById(R.id.start);
_Stop =(Button)findViewById(R.id.stop);
_Send = (Button)findViewById(R.id.sendbtn);
_Play =(Button)findViewById(R.id.play);
_CountTimer =(TextView)findViewById(R.id.timer_view);
_ImgView =(ImageView)findViewById(R.id.imgView);
boolean exists = (new File(path)).exists();
if(!exists) {
new File(path).mkdirs();
}
else {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mRecorder.setOutputFile(path + audioname);
}
_CountTimer.setText("00:30");
final GumzoCounterClass timer = new GumzoCounterClass(30000, 1000);
_Start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
timer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
_ImgView.setVisibility(View.VISIBLE);
_Start.setVisibility(View.INVISIBLE);
_Play.setVisibility(View.INVISIBLE);
_Stop.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Start Recording", Toast.LENGTH_SHORT).show();
}
});
_Stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_ImgView.setVisibility(View.INVISIBLE);
_Start.setVisibility(View.VISIBLE);
_Stop.setVisibility(View.INVISIBLE);
_Play.setVisibility(View.VISIBLE);
mRecorder.stop();
timer.cancel();
_CountTimer.setText("Stoped Recording");
Toast.makeText(getApplicationContext(), "Stop Recording", Toast.LENGTH_SHORT).show();
}
});
_Play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer m = new MediaPlayer();
// _Start.setEnabled(false);
try {
m.setDataSource(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
m.prepare();
} catch (IOException e) {
e.printStackTrace();
}
m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
}
});
}
public class GumzoCounterClass extends CountDownTimer {
public GumzoCounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
_CountTimer.setText("Completed");
mRecorder.stop();
mRecorder.release();
}
@Override
public void onTick(long millisUntilFinished) {
_CountTimer.setText((millisUntilFinished / 1000) + "");
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logout:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null){
LoginManager.getInstance().logOut();
startActivity(new Intent(getApplicationContext(),Login_Activity.class));
finish();
}
Toast.makeText(MainActivity.this, "Logout User", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
请帮助我这是因为您正在对
null
对象调用stop。在停止录制之前,您应该根据停止录制时需要执行的操作检查MediaRecorder的状态
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
并且releaseMediaRecorder()
看起来像
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
// clear recorder configuration
mMediaRecorder.reset();
// release the recorder object
mMediaRecorder.release();
mMediaRecorder = null;
}
}
我们可以吃logcat吗@NiravRanpara现在我正在更新logcatetahnks,但第一次它是如何正常工作的。我不明白,请尝试初始化mRecorder=newMediaRecorder();内一次点击_开始
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
// clear recorder configuration
mMediaRecorder.reset();
// release the recorder object
mMediaRecorder.release();
mMediaRecorder = null;
}
}