Files
discord.js/apps/guide/content/docs/voice/seeking.mdx
Souji 591668099e feat(guide): port remaining prs/issues from legacy (#10974)
* chore: remove await wait placeholder

prefer using an explanatory placeholder rather than this artificial
example
original issue: https://github.com/discordjs/guide/issues/1360

* chore: remove implicit grant guide and add disclaimer

issue: https://github.com/discordjs/guide/issues/1370/
pr: https://github.com/discordjs/guide/pull/1543/

* chore(sharding): improve broadcast sample and use of context argument

original PR: https://github.com/discordjs/guide/pull/1624

* feat: add page about setup with proxy

original PR: https://github.com/discordjs/guide/pull/1623

* chore: clarify hiding of commands

original PR: https://github.com/discordjs/guide/pull/1617

* feat(voice): seeking

original PR: https://github.com/discordjs/guide/pull/1483

* chore(oauth2): typo

* chore: align with rest of the guide

remove abstraction layers in ws proxy handling in favour of directly setting globals

* chore: branding over grammar

* Apply suggestions from code review

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>

* chore: remove now obsolete example explanation from comments

---------

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>
2025-07-12 00:13:54 +00:00

65 lines
1.7 KiB
Plaintext

---
title: Seeking
---
There is no built-in method for seeking audio resources in discord.js. However external libraries such as `prism-media` can be used to tackle this issue.
## Seeking in a stream
To seek resource, you can create a new function with the following code:
```js
// Require necessary package
const prism = require('prism-media'); // [!code word:prism]
function createFFmpegStream(stream, seek) {
let seekPosition = '0';
if (seek) seekPosition = String(seek);
const transcoder = new prism.FFmpeg({
args: [
'-analyzeduration',
'0',
'-loglevel',
'0',
'-f',
's16le',
'-ar',
'48000',
'-ac',
'2',
'-ss',
seekPosition,
'-ab',
'320',
],
});
const s16le = stream.pipe(transcoder);
const opus = s16le.pipe(new prism.opus.Encoder({ rate: 48000, channels: 2, frameSize: 960 }));
return opus;
}
```
This function takes two arguments: the audio stream and the desired seek position, expressed as duration within the duration of the full stream. It returns the seeked stream.
<Callout>
You can find configuration options in the [prism media documentation](https://amishshah.github.io/prism-media/).
</Callout>
## Using seek with the audio player
```js
const { createAudioResource, createAudioPlayer } = require('@discordjs/voice');
const fs = require('fs');
const player = createAudioPlayer();
const normalAudioResource = createAudioResource('Your audio file path');
player.play(normalAudioResource);
// [!code word:createFFmpegStream]
const seekedAudioStream = createFFmpegStream(fs.createReadStream('Your audio file path'), 10); // Seek to 10s
const seekedAudioResource = createAudioResource(seekedAudioStream);
player.play(seekedAudioResource);
```