mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
Add optional streamOptions to playStream() functions (#663)
* Adding playStream options Added optional volume and seek properties * fixed code formatting Edited my code styling to match with the rest of d.js * Added Default Variable * Added Documentation Added documentation for the streamOptions parameter Added a default value for seek in FFmpegConverterEngine Removed redundantcy * Fixed docs for voiceConnection * Change _updateMember's notSame check from && to || (#665) * Fixed git merge fail * Built Docs. Again......... * Hopefully finally fixed
This commit is contained in:
committed by
Schuyler Cebulskie
parent
4be7a1bfd1
commit
f7a542f12b
File diff suppressed because one or more lines are too long
@@ -203,6 +203,7 @@ class VoiceConnection extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Play the given file in the voice connection
|
* Play the given file in the voice connection
|
||||||
* @param {string} file The path to the file
|
* @param {string} file The path to the file
|
||||||
|
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
|
||||||
* @returns {StreamDispatcher}
|
* @returns {StreamDispatcher}
|
||||||
* @example
|
* @example
|
||||||
* // play files natively
|
* // play files natively
|
||||||
@@ -212,36 +213,42 @@ class VoiceConnection extends EventEmitter {
|
|||||||
* })
|
* })
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
playFile(file) {
|
playFile(file, { seek = 0, volume = 1 }) {
|
||||||
return this.player.playFile(file);
|
const options = { seek: seek, volume: volume };
|
||||||
|
return this.player.playFile(file, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays and converts an audio stream in the voice connection
|
* Plays and converts an audio stream in the voice connection
|
||||||
* @param {ReadableStream} stream The audio stream to play
|
* @param {ReadableStream} stream The audio stream to play
|
||||||
|
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
|
||||||
* @returns {StreamDispatcher}
|
* @returns {StreamDispatcher}
|
||||||
* @example
|
* @example
|
||||||
* // play streams using ytdl-core
|
* // play streams using ytdl-core
|
||||||
* const ytdl = require('ytdl-core');
|
* const ytdl = require('ytdl-core');
|
||||||
|
* const streamOptions = {seek: 0, volume: 1};
|
||||||
* voiceChannel.join()
|
* voiceChannel.join()
|
||||||
* .then(connection => {
|
* .then(connection => {
|
||||||
* const stream = ytdl('https://www.youtube.com/watch?v=XAWgeLF9EVQ', {filter : 'audioonly'});
|
* const stream = ytdl('https://www.youtube.com/watch?v=XAWgeLF9EVQ', {filter : 'audioonly'});
|
||||||
* const dispatcher = connection.playStream(stream);
|
* const dispatcher = connection.playStream(stream, streamOptions);
|
||||||
* })
|
* })
|
||||||
* .catch(console.log);
|
* .catch(console.log);
|
||||||
*/
|
*/
|
||||||
playStream(stream) {
|
playStream(stream, { seek = 0, volume = 1 }) {
|
||||||
return this.player.playStream(stream);
|
const options = { seek: seek, volume: volume };
|
||||||
|
return this.player.playStream(stream, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays a stream of 16-bit signed stereo PCM at 48KHz.
|
* Plays a stream of 16-bit signed stereo PCM at 48KHz.
|
||||||
* @param {ReadableStream} stream The audio stream to play.
|
* @param {ReadableStream} stream The audio stream to play.
|
||||||
|
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
|
||||||
* @returns {StreamDispatcher}
|
* @returns {StreamDispatcher}
|
||||||
*/
|
*/
|
||||||
playConvertedStream(stream) {
|
playConvertedStream(stream, { seek = 0, volume = 1 }) {
|
||||||
|
const options = { seek: seek, volume: volume };
|
||||||
this._shutdown();
|
this._shutdown();
|
||||||
const dispatcher = this.player.playPCMStream(stream);
|
const dispatcher = this.player.playPCMStream(stream, options);
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ nonce.fill(0);
|
|||||||
* @extends {EventEmitter}
|
* @extends {EventEmitter}
|
||||||
*/
|
*/
|
||||||
class StreamDispatcher extends EventEmitter {
|
class StreamDispatcher extends EventEmitter {
|
||||||
constructor(player, stream, sd) {
|
constructor(player, stream, sd, streamOptions) {
|
||||||
super();
|
super();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
@@ -30,7 +30,7 @@ class StreamDispatcher extends EventEmitter {
|
|||||||
};
|
};
|
||||||
this._startStreaming();
|
this._startStreaming();
|
||||||
this._triggered = false;
|
this._triggered = false;
|
||||||
this._volume = 1;
|
this._volume = streamOptions.volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class FfmpegConverterEngine extends ConverterEngine {
|
|||||||
this.emit('error', err);
|
this.emit('error', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
createConvertStream() {
|
createConvertStream(seek = 0) {
|
||||||
super.createConvertStream();
|
super.createConvertStream();
|
||||||
const encoder = ChildProcess.spawn(this.command, [
|
const encoder = ChildProcess.spawn(this.command, [
|
||||||
'-analyzeduration', '0',
|
'-analyzeduration', '0',
|
||||||
@@ -20,7 +20,7 @@ class FfmpegConverterEngine extends ConverterEngine {
|
|||||||
'-i', '-',
|
'-i', '-',
|
||||||
'-f', 's16le',
|
'-f', 's16le',
|
||||||
'-ar', '48000',
|
'-ar', '48000',
|
||||||
'-ss', '0',
|
'-ss', seek.toString(),
|
||||||
'pipe:1',
|
'pipe:1',
|
||||||
], { stdio: ['pipe', 'pipe', 'ignore'] });
|
], { stdio: ['pipe', 'pipe', 'ignore'] });
|
||||||
encoder.on('error', e => this.handleError(encoder, e));
|
encoder.on('error', e => this.handleError(encoder, e));
|
||||||
|
|||||||
@@ -25,8 +25,9 @@ class VoiceConnectionPlayer extends EventEmitter {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
convertStream(stream) {
|
convertStream(stream, { seek = 0, volume = 1 }) {
|
||||||
const encoder = this.converterEngine.createConvertStream();
|
const options = { seek: seek, volume: volume };
|
||||||
|
const encoder = this.converterEngine.createConvertStream(options.seek);
|
||||||
const pipe = stream.pipe(encoder.stdin);
|
const pipe = stream.pipe(encoder.stdin);
|
||||||
pipe.on('unpipe', () => pipe.destroy());
|
pipe.on('unpipe', () => pipe.destroy());
|
||||||
this.processMap.set(encoder.stdout, {
|
this.processMap.set(encoder.stdout, {
|
||||||
@@ -88,11 +89,13 @@ class VoiceConnectionPlayer extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
playPCMStream(pcmStream) {
|
playPCMStream(pcmStream, { seek = 0, volume = 1 }) {
|
||||||
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData);
|
const options = { seek: seek, volume: volume };
|
||||||
|
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
|
||||||
dispatcher.on('speaking', value => this.setSpeaking(value));
|
dispatcher.on('speaking', value => this.setSpeaking(value));
|
||||||
dispatcher.on('end', () => this.killStream(pcmStream));
|
dispatcher.on('end', () => this.killStream(pcmStream));
|
||||||
dispatcher.on('error', () => this.killStream(pcmStream));
|
dispatcher.on('error', () => this.killStream(pcmStream));
|
||||||
|
dispatcher.setVolume(options.volume);
|
||||||
this.dispatcher = dispatcher;
|
this.dispatcher = dispatcher;
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,16 @@ const BasePlayer = require('./BasePlayer');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
class DefaultPlayer extends BasePlayer {
|
class DefaultPlayer extends BasePlayer {
|
||||||
playFile(file) {
|
playFile(file, { seek = 0, volume = 1 }) {
|
||||||
return this.playStream(fs.createReadStream(file));
|
const options = { seek: seek, volume: volume };
|
||||||
|
return this.playStream(fs.createReadStream(file), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
playStream(stream) {
|
playStream(stream, { seek = 0, volume = 1 }) {
|
||||||
this._shutdown();
|
this._shutdown();
|
||||||
const pcmStream = this.convertStream(stream);
|
const options = { seek: seek, volume: volume };
|
||||||
const dispatcher = this.playPCMStream(pcmStream);
|
const pcmStream = this.convertStream(stream, options);
|
||||||
|
const dispatcher = this.playPCMStream(pcmStream, options);
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user