我正在创建一个婴儿监视器(只有音频)应用程序,但每次屏幕被锁定我的应用程序被停止,尝试使用服务,wakelock,audiorecord lib和其他解决方案,但这不起作用。以下代码:
谢谢你的帮助!
主要活动:
public class MainActivity extends AppCompatActivity {
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
verifyPermissions();
}
public void buttonStart(View view) {
thread = new Thread(new Runnable() {
@Override
public void run() {
Intent serviceIntent = new Intent(this, RecordService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
ContextCompat.startForegroundService(this, serviceIntent);
}
});
thread.start();
}
public void buttonStop(View view) {
Intent serviceIntent = new Intent(this.getApplicationContext(), RecordService.class);
this.stopService(serviceIntent);
}
}
recordservice:此服务从buttonstart和buttonstop方法的mainactivity启动和停止。
public class RecordService extends Service {
private MediaRecorder mediaRecorder;
private MediaPlayer player;
private String fileName;
@Override
public void onCreate() {
super.onCreate();
createNotification();
}
private PowerManager.WakeLock wakeLockPartial = null;
public int onStartCommand(Intent intent, int flags, int startId) {
createNotification();
String input = intent.getStringExtra("inputExtra");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(getApplicationContext().POWER_SERVICE);
wakeLockPartial = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "angelear:mywakelocktag");
wakeLockPartial.acquire();
mediaRecorder = new MediaRecorder();
resetRecorder();
mediaRecorder.start();
}
});
thread.start();
return START_STICKY; //tried use START_NOT_STICKY
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
wakeLockPartial.release();
}
}
private void createNotification() {
if (Build.VERSION.SDK_INT >= 26) {
String CHANNEL_ID = "my_channel_01";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("")
.setContentText("").build();
startForeground(1, notification);
}
}
private void resetRecorder() {
long currentTimestamp = System.currentTimeMillis();
fileName = this.getExternalFilesDir(null).getAbsolutePath() + "/" + currentTimestamp + ".3gp";
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncodingBitRate(16);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setOutputFile(fileName);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!