Skip to content

Commit 98cb70f

Browse files
committed
improve: audio ending logic
This commit greatly improves the capabilities and audio sending stability of /voice, allowing it to only force stop sending audio when explictly asked to.
1 parent 36e19ae commit 98cb70f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

docs/Connection/play.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ connection.play(stream)
1313
> [!WARNING]
1414
> The connection must be initialized before calling this function. Always check if `connection.udp != null` before calling this function. Make sure that the stream is an Opus stream.
1515
16+
> [!IMPORTANT]
17+
> The stream real ending MUST end with 0 (as Buffer), otherwise it will persist with the interval to play packets.
18+
1619
## Parameters
1720

1821
- `stream` - The stream to play.

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DISCORD_CLOSE_CODES = {
1919
4014: { error: false },
2020
4015: { reconnect: true }
2121
}
22+
const STREAM_END = Buffer.alloc(1)
2223

2324
const ssrcs = {}
2425

@@ -411,7 +412,8 @@ class Connection extends EventEmitter {
411412
this.playInterval = setInterval(() => {
412413
const chunk = this.audioStream.read(OPUS_FRAME_SIZE)
413414

414-
if (!chunk) return this.stop('finished')
415+
if (chunk === STREAM_END) return this.stop('finished')
416+
if (!chunk) return;
415417

416418
this.sendAudioChunk(packetBuffer, chunk)
417419
}, OPUS_FRAME_DURATION)

0 commit comments

Comments
 (0)