Skip to content

Commit

Permalink
LiveEffect: Add foreground service
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 committed Jul 11, 2024
1 parent fd840ad commit 8c5de27
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 10 deletions.
29 changes: 19 additions & 10 deletions samples/LiveEffect/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@
<uses-feature android:name="android.hardware.audio.output" android:required="true" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<application
android:allowBackup="false"
android:fullBackupContent="false"
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.google.oboe.samples.liveEffect.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.oboe.samples.liveEffect.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".DuplexStreamForegroundService"
android:foregroundServiceType="mediaPlayback|microphone"
android:exported="false">
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.google.oboe.samples.liveEffect;

import android.app.ForegroundServiceStartNotAllowedException;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.content.ContextCompat;

public class DuplexStreamForegroundService extends Service {
private static final String TAG = MainActivity.class.getName();
public static final String ACTION_START = "ACTION_START";
public static final String ACTION_STOP = "ACTION_STOP";

public void startForegroundService1() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
startForeground(1, buildNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
| ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
}
}

public void stopForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
stopForeground(STOP_FOREGROUND_REMOVE);
}
}

@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}

private Notification buildNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(new NotificationChannel(
"all",
"All Notifications",
NotificationManager.IMPORTANCE_NONE));

return new Notification.Builder(this, "all")
.setContentTitle("Recording audio")
.setContentText("recording...")
.setSmallIcon(R.drawable.balance_seekbar)
.build();
}
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Receive onStartCommand" + intent);
switch (intent.getAction()) {
case ACTION_START:
Log.i(TAG, "Receive ACTION_START" + intent.getExtras());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
startForeground(1, buildNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
| ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
}
break;
case ACTION_STOP:
Log.i(TAG, "Receive STOP_RECORD" + intent.getExtras());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
stopForeground(STOP_FOREGROUND_REMOVE);
}
break;
}
return START_NOT_STICKY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.google.oboe.samples.liveEffect;

import static com.google.oboe.samples.liveEffect.DuplexStreamForegroundService.ACTION_START;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
Expand Down Expand Up @@ -126,6 +129,12 @@ public void onClick(View v) {
LiveEffectEngine.setDefaultStreamValues(this);
setVolumeControlStream(AudioManager.STREAM_MUSIC);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent serviceIntent = new Intent(ACTION_START, null, this,
DuplexStreamForegroundService.class);
startForegroundService(serviceIntent);
}

onStartTest();
}

Expand Down Expand Up @@ -163,6 +172,12 @@ protected void onPause() {
@Override
protected void onDestroy() {
onStopTest();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent serviceIntent = new Intent(ACTION_START, null, this,
DuplexStreamForegroundService.class);
startForegroundService(serviceIntent);
}
super.onDestroy();
}

Expand Down

0 comments on commit 8c5de27

Please sign in to comment.