Skip to content

Commit fb69c6f

Browse files
committed
fix(audio-context): AudioScheduledSourceNode
1 parent 23866d8 commit fb69c6f

6 files changed

Lines changed: 44 additions & 26 deletions

File tree

packages/audio-context/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/audio-context",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "Web Audio API for NativeScript",
55
"main": "index",
66
"types": "index.d.ts",
261 Bytes
Binary file not shown.

packages/audio-context/src-native/android/.idea/deploymentTargetSelector.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioBufferSourceNode.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ public AudioBuffer getBuffer() {
4242
}
4343

4444
public void setBuffer(@Nullable AudioBuffer buffer) {
45-
stop();
4645
mBuffer = buffer;
47-
AudioContext.getInstance().switchBufferSource(id, buffer);
46+
AudioContext ctx = AudioContext.getInstance();
47+
String newId = ctx.switchBufferSource(id, buffer);
48+
if (newId != null && !newId.equals(id)) {
49+
if (playbackRateId != null) {
50+
ctx.attachPlaybackRateToVoice(id, "");
51+
ctx.attachPlaybackRateToVoice(newId, playbackRateId);
52+
}
53+
id = newId;
54+
}
4855
}
4956

5057
@Override

packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioContext.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,40 +2581,42 @@ public void endExternalPcmSource(String trackId) {
25812581

25822582
private native void nativeEndExternalPcmSource(String trackId);
25832583

2584-
void switchBufferSource(String trackId, @Nullable AudioBuffer buffer) {
2585-
if (trackId == null) return;
2584+
@Nullable
2585+
String switchBufferSource(String trackId, @Nullable AudioBuffer buffer) {
2586+
if (trackId == null) return null;
25862587
stopTrack(trackId);
25872588

25882589
if (buffer == null) {
25892590
directBuffers.remove(trackId + "::buffer");
2590-
return;
2591+
return null;
25912592
}
25922593

25932594
if (nativeAvailable) {
25942595
java.nio.ByteBuffer src = directBuffers.get(buffer.id);
2595-
if (src != null) {
2596-
try {
2597-
int sampleRate = sampleRates.containsKey(buffer.id) ? sampleRates.get(buffer.id) : 48000;
2598-
int channels = bufferChannels.containsKey(buffer.id) ? bufferChannels.get(buffer.id) : 1;
2599-
int byteLen = src.capacity();
2600-
int bytesPerSample = (byteLen % 4 == 0) ? 4 : 2;
2601-
java.nio.ByteBuffer srcView = src.duplicate();
2602-
srcView.position(src.position());
2603-
srcView.limit(src.limit());
2604-
java.nio.ByteBuffer srcSlice = srcView.slice();
2605-
nativeCreateBufferSourceDirect(srcSlice, sampleRate, channels, bytesPerSample);
2606-
directBuffers.put(trackId, src);
2607-
getOrCreate(trackId).incrementAndGet();
2608-
sampleRates.put(trackId, sampleRate);
2609-
bufferChannels.put(trackId, channels);
2610-
} catch (Throwable ignored) {
2611-
}
2596+
if (src == null) return null;
2597+
try {
2598+
int sampleRate = sampleRates.containsKey(buffer.id) ? sampleRates.get(buffer.id) : 48000;
2599+
int channels = bufferChannels.containsKey(buffer.id) ? bufferChannels.get(buffer.id) : 1;
2600+
int byteLen = src.capacity();
2601+
int bytesPerSample = (byteLen % 4 == 0) ? 4 : 2;
2602+
java.nio.ByteBuffer srcView = src.duplicate();
2603+
srcView.position(src.position());
2604+
srcView.limit(src.limit());
2605+
java.nio.ByteBuffer srcSlice = srcView.slice();
2606+
String newId = nativeCreateBufferSourceDirect(srcSlice, sampleRate, channels, bytesPerSample);
2607+
if (newId == null || newId.isEmpty()) return null;
2608+
directBuffers.put(newId, src);
2609+
getOrCreate(newId).incrementAndGet();
2610+
sampleRates.put(newId, sampleRate);
2611+
bufferChannels.put(newId, channels);
2612+
return newId;
2613+
} catch (Throwable ignored) {
2614+
return null;
26122615
}
2613-
return;
26142616
}
26152617

26162618
java.nio.ByteBuffer src = directBuffers.get(buffer.id);
2617-
if (src == null) return;
2619+
if (src == null) return null;
26182620
String bufferId = buffer.id;
26192621
int sampleRate = sampleRates.containsKey(bufferId) ? sampleRates.get(bufferId) : 48000;
26202622
int srcChannels = bufferChannels.containsKey(bufferId) ? bufferChannels.get(bufferId) : 1;
@@ -2635,6 +2637,7 @@ void switchBufferSource(String trackId, @Nullable AudioBuffer buffer) {
26352637
monoBb.position(0);
26362638
directBuffers.put(trackId + "::buffer", monoBb);
26372639
sampleRates.put(trackId, sampleRate);
2640+
return null;
26382641
}
26392642

26402643
String createBufferSource(String contextId, @Nullable AudioBuffer buffer) {

packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioScheduledSourceNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.nativescript.audiocontext;
22

33
public class AudioScheduledSourceNode implements NativeObject, AudioNode {
4-
public final String id;
4+
public String id;
55

66
public AudioScheduledSourceNode(String id) {
77
this.id = id;

0 commit comments

Comments
 (0)