这是自我解释的。 mediarecorder类的start方法在调用它的第二个时抛出非法状态异常。我第一次按下按钮一切正常。第二次没有。我解决了很多问题,并且发现了什么是错的。我尝试了有关媒体录像机的其他问题的解决方案,但他们没有帮助。权限位于清单中。所以这不是问题,因为它第一次起作用。
MAIN
package com.boomer.omer.notetoself;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class Activity_Main extends Activity implements View.OnTouchListener {
ProgressBar progressBar_record;
Button button_record;
Recorder recorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar_record = (ProgressBar)findViewById(R.id.progressBar_recorder);
button_record = (Button)findViewById(R.id.button_record);
button_record.setOnTouchListener(this);
recorder = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.button_record:
if(event.getAction() == MotionEvent.ACTION_DOWN){
button_record.setText("RECORDING");
startRecorder();
}else if (event.getAction() == MotionEvent.ACTION_UP){
button_record.setText("RECORD");
stopRecorder();
}
break;
}
return false;
}
public void startRecorder(){
recorder = new Recorder(this,progressBar_record);
recorder.execute();
}
public void stopRecorder(){
progressBar_record.setProgress(0);
recorder.cancel(true);
recorder = null;
}
}
RECORDER CLASS
public class Recorder extends AsyncTask<Void,Integer,Void> {
public static final String LOG_TAG = "NTS:";
static final int MAX_LENGTH = 30;
MediaRecorder mRecorder;
String mFileName = null;
ProgressBar recordBar;
Activity parentActivity;
int mLength = 0;
public Recorder(Activity activity,ProgressBar progressBar) {
super();
mFileName = activity.getFilesDir().getAbsolutePath();
mFileName += "/recentRecord.3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recordBar = progressBar;
parentActivity =activity;
}
public void startRecording(){
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
public void stopRecording(){
mRecorder.stop();
mRecorder.reset();
mRecorder.release();
mRecorder = null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
startRecording();
}
@Override
protected Void doInBackground(Void... params) {
long secondMillis = System.currentTimeMillis();
while(mLength < MAX_LENGTH){
if((System.currentTimeMillis() - secondMillis) >= 1000){
secondMillis = System.currentTimeMillis();
mLength++;
publishProgress(mLength);
}
}
stopRecording();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
stopRecording();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
recordBar.setProgress(values[0]);
}
@Override
protected void onCancelled(Void aVoid) {
super.onCancelled(aVoid);
stopRecording();
}
@Override
protected void onCancelled() {
super.onCancelled();
stopRecording();
}
}