discordapi.player

Module Contents

Classes

AudioSource

AudioSource providing the Opus packet to send to Voice server.

FFMPEGAudioSource

AudioSource that utilizes FFMPEG commandline tool to play audio.

AudioPlayer

Player to play AudioSource to given VoiceClient.

SingleAudioPlayer

AudioPlayer that disconnects right after the source finishes.

QueuedAudioPlayer

AudioPlayer with audio queue implemented.

class discordapi.player.AudioSource

AudioSource providing the Opus packet to send to Voice server.

prepare(self)

Preparation to do before the song plays.

This method gets called before it starts playing. You can e.g. download the audio file, spawn a process, connect to remote server, etc etc.

Whether to override this method or not is your choice.

abstract read(self)

Method that returns a packet of Opus audio stream to be sent.

Return empty value (b’’) if the audio stream has finished.

Each packet contains around 20ms worth of data. If you’re sending Opus data stored in Ogg container(which is also what FFMPEG uses), You can extract each packets from it and return it right away.

This method should be implemented by the inherited class.

cleanup(self)

Method to clean things after audio stops playing.

This method gets called after the audio stops. You can e.g. remove the used audio file, kill the used process, etc etc.

Whether to override this method or not is your choice.

class discordapi.player.FFMPEGAudioSource(filename, inputargs=None, outputargs=None, ffmpeg='ffmpeg')

Bases: AudioSource

Inheritance diagram of discordapi.player.FFMPEGAudioSource

AudioSource that utilizes FFMPEG commandline tool to play audio.

This class requires ffmpeg to be installed on the system.

Initialize the ffmpeg settings.

Parameters
  • filename – Input file to process. gets passed straight into -i.

  • inputargs – Options to be used in input stream.

  • outputargs – Options to be used in output stream.

  • ffmpeg – directory to the binary. defaults to “ffmpeg”.

prepare(self)

Starts FFMPEG process and initializes Ogg parser.

read(self)

Method that returns a packet of Opus audio stream to be sent.

Return empty value (b’’) if the audio stream has finished.

Each packet contains around 20ms worth of data. If you’re sending Opus data stored in Ogg container(which is also what FFMPEG uses), You can extract each packets from it and return it right away.

This method should be implemented by the inherited class.

cleanup(self)

Method to clean things after audio stops playing.

This method gets called after the audio stops. You can e.g. remove the used audio file, kill the used process, etc etc.

Whether to override this method or not is your choice.

class discordapi.player.AudioPlayer(client=None, source=None, callback=None)

Bases: discordapi.util.StoppableThread

Inheritance diagram of discordapi.player.AudioPlayer

Player to play AudioSource to given VoiceClient.

This player is designed to be managed separately from the voice client. it will keep on running after the source finishes playing or client stops. you can also change which client to play to, if you want.

This player inherits StoppableThread, will run on a separate thread, and is able to be stopped by calling .stop method.

client

DiscordVoiceClient to play to.

source

Source to play with.

callback

Function to be called after the source finishes.

_resumed

Event indicating if the player has been paused or not.

_ready

Event indicating if the player is ready to play.

_sent_silence

bool indicating if the five frame of silence has been played after the player pauses playing.

loop

Integer tracking how many packets have been sent so far, to determine when to send the next packet.

start_time

Integer indicating when the transmission has started, to determine when to send the next packet.

Initializes player.

Parameters
  • client – Client to play to.

  • source – Source to play with.

  • callback – A function to be called after the song finishes playing. this function receives a single argument which is player object.

set_client(self, client)

Sets client. Also checks if client is in the right type.

set_source(self, source)

Sets source. Also checks if source is in the right type.

set_callback(self, callback)

Sets callback. Also checks if callback is in the right type.

play(self, source=None)

Starts playing the source.

It can set the source if provided. This method will refuse playing if either client or source is not set.

stop(self)
pause(self)
resume(self)
_prepare_play(self)

Initializes needed attributes to start playing.

run(self)

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

_send_and_wait(self, data)
_source_is_finished(self)
class discordapi.player.SingleAudioPlayer(client=None, source=None, callback=None)

Bases: AudioPlayer

Inheritance diagram of discordapi.player.SingleAudioPlayer

AudioPlayer that disconnects right after the source finishes.

Initializes player.

Parameters
  • client – Client to play to.

  • source – Source to play with.

  • callback – A function to be called after the song finishes playing. this function receives a single argument which is player object.

_source_is_finished(self)
class discordapi.player.QueuedAudioPlayer(client=None, source=None, callback=None)

Bases: AudioPlayer

Inheritance diagram of discordapi.player.QueuedAudioPlayer

AudioPlayer with audio queue implemented.

player keeps on playing until the queue gets empty. .play method can be called again to resume playing.

Initializes player.

Parameters
  • client – Client to play to.

  • source – Source to play with.

  • callback – A function to be called after the song finishes playing. this function receives a single argument which is player object.

set_source(self, source)

Sets source. Also checks if source is in the right type.

add_to_queue(self, source)

Adds list of source to the queue. can be used with single object.

_update_source(self)
play(self, source=None)

Starts playing the source.

It can set the source if provided. This method will refuse playing if either client or source is not set.

_source_is_finished(self)