我正在使用BroadcastReceiver启动记录器,但是当我调用context.stopService(intent)
时,会调用onDestroy()
。我正在使用onDestroy()
停止记录器,但是并没有停止花费更多时间来release
记录器,这是我的代码。
public class RecorderService extends Service {
MediaRecorder recorder;
static final String TAGS=" Inside Service";
private static final int ID_SERVICE = 101;
File file;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build();
startForeground(ID_SERVICE,notification);
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(NotificationManager notificationManager){
String channelId = "my_service_channelid";
String channelName = "My Foreground Service";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(channel);
return channelId;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
recorder = new MediaRecorder();
recorder.reset();
String phoneNumber=intent.getStringExtra("number");
Log.d(TAGS, "Phone number in service: "+phoneNumber);
String time=new CommanMethods().getTIme();
String path=new CommanMethods().getPath();
String rec=path+"/"+time+".amr";
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setOutputFile(rec);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
Log.d(TAGS, "onStartCommand: "+"Recording started");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
super.onDestroy();
if (recorder != null) {
try {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
} catch (RuntimeException e) {
Log.d("mrecorder", e.getMessage());
}
Log.d("recorder", "onDestroy: " + "Recording stopped");
}
}
}
原木猫
2020-09-23 19:58:47.336 6365-6365/com.pranay.boot I/MediaRecorder: stop()
2020-09-23 19:58:47.418 6365-6365/com.pranay.boot I/MediaRecorder: reset()
2020-09-23 19:58:47.418 6365-6365/com.pranay.boot I/MediaRecorder: release()
2020-09-23 19:58:47.421 6365-6365/com.pranay.boot D/recorder: onDestroy: Recording stopped
2020-09-23 20:00:02.457 6365-6386/com.pranay.boot I/MediaRecorder: release()// the recorder taking more time to release
有什么解决办法吗?