当我第一次录制这个工作正常但是第二次开始录制时停止然后录制然后它崩溃然后给出 java.lang.IllegalStateException
我不知道为什么
我的代码是
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);
}
}
我的日志
java.lang.IllegalStateException
at android.media.MediaRecorder.stop(Native Method)
at user.example.com.myApp.MainActivity$2.onClick(MainActivity.java:95)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
请帮助我
答案 0 :(得分:1)
因为你在null
对象上调用stop。您应该在停止之前检查state
的{{1}}
答案 1 :(得分:1)
根据official example from google,您需要在停止录制时执行以下操作。
<table ng-controller="MyCtrlr">
<tr>
<td>nid</td>
<td>title</td>
<td>field_event_list</td>
<td>event_date</td>
</tr>
<tr ng-repeat="data in jsonResp">
<td>{{data.nid}}
</td>
<td>{{data.title | getPlaintext}}</td>
<td>{{data.field_event_list | getPlaintext}}
</td>
<td>{{data.event_date | getPlaintext}}
</td>
</tr>
</table>
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
看起来像,
releaseMediaRecorder()