Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions app/src/main/java/fr/dzx/audiosource/RecordService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fr.dzx.audiosource;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.media.AudioFormat;
import android.media.AudioRecord;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
startForeground(NOTIFICATION_ID, notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID, notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE);
} else {
Expand All @@ -78,12 +80,19 @@ public int onStartCommand(Intent intent, int flags, int startId) {
DEFAULT_CHANNEL_CONFIG,
DEFAULT_AUDIO_ENCODING);

AudioRecord recorder = new AudioRecord(
MediaRecorder.AudioSource.DEFAULT,
DEFAULT_SAMPLE_RATE,
DEFAULT_CHANNEL_CONFIG,
DEFAULT_AUDIO_ENCODING,
2 * minBufSize);
AudioRecord recorder;
try {
recorder = new AudioRecord(
MediaRecorder.AudioSource.DEFAULT,
DEFAULT_SAMPLE_RATE,
DEFAULT_CHANNEL_CONFIG,
DEFAULT_AUDIO_ENCODING,
2 * minBufSize);
} catch (SecurityException e) {
Log.e(App.TAG, "Permission RECORD_AUDIO not granted");
stopSelf();
return START_NOT_STICKY;
}

if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
Log.e(App.TAG, "Failed to initialize AudioRecord with default parameters");
Expand Down Expand Up @@ -127,7 +136,12 @@ protected void showNotificationListening() {
.setSmallIcon(R.drawable.ic_microphone)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
getNotificationManager().notify(NOTIFICATION_ID, notification);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) {
getNotificationManager().notify(NOTIFICATION_ID, notification);
} else {
Log.w(App.TAG, "POST_NOTIFICATIONS permission not granted");
}
}

protected void showNotificationEstablished() {
Expand All @@ -139,7 +153,12 @@ protected void showNotificationEstablished() {
.setSmallIcon(R.drawable.ic_microphone)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
getNotificationManager().notify(NOTIFICATION_ID, notification);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) {
getNotificationManager().notify(NOTIFICATION_ID, notification);
} else {
Log.w(App.TAG, "POST_NOTIFICATIONS permission not granted");
}
}

private NotificationManagerCompat getNotificationManager() {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/fr/dzx/audiosource/RecordThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
public class RecordThread extends Thread {
private static final int SAMPLE_RATE = 44100;
private static final int CHANNELS = 1;

private static final String SOCKET_NAME = "audiosource";

private final RecordService service;
private final AudioRecord recorder;

private volatile LocalServerSocket serverSocket;

RecordThread(RecordService service, AudioRecord recorder) {
Expand Down