I'm creating an baby monitor (only audio) app, but every time screen is locked my app is stopped, tried use Service, WakeLock, AudioRecord Lib and others solutions, but this do not work. Following code:
Thank you for help!
MainActivity:
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: this service is started and stopped from MainActivity on buttonStart and buttonStop methods.
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(); } } } https://stackoverflow.com/questions/65555355/how-to-keep-audiorecord-or-mediarecorder-running-when-screen-is-locked-android January 04, 2021 at 05:59AM
没有评论:
发表评论