我是android开发的新手。当我在我的服务类中录制一个调用时,recorder.start()正在获取IllegalStateException。这是我的Logcat:
java.lang.IllegalStateException.android.media.MediaRecorder.stop(Native Method)
我的服务课程是: -
公共类RecordService扩展了服务{
private MediaRecorder recorder = null;
private String phoneNumber = null;
private String fileName;
private boolean onCall = false;
private boolean recording = false;
private boolean silentMode = false;
private boolean onForeground = false;
private int currentFormat = 0;
private String AUDIO_RECORDER_FOLDER = "Testing_recording";
String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP3,
AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
static final String AUDIO_RECORDER_FILE_EXT_MP3 = ".mp3";
static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,
MediaRecorder.OutputFormat.THREE_GPP };
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Constants.TAG, "RecordService onStartCommand");
if (intent != null) {
int commandType = intent.getIntExtra("commandType", 0);
if (commandType != 0) {
if (commandType == Constants.RECORDING_ENABLED) {
Log.d(Constants.TAG, "RecordService RECORDING_ENABLED");
silentMode = intent.getBooleanExtra("silentMode", true);
if (!silentMode && phoneNumber != null && onCall
&& !recording)
commandType = Constants.STATE_START_RECORDING;
} else if (commandType == Constants.RECORDING_DISABLED) {
Log.d(Constants.TAG, "RecordService RECORDING_DISABLED");
silentMode = intent.getBooleanExtra("silentMode", true);
if (onCall && phoneNumber != null && recording)
commandType = Constants.STATE_STOP_RECORDING;
}
if (commandType == Constants.STATE_INCOMING_NUMBER) {
Log.d(Constants.TAG, "RecordService STATE_INCOMING_NUMBER");
startService();
if (phoneNumber == null)
phoneNumber = intent.getStringExtra("phoneNumber");
silentMode = intent.getBooleanExtra("silentMode", true);
} else if (commandType == Constants.STATE_CALL_START) {
Log.d(Constants.TAG, "RecordService STATE_CALL_START");
onCall = true;
if (phoneNumber != null && onCall
&& !recording) {
startService();
startRecording(intent);
}
} else if (commandType == Constants.STATE_CALL_END) {
Log.d(Constants.TAG, "RecordService STATE_CALL_END");
onCall = false;
phoneNumber = null;
stopAndReleaseRecorder();
recording = false;
stopService();
} else if (commandType == Constants.STATE_START_RECORDING) {
Log.d(Constants.TAG, "RecordService STATE_START_RECORDING");
if ( phoneNumber != null && onCall) {
startService();
startRecording(intent);
}
} else if (commandType == Constants.STATE_STOP_RECORDING) {
Log.d(Constants.TAG, "RecordService STATE_STOP_RECORDING");
stopAndReleaseRecorder();
recording = false;
}
}
}
return super.onStartCommand(intent, flags, startId);
}
/**
* in case it is impossible to record
*/
private void terminateAndEraseFile() {
Log.d(Constants.TAG, "RecordService terminateAndEraseFile");
stopAndReleaseRecorder();
recording = false;
// deleteFile();
}
private void stopService() {
Log.d(Constants.TAG, "RecordService stopService");
stopForeground(true);
onForeground = false;
this.stopSelf();
}
private void deletseFile() {
Log.d(Constants.TAG, "RecordService deleteFile");
FileHelper.deleteFile(fileName);
fileName = null;
}
private void stopAndReleaseRecorder() {
if (recorder == null)
return;
Log.d(Constants.TAG, "RecordService stopAndReleaseRecorder");
boolean recorderStopped = false;
boolean exception = false;
try {
recorder.stop();
recorderStopped = true;
} catch (IllegalStateException e) {
Log.e(Constants.TAG, "IllegalStateException");
e.printStackTrace();
exception = true;
} catch (RuntimeException e) {
Log.e(Constants.TAG, "RuntimeException");
exception = true;
} catch (Exception e) {
Log.e(Constants.TAG, "Exception");
e.printStackTrace();
exception = true;
}
try {
recorder.reset();
} catch (Exception e) {
Log.e(Constants.TAG, "Exception");
e.printStackTrace();
exception = true;
}
try {
recorder.release();
} catch (Exception e) {
Log.e(Constants.TAG, "Exception");
e.printStackTrace();
exception = true;
}
recorder = null;
if (exception) {
// deleteFile();
}
if (recorderStopped) {
Toast toast = Toast.makeText(this,
this.getString(R.string.receiver_end_call),
Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onDestroy() {
Log.d(Constants.TAG, "RecordService onDestroy");
stopAndReleaseRecorder();
stopService();
super.onDestroy();
}
private void startRecording(Intent intent) {
Log.d(Constants.TAG, "RecordService startRecording");
boolean exception = false;
recorder = new MediaRecorder();
try {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// fileName = FileHelper.getFilename(phoneNumber);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
OnErrorListener errorListener = new OnErrorListener() {
public void onError(MediaRecorder arg0, int arg1, int arg2) {
Log.e(Constants.TAG, "OnErrorListener " + arg1 + "," + arg2);
terminateAndEraseFile();
}
};
recorder.setOnErrorListener(errorListener);
OnInfoListener infoListener = new OnInfoListener() {
public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
Log.e(Constants.TAG, "OnInfoListener " + arg1 + "," + arg2);
terminateAndEraseFile();
}
};
recorder.setOnInfoListener(infoListener);
recorder.prepare();
// Sometimes prepare takes some time to complete
Thread.sleep(2000);
try {
recorder.start();
} catch (Throwable t) {
t.printStackTrace();
Log.w("sf", t);
}
// recorder.start();
/*new Handler().postDelayed(new Runnable() {
public void run() {
// recorder.stop();
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
System.out.println("hello helloo");
recorder = null;
}
Log.e("Ho rha hai", "goodndnd");
}
}, 40000);*/
recording = true;
Log.d(Constants.TAG, "RecordService recorderStarted");
Toast.makeText(getApplicationContext(), "recording is strated ", 2000).show();
} catch (IllegalStateException e) {
Log.e(Constants.TAG, "IllegalStateException");
e.printStackTrace();
exception = true;
} catch (IOException e) {
Log.e(Constants.TAG, "IOException");
e.printStackTrace();
exception = true;
} catch (Exception e) {
Log.e(Constants.TAG, "Exception");
e.printStackTrace();
exception = true;
}
if (exception) {
terminateAndEraseFile();
}
if (recording) {
Toast toast = Toast.makeText(this,
this.getString(R.string.receiver_start_call),
Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(this,
this.getString(R.string.record_impossible),
Toast.LENGTH_LONG);
toast.show();
}
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
public void onError(MediaRecorder mr, int what, int extra) {
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
public void onInfo(MediaRecorder mr, int what, int extra) {
}
};
private void startService() {
if (!onForeground) {
Log.d(Constants.TAG, "RecordService startService");
Intent intent = new Intent(this, MainActivity.class);
// intent.setAction(Intent.ACTION_VIEW);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent, 0);
Notification notification = new NotificationCompat.Builder(
getBaseContext())
.setContentTitle(
this.getString(R.string.notification_title))
.setTicker(this.getString(R.string.notification_ticker))
.setContentText(this.getString(R.string.notification_text))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent).setOngoing(true)
.getNotification();
notification.flags = Notification.FLAG_NO_CLEAR;
startForeground(1337, notification);
onForeground = true;
}
}
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd_MMM_yy_HH_mm_ss");
String dateString = formatter.format(new Date(System
.currentTimeMillis()));
return (file.getAbsolutePath() + "/" + "Rec_" + dateString + file_exts[currentFormat]);
}
}
和广播接收器是这样的: -
公共类MyPhoneReceiver扩展了BroadcastReceiver {
private String phoneNumber;
@Override
public void onReceive(Context context, Intent intent) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String extraState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.d(Constants.TAG, "MyPhoneReciever phoneNumber "+phoneNumber);
if (MainActivity.updateExternalStorageState() == Constants.MEDIA_MOUNTED) {
try {
SharedPreferences settings = context.getSharedPreferences(
Constants.LISTEN_ENABLED, 0);
boolean silent = settings.getBoolean("silentMode", true);
if (extraState != null) {
if (extraState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Intent myIntent = new Intent(context,
RecordService.class);
myIntent.putExtra("commandType",
Constants.STATE_CALL_START);
context.startService(myIntent);
} else if (extraState
.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Intent myIntent = new Intent(context,
RecordService.class);
myIntent.putExtra("commandType",
Constants.STATE_CALL_END);
context.startService(myIntent);
} else if (extraState
.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (phoneNumber == null)
phoneNumber = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent myIntent = new Intent(context,
RecordService.class);
myIntent.putExtra("commandType",
Constants.STATE_INCOMING_NUMBER);
myIntent.putExtra("phoneNumber", phoneNumber);
myIntent.putExtra("silentMode", silent);
context.startService(myIntent);
}
} else if (phoneNumber != null) {
Intent myIntent = new Intent(context, RecordService.class);
myIntent.putExtra("commandType",
Constants.STATE_INCOMING_NUMBER);
myIntent.putExtra("phoneNumber", phoneNumber);
myIntent.putExtra("silentMode", silent);
context.startService(myIntent);
}
} catch (Exception e) {
Log.e(Constants.TAG, "Exception");
e.printStackTrace();
}
}
}
}