Skip to content

Commit

Permalink
better support for existing mediacontrols
Browse files Browse the repository at this point in the history
  • Loading branch information
defsub committed May 1, 2023
1 parent a64d469 commit b7de087
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class AudioService extends MediaBrowserServiceCompat {
private static final int NOTIFICATION_ID = 1124;
private static final int REQUEST_CONTENT_INTENT = 1000;
public static final String NOTIFICATION_CLICK_ACTION = "com.ryanheise.audioservice.NOTIFICATION_CLICK";
public static final String CUSTOM_ACTION_STOP = "com.ryanheise.audioservice.action.STOP";
public static final String CUSTOM_ACTION_FAST_FORWARD = "com.ryanheise.audioservice.action.FAST_FORWARD";
public static final String CUSTOM_ACTION_REWIND = "com.ryanheise.audioservice.action.REWIND";
private static final String BROWSABLE_ROOT_ID = "root";
private static final String RECENT_ROOT_ID = "recent";
// See the comment in onMediaButtonEvent to understand how the BYPASS keycodes work.
Expand Down Expand Up @@ -434,24 +437,7 @@ NotificationCompat.Action createAction(String resource, String label, long actio
}

private boolean needCustomMediaControl(MediaControl control) {
if (control.customAction != null) {
return true;
}

// Android 13 changes MediaControl behavior as documented here:
// https://developer.android.com/about/versions/13/behavior-changes-13
// The below actions will be added to slots 1-3, if included.
// 1 - ACTION_PLAY, ACTION_PLAY
// 2 - ACTION_SKIP_TO_PREVIOUS
// 3 - ACTION_SKIP_TO_NEXT
// Custom actions will use slots 2-5 if included.
// - ACTION_STOP
// - ACTION_FAST_FORWARD
// - ACTION_REWIND
return (Build.VERSION.SDK_INT >= 33 &&
(control.actionCode == PlaybackStateCompat.ACTION_STOP ||
control.actionCode == PlaybackStateCompat.ACTION_FAST_FORWARD ||
control.actionCode == PlaybackStateCompat.ACTION_REWIND));
return control.customAction != null;
}

private Bundle mapToBundle(Map<?, ?> map) {
Expand Down Expand Up @@ -1082,7 +1068,15 @@ public void onSetShuffleMode(int shuffleMode) {
@Override
public void onCustomAction(String action, Bundle extras) {
if (listener == null) return;
listener.onCustomAction(action, extras);
if (CUSTOM_ACTION_STOP.equals(action)) {
listener.onStop();
} else if (CUSTOM_ACTION_FAST_FORWARD.equals(action)) {
listener.onFastForward();
} else if (CUSTOM_ACTION_REWIND.equals(action)) {
listener.onRewind();
} else {
listener.onCustomAction(action, extras);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -877,6 +878,10 @@ public void onMethodCall(MethodCall call, Result result) {
Map<?, ?> extras = (Map<?, ?>) customActionMap.get("extras");
customAction = new CustomAction(name, extras);
}
// check if we need to insert a CustomAction to support this MediaControl
if (needCustomAction(actionCode) && customAction == null) {
customAction = createCustomAction(actionCode);
}
actions.add(new MediaControl(resource, label, actionCode, customAction));
}
for (Integer rawSystemAction : rawSystemActions) {
Expand Down Expand Up @@ -1167,6 +1172,36 @@ private static List<MediaSessionCompat.QueueItem> raw2queue(List<Map<?, ?>> rawQ
return queue;
}

private static boolean needCustomAction(long actionCode) {
// Android 13 changes MediaControl behavior as documented here:
// https://developer.android.com/about/versions/13/behavior-changes-13
// The below actions will be added to slots 1-3, if included.
// 1 - ACTION_PLAY, ACTION_PLAY
// 2 - ACTION_SKIP_TO_PREVIOUS
// 3 - ACTION_SKIP_TO_NEXT
// Custom actions will use slots 2-5 if included.
// - ACTION_STOP
// - ACTION_FAST_FORWARD
// - ACTION_REWIND
return Build.VERSION.SDK_INT >= 33 &&
(actionCode == PlaybackStateCompat.ACTION_STOP ||
actionCode == PlaybackStateCompat.ACTION_FAST_FORWARD ||
actionCode == PlaybackStateCompat.ACTION_REWIND);
}

private static CustomAction createCustomAction(long actionCode) {
if (Build.VERSION.SDK_INT >= 33) {
if (actionCode == PlaybackStateCompat.ACTION_STOP) {
return new CustomAction(AudioService.CUSTOM_ACTION_STOP, null);
} else if (actionCode == PlaybackStateCompat.ACTION_FAST_FORWARD) {
return new CustomAction(AudioService.CUSTOM_ACTION_FAST_FORWARD, null);
} else if (actionCode == PlaybackStateCompat.ACTION_REWIND) {
return new CustomAction(AudioService.CUSTOM_ACTION_REWIND, null);
}
}
return null;
}

public static Long getLong(Object o) {
return (o == null || o instanceof Long) ? (Long)o : Long.valueOf((Integer) o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
1 change: 0 additions & 1 deletion audio_service/example/lib/example_custom_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ class AudioPlayerHandler extends BaseAudioHandler
MediaAction.seekForward,
MediaAction.seekBackward,
},
androidCompactActionIndices: const [0, 1],
processingState: const {
ProcessingState.idle: AudioProcessingState.idle,
ProcessingState.loading: AudioProcessingState.loading,
Expand Down
11 changes: 10 additions & 1 deletion audio_service/lib/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,16 @@ class BaseAudioHandler extends AudioHandler {

@override
Future<dynamic> customAction(String name,
[Map<String, dynamic>? extras]) async {}
[Map<String, dynamic>? extras]) async {
switch (name) {
case CustomAction.stop:
return stop();
case CustomAction.fastForward:
return fastForward();
case CustomAction.rewind:
return rewind();
}
}

@override
Future<void> onTaskRemoved() async {}
Expand Down
12 changes: 6 additions & 6 deletions audio_service/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ environment:

dependencies:
# Use these deps for local development
# audio_service_platform_interface:
# path: ../audio_service_platform_interface
# audio_service_web:
# path: ../audio_service_web
audio_service_platform_interface:
path: ../audio_service_platform_interface
audio_service_web:
path: ../audio_service_web

# Use these deps when pushing to origin (for the benefit of testers)
# audio_service_platform_interface:
Expand All @@ -27,8 +27,8 @@ dependencies:
# path: audio_service_web

# Use these deps when publishing.
audio_service_platform_interface: ^0.1.0
audio_service_web: ^0.1.1
#audio_service_platform_interface: ^0.1.0
#audio_service_web: ^0.1.1

audio_session: ^0.1.6+1
rxdart: '>=0.26.0 <0.28.0'
Expand Down
2 changes: 1 addition & 1 deletion audio_service_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the audio_service plugin. Different
homepage: https://github.com/ryanheise/audio_service/tree/master/audio_service_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 0.2.0
version: 0.1.1

dependencies:
flutter:
Expand Down
6 changes: 3 additions & 3 deletions audio_service_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ environment:

dependencies:
# Use these deps for local development
# audio_service_platform_interface:
# path: ../audio_service_platform_interface
audio_service_platform_interface:
path: ../audio_service_platform_interface

# Use these deps when pushing to origin (for the benefit of testers)
# audio_service_platform_interface:
Expand All @@ -20,7 +20,7 @@ dependencies:
# path: audio_service_platform_interface

# Use these deps when publishing.
audio_service_platform_interface: ^0.1.0
#audio_service_platform_interface: ^0.1.0

rxdart: '>=0.26.0 <0.28.0'
js: ^0.6.3
Expand Down

0 comments on commit b7de087

Please sign in to comment.