@octane-xplat/audio 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -66
- package/package.json +103 -98
- package/platforms/android/audio.aar +0 -0
- package/platforms/android/java/com/xplat/audio/AudioBridge.java +16 -8
- package/platforms/android/java/com/xplat/audio/AudioPlaybackService.java +63 -10
- package/platforms/macos/include/XplatAudioPlayer.h +3 -0
- package/platforms/macos/src/XplatAudioPlayer.m +27 -1
- package/src/audio.android.ts +193 -136
- package/src/audio.ios.ts +495 -377
- package/src/audio.macos.ts +372 -267
- package/src/audio.ts +8 -7
- package/src/audio.web.ts +306 -179
- package/src/audio.windows.ts +7 -0
- package/src/queue.ts +15 -0
- package/src/settings.ts +8 -0
- package/src/types.ts +89 -27
- package/types/generated/.tsrx-typegen-manifest.json +12 -0
- package/types/generated/audio.android.d.ts +3 -0
- package/types/generated/audio.d.ts +3 -0
- package/types/generated/audio.ios.d.ts +3 -0
- package/types/generated/index.d.ts +2 -0
- package/types/generated/queue.d.ts +3 -0
- package/types/generated/settings.d.ts +2 -0
- package/types/generated/types.d.ts +90 -0
- package/LICENSE +0 -21
- package/types/index.d.ts +0 -1
- package/types/types.d.ts +0 -36
package/README.md
CHANGED
|
@@ -1,69 +1,5 @@
|
|
|
1
1
|
# `@octane-xplat/audio`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
pnpm add @octane-xplat/audio
|
|
5
|
-
```
|
|
3
|
+
[Documentation](https://octane-xplat.goddardai.org/package-audio).
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
sound-effect bank. iOS uses `AVPlayer`/`AVAudioSession` (interruption
|
|
9
|
-
handling included) and Android uses a package-owned Media3 playback service.
|
|
10
|
-
macOS uses a package-owned AVPlayer bridge. Web uses an
|
|
11
|
-
`HTMLAudioElement` plus the Media Session API for system controls.
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
import { createAudioPlayer } from '@octane-xplat/audio'
|
|
15
|
-
|
|
16
|
-
const player = createAudioPlayer()
|
|
17
|
-
await player.setQueue([{ id: 'ep1', source: 'https://example.com/ep1.mp3' }])
|
|
18
|
-
await player.play()
|
|
19
|
-
|
|
20
|
-
const off = player.subscribe((snapshot) => {
|
|
21
|
-
// { state, currentTime, duration } — poll or subscribe, your choice
|
|
22
|
-
})
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Playback features vary by target and runtime state, so call
|
|
26
|
-
`capabilities()` before promising behavior like lock-screen controls. It
|
|
27
|
-
reports `supported`, `backgroundPlayback`, `systemControls`,
|
|
28
|
-
`userGestureRequired`, and `interruptions`. The `macos` package condition
|
|
29
|
-
selects the desktop bridge without importing mobile implementations. Rebuild
|
|
30
|
-
the macOS app after installing this package. macOS reports system controls and
|
|
31
|
-
interruption recovery as unavailable; a missing bridge reports `supported: false`.
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
// Continue with the player created above.
|
|
35
|
-
const capabilities = player.capabilities()
|
|
36
|
-
console.log(capabilities.backgroundPlayback, capabilities.systemControls)
|
|
37
|
-
off()
|
|
38
|
-
player.dispose()
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
On macOS, use an absolute local path, `file:` URL, or HTTP(S) URL. Queue
|
|
42
|
-
replacement rejects obsolete pending loads and stops the previous item.
|
|
43
|
-
Malformed media, network errors, reported output errors, and loading or
|
|
44
|
-
buffering stalled for 30 seconds become terminal errors. A new queue can retry;
|
|
45
|
-
disposal stops playback and notifications. Position, duration, and seek use
|
|
46
|
-
seconds. Real audible playback remains a separate qualification gate.
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
import { createAudioPlayer } from '@octane-xplat/audio'
|
|
50
|
-
|
|
51
|
-
export async function playTrack(source: string) {
|
|
52
|
-
const player = createAudioPlayer()
|
|
53
|
-
try {
|
|
54
|
-
await player.setQueue([{ id: 'track', source }])
|
|
55
|
-
await player.play()
|
|
56
|
-
await player.seek(2.5)
|
|
57
|
-
await player.pause()
|
|
58
|
-
} finally {
|
|
59
|
-
player.dispose()
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
For short UI sounds use [`@octane-xplat/sounds`](../sounds/README.md).
|
|
65
|
-
|
|
66
|
-
Guide: [Media services](../../docs/platform/media-services.md); per-target
|
|
67
|
-
availability: [platform notes](../../.agents/docs/notes/platform-notes.md). Exercised by
|
|
68
|
-
the harness `MediaServices` screen
|
|
69
|
-
([`packages/app/src/MediaServices.tsrx`](../app/src/MediaServices.tsrx)).
|
|
5
|
+
[API reference](https://octane-xplat.goddardai.org/api-audio).
|
package/package.json
CHANGED
|
@@ -1,99 +1,104 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
2
|
+
"name": "@octane-xplat/audio",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Long-form audio playback for Octane xplat",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"audio",
|
|
8
|
+
"audio-player",
|
|
9
|
+
"cross-platform",
|
|
10
|
+
"ios",
|
|
11
|
+
"music",
|
|
12
|
+
"nativescript",
|
|
13
|
+
"octane",
|
|
14
|
+
"playback",
|
|
15
|
+
"web",
|
|
16
|
+
"xplat"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/audio",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
23
|
+
"directory": "packages/audio"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"types/generated",
|
|
27
|
+
"src",
|
|
28
|
+
"platforms",
|
|
29
|
+
"!src/**/*.test.*",
|
|
30
|
+
"!src/**/*.spec.*"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./types/generated/index.d.ts",
|
|
36
|
+
"macos": "./src/index.macos.ts",
|
|
37
|
+
"web": "./src/index.web.ts",
|
|
38
|
+
"native": "./src/index.ts",
|
|
39
|
+
"default": "./src/index.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "node --test tests/*.test.mjs",
|
|
47
|
+
"typegen": "tsrx-typegen --target audio",
|
|
48
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
49
|
+
"test:packed": "node tests/packed-consumer.mjs",
|
|
50
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
51
|
+
"test:macos-runtime": "node tests/packed-consumer.mjs --macos-runtime",
|
|
52
|
+
"typecheck": "node ../../scripts/typecheck.ts",
|
|
53
|
+
"test:runtime": "node tests/runtime.mjs",
|
|
54
|
+
"test:browser": "node tests/browser.mjs",
|
|
55
|
+
"test:macos-budgets": "node tests/profile-macos.mjs"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@octane-xplat/platform": "0.20.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@nativescript-community/octane": "0.2.5",
|
|
62
|
+
"@nativescript/core": "9.1.3",
|
|
63
|
+
"@octanejs/vite-plugin": "0.5.0",
|
|
64
|
+
"octane": "0.16.3",
|
|
65
|
+
"tsrx-typegen": "0.6.1",
|
|
66
|
+
"typescript": "7.1.0-beta",
|
|
67
|
+
"vite": "8.3.6",
|
|
68
|
+
"playwright": "1.63.0",
|
|
69
|
+
"@tsrx/content-mapper": "0.1.2",
|
|
70
|
+
"typescript-classic": "npm:typescript@5.9.3"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
74
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
75
|
+
"octane": ">=0.6.3 <1"
|
|
76
|
+
},
|
|
77
|
+
"peerDependenciesMeta": {
|
|
78
|
+
"@nativescript-community/octane": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
81
|
+
"@nativescript/core": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
"octane": {
|
|
85
|
+
"optional": true
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"nativescript": {
|
|
89
|
+
"platforms": {
|
|
90
|
+
"android": "9.1.0",
|
|
91
|
+
"ios": "9.1.0"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
"xplat": {
|
|
95
|
+
"macos": {
|
|
96
|
+
"frameworks": [
|
|
97
|
+
"Foundation",
|
|
98
|
+
"AVFoundation",
|
|
99
|
+
"CoreMedia"
|
|
100
|
+
]
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
"types": "./types/generated/index.d.ts"
|
|
104
|
+
}
|
|
Binary file
|
|
@@ -6,7 +6,11 @@ import android.util.Log;
|
|
|
6
6
|
|
|
7
7
|
/** Commands cross the JS/service boundary as intents so they remain ordered. */
|
|
8
8
|
public final class AudioBridge {
|
|
9
|
+
public static final String ACTION_UPDATE = "com.xplat.audio.UPDATE";
|
|
9
10
|
public static final String ACTION_QUEUE = "com.xplat.audio.QUEUE";
|
|
11
|
+
public static final String ACTION_RATE = "com.xplat.audio.RATE";
|
|
12
|
+
public static final String ACTION_VOLUME = "com.xplat.audio.VOLUME";
|
|
13
|
+
public static final String ACTION_MUTED = "com.xplat.audio.MUTED";
|
|
10
14
|
public static final String ACTION_PLAY = "com.xplat.audio.PLAY";
|
|
11
15
|
public static final String ACTION_PAUSE = "com.xplat.audio.PAUSE";
|
|
12
16
|
public static final String ACTION_SEEK = "com.xplat.audio.SEEK";
|
|
@@ -17,7 +21,7 @@ public final class AudioBridge {
|
|
|
17
21
|
// app cannot observe or forge.
|
|
18
22
|
private static final String COMMAND_TOKEN = java.util.UUID.randomUUID().toString();
|
|
19
23
|
private static volatile String snapshot = "{\"state\":\"idle\",\"currentTime\":0,\"duration\":0}";
|
|
20
|
-
private static
|
|
24
|
+
private static final java.util.concurrent.ConcurrentHashMap<String, String> pendingQueues = new java.util.concurrent.ConcurrentHashMap<>();
|
|
21
25
|
|
|
22
26
|
private AudioBridge() {}
|
|
23
27
|
|
|
@@ -27,7 +31,12 @@ public final class AudioBridge {
|
|
|
27
31
|
intent.putExtra("commandToken", COMMAND_TOKEN);
|
|
28
32
|
// The queue JSON can exceed the ~1MB Binder extra limit — hand it to
|
|
29
33
|
// the service in-process and keep the intent small.
|
|
30
|
-
|
|
34
|
+
String queueKey = null;
|
|
35
|
+
if (payload != null && (ACTION_QUEUE.equals(action) || ACTION_UPDATE.equals(action))) {
|
|
36
|
+
queueKey = java.util.UUID.randomUUID().toString();
|
|
37
|
+
pendingQueues.put(queueKey, payload);
|
|
38
|
+
intent.putExtra("queueKey", queueKey);
|
|
39
|
+
}
|
|
31
40
|
intent.putExtra("index", index);
|
|
32
41
|
intent.putExtra("seconds", seconds);
|
|
33
42
|
try {
|
|
@@ -36,7 +45,7 @@ public final class AudioBridge {
|
|
|
36
45
|
// A JS-initiated command while the app is backgrounded throws
|
|
37
46
|
// IllegalStateException — surface it through snapshot.error and
|
|
38
47
|
// rethrow so the caller's promise rejects.
|
|
39
|
-
|
|
48
|
+
if (queueKey != null) pendingQueues.remove(queueKey);
|
|
40
49
|
Log.e(TAG, "Audio command dispatch failed", failure);
|
|
41
50
|
publish("{\"state\":\"error\",\"currentTime\":0,\"duration\":0,\"error\":\"audio command dispatch failed\"}");
|
|
42
51
|
throw failure;
|
|
@@ -47,12 +56,11 @@ public final class AudioBridge {
|
|
|
47
56
|
return intent != null && COMMAND_TOKEN.equals(intent.getStringExtra("commandToken"));
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
static String takeQueue() {
|
|
51
|
-
String
|
|
52
|
-
|
|
53
|
-
return queue;
|
|
59
|
+
static String takeQueue(Intent intent) {
|
|
60
|
+
String key = intent.getStringExtra("queueKey");
|
|
61
|
+
return key == null ? null : pendingQueues.remove(key);
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
public static void publish(String value) { snapshot = value; }
|
|
57
|
-
public static String snapshot() { return snapshot; }
|
|
65
|
+
public static String snapshot() { AudioPlaybackService.refreshSnapshot(); return snapshot; }
|
|
58
66
|
}
|
|
@@ -20,13 +20,18 @@ import org.json.JSONObject;
|
|
|
20
20
|
|
|
21
21
|
public final class AudioPlaybackService extends MediaSessionService {
|
|
22
22
|
private static final String TAG = "XplatAudio";
|
|
23
|
+
private static AudioPlaybackService active;
|
|
23
24
|
private ExoPlayer player;
|
|
24
25
|
private MediaSession session;
|
|
25
26
|
private Player.Listener listener;
|
|
26
27
|
private String error;
|
|
28
|
+
private float volume = 1;
|
|
29
|
+
private boolean muted;
|
|
30
|
+
private boolean queueComplete = true;
|
|
27
31
|
|
|
28
32
|
@Override public void onCreate() {
|
|
29
33
|
super.onCreate();
|
|
34
|
+
active = this;
|
|
30
35
|
player = new ExoPlayer.Builder(this).build();
|
|
31
36
|
player.setAudioAttributes(AudioAttributes.DEFAULT, true);
|
|
32
37
|
listener = new Player.Listener() {
|
|
@@ -56,7 +61,21 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
56
61
|
try {
|
|
57
62
|
switch (intent.getAction()) {
|
|
58
63
|
case AudioBridge.ACTION_QUEUE:
|
|
59
|
-
setQueue(AudioBridge.takeQueue(), intent.getIntExtra("index", 0));
|
|
64
|
+
setQueue(AudioBridge.takeQueue(intent), intent.getIntExtra("index", 0), false);
|
|
65
|
+
break;
|
|
66
|
+
case AudioBridge.ACTION_UPDATE:
|
|
67
|
+
setQueue(AudioBridge.takeQueue(intent), 0, true);
|
|
68
|
+
break;
|
|
69
|
+
case AudioBridge.ACTION_RATE:
|
|
70
|
+
player.setPlaybackParameters(new androidx.media3.common.PlaybackParameters((float) intent.getDoubleExtra("seconds", 1), 1));
|
|
71
|
+
break;
|
|
72
|
+
case AudioBridge.ACTION_VOLUME:
|
|
73
|
+
volume = (float) intent.getDoubleExtra("seconds", 1);
|
|
74
|
+
player.setVolume(muted ? 0 : volume);
|
|
75
|
+
break;
|
|
76
|
+
case AudioBridge.ACTION_MUTED:
|
|
77
|
+
muted = intent.getDoubleExtra("seconds", 0) != 0;
|
|
78
|
+
player.setVolume(muted ? 0 : volume);
|
|
60
79
|
break;
|
|
61
80
|
case AudioBridge.ACTION_PLAY: player.play(); break;
|
|
62
81
|
case AudioBridge.ACTION_PAUSE: player.pause(); break;
|
|
@@ -75,32 +94,65 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
75
94
|
return result;
|
|
76
95
|
}
|
|
77
96
|
|
|
78
|
-
private void setQueue(String encoded, int startAt) throws Exception {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
private void setQueue(String encoded, int startAt, boolean update) throws Exception {
|
|
98
|
+
if (encoded == null) throw new IllegalStateException("Missing audio queue command");
|
|
99
|
+
JSONObject payload = new JSONObject(encoded);
|
|
100
|
+
JSONArray queue = payload.getJSONArray("tracks");
|
|
101
|
+
java.util.List<MediaItem> items = new java.util.ArrayList<>();
|
|
82
102
|
for (int i = 0; i < queue.length(); i++) {
|
|
83
103
|
JSONObject track = queue.getJSONObject(i);
|
|
84
104
|
String source = track.getString("source");
|
|
85
105
|
MediaMetadata metadata = new MediaMetadata.Builder()
|
|
86
|
-
.setTitle(track.optString("title", track.
|
|
106
|
+
.setTitle(track.optString("title", track.getString("id")))
|
|
87
107
|
.setArtist(track.optString("artist", ""))
|
|
88
108
|
.setAlbumTitle(track.optString("album", ""))
|
|
89
109
|
.build();
|
|
90
110
|
Uri uri = source.startsWith("/") ? Uri.fromFile(new File(source)) : Uri.parse(source);
|
|
91
|
-
|
|
111
|
+
items.add(new MediaItem.Builder().setMediaId(track.getString("id")).setUri(uri).setMediaMetadata(metadata).build());
|
|
112
|
+
}
|
|
113
|
+
MediaItem current = player.getCurrentMediaItem();
|
|
114
|
+
int retained = -1;
|
|
115
|
+
if (update && current != null) {
|
|
116
|
+
for (int i = 0; i < items.size(); i++) {
|
|
117
|
+
MediaItem item = items.get(i);
|
|
118
|
+
if (item.mediaId.equals(current.mediaId) && item.localConfiguration.uri.equals(current.localConfiguration.uri)) retained = i;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
boolean waiting = !queueComplete && player.getPlaybackState() == Player.STATE_ENDED;
|
|
122
|
+
queueComplete = payload.optBoolean("complete", true);
|
|
123
|
+
error = null;
|
|
124
|
+
if (retained >= 0) {
|
|
125
|
+
// Edit around the current native MediaItem; never replace its source.
|
|
126
|
+
int oldIndex = player.getCurrentMediaItemIndex();
|
|
127
|
+
player.removeMediaItems(oldIndex + 1, player.getMediaItemCount());
|
|
128
|
+
player.removeMediaItems(0, oldIndex);
|
|
129
|
+
player.addMediaItems(0, items.subList(0, retained));
|
|
130
|
+
player.addMediaItems(items.subList(retained + 1, items.size()));
|
|
131
|
+
player.replaceMediaItem(retained, current.buildUpon().setMediaMetadata(items.get(retained).mediaMetadata).build());
|
|
132
|
+
if (waiting && player.getPlayWhenReady() && retained + 1 < items.size()) {
|
|
133
|
+
player.seekTo(retained + 1, 0);
|
|
134
|
+
player.prepare();
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
player.pause();
|
|
138
|
+
player.setMediaItems(items, items.isEmpty() ? 0 : Math.max(0, Math.min(startAt, items.size() - 1)), 0);
|
|
139
|
+
player.prepare();
|
|
92
140
|
}
|
|
93
|
-
if (queue.length() > 0) player.seekTo(Math.max(0, Math.min(startAt, queue.length() - 1)), 0);
|
|
94
|
-
player.prepare();
|
|
95
141
|
publishSnapshot();
|
|
96
142
|
}
|
|
97
143
|
|
|
144
|
+
static void refreshSnapshot() {
|
|
145
|
+
AudioPlaybackService service = active;
|
|
146
|
+
if (service != null && android.os.Looper.myLooper() == android.os.Looper.getMainLooper()) service.publishSnapshot();
|
|
147
|
+
}
|
|
148
|
+
|
|
98
149
|
private void publishSnapshot() {
|
|
99
150
|
try {
|
|
100
151
|
JSONObject value = new JSONObject();
|
|
101
152
|
int index = player == null ? -1 : player.getCurrentMediaItemIndex();
|
|
102
|
-
value.put("state", error != null ? "error" : player == null || player.getMediaItemCount() == 0 ? "idle" : player.getPlaybackState() == Player.STATE_ENDED ? "ended" : player.isPlaying() ? "playing" : player.getPlaybackState() == Player.STATE_BUFFERING ? "loading" : "paused");
|
|
153
|
+
value.put("state", error != null ? "error" : player == null || player.getMediaItemCount() == 0 ? "idle" : player.getPlaybackState() == Player.STATE_ENDED ? (queueComplete ? "ended" : player.getPlayWhenReady() ? "waiting" : "paused") : player.isPlaying() ? "playing" : player.getPlaybackState() == Player.STATE_BUFFERING ? "loading" : "paused");
|
|
103
154
|
value.put("index", index);
|
|
155
|
+
if (player.getCurrentMediaItem() != null) value.put("id", player.getCurrentMediaItem().mediaId);
|
|
104
156
|
value.put("currentTime", player == null ? 0 : Math.max(0, player.getCurrentPosition()) / 1000.0);
|
|
105
157
|
value.put("duration", player == null || player.getDuration() < 0 ? 0 : player.getDuration() / 1000.0);
|
|
106
158
|
if (error != null) value.put("error", error);
|
|
@@ -109,6 +161,7 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
109
161
|
}
|
|
110
162
|
|
|
111
163
|
@Override public void onDestroy() {
|
|
164
|
+
if (active == this) active = null;
|
|
112
165
|
if (player != null) {
|
|
113
166
|
player.removeListener(listener);
|
|
114
167
|
player.release();
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// One instance owns one AVPlayer. Calls and notifications run on the host main thread.
|
|
4
4
|
@interface XplatAudioPlayer : NSObject
|
|
5
5
|
- (void)setSource:(NSString *)source;
|
|
6
|
+
- (void)setPlaybackRate:(double)rate;
|
|
7
|
+
- (void)setVolume:(double)volume;
|
|
8
|
+
- (void)setMuted:(BOOL)muted;
|
|
6
9
|
- (void)play;
|
|
7
10
|
- (void)pause;
|
|
8
11
|
- (void)seekSeconds:(double)seconds;
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
@property(nonatomic, strong) NSMutableArray *observers;
|
|
9
9
|
@property(nonatomic, copy) NSString *state;
|
|
10
10
|
@property(nonatomic, copy) NSString *failure;
|
|
11
|
+
@property(nonatomic) double playbackRate;
|
|
12
|
+
@property(nonatomic) double volume;
|
|
13
|
+
@property(nonatomic) BOOL muted;
|
|
11
14
|
@property(nonatomic) BOOL disposed;
|
|
12
15
|
@property(nonatomic) BOOL wantsPlayback;
|
|
13
16
|
@property(nonatomic) NSTimeInterval loadingSince;
|
|
@@ -17,6 +20,8 @@
|
|
|
17
20
|
- (instancetype)init {
|
|
18
21
|
if ((self = [super init])) {
|
|
19
22
|
_state = @"idle";
|
|
23
|
+
_playbackRate = 1;
|
|
24
|
+
_volume = 1;
|
|
20
25
|
_observers = [NSMutableArray new];
|
|
21
26
|
}
|
|
22
27
|
return self;
|
|
@@ -58,7 +63,10 @@
|
|
|
58
63
|
self.state = @"loading";
|
|
59
64
|
self.loadingSince = NSProcessInfo.processInfo.systemUptime;
|
|
60
65
|
self.item = [AVPlayerItem playerItemWithURL:url];
|
|
66
|
+
self.item.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithmTimeDomain;
|
|
61
67
|
self.player = [AVPlayer playerWithPlayerItem:self.item];
|
|
68
|
+
self.player.volume = self.volume;
|
|
69
|
+
self.player.muted = self.muted;
|
|
62
70
|
__weak XplatAudioPlayer *owner = self;
|
|
63
71
|
AVPlayerItem *item = self.item;
|
|
64
72
|
NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
|
|
@@ -106,10 +114,28 @@
|
|
|
106
114
|
self.state = @"paused";
|
|
107
115
|
}
|
|
108
116
|
self.wantsPlayback = YES;
|
|
109
|
-
|
|
117
|
+
// Submit one playback transition. play followed by setRate can freeze at
|
|
118
|
+
// time zero during rapid pause/resume on macOS 26 (see readiness record).
|
|
119
|
+
[self.player playImmediatelyAtRate:self.playbackRate];
|
|
110
120
|
[self refresh];
|
|
111
121
|
}
|
|
112
122
|
|
|
123
|
+
- (void)setPlaybackRate:(double)rate {
|
|
124
|
+
if (self.disposed || !isfinite(rate)) return;
|
|
125
|
+
_playbackRate = fmax(0.5, fmin(3, rate));
|
|
126
|
+
if (self.wantsPlayback) self.player.rate = _playbackRate;
|
|
127
|
+
}
|
|
128
|
+
- (void)setVolume:(double)volume {
|
|
129
|
+
if (self.disposed || !isfinite(volume)) return;
|
|
130
|
+
_volume = fmax(0, fmin(1, volume));
|
|
131
|
+
self.player.volume = _volume;
|
|
132
|
+
}
|
|
133
|
+
- (void)setMuted:(BOOL)muted {
|
|
134
|
+
if (self.disposed) return;
|
|
135
|
+
_muted = muted;
|
|
136
|
+
self.player.muted = muted;
|
|
137
|
+
}
|
|
138
|
+
|
|
113
139
|
- (void)pause {
|
|
114
140
|
if (!self.player || self.disposed) return;
|
|
115
141
|
self.wantsPlayback = NO;
|