Discord Player
Common actions

Playing Local Files

Learn how to play local files using discord-player

Wouldn't it be nice if you could play local files? Well, Discord Player got you covered. Discord Player natively supports local file playback with the help of Attachment extractor. Discord Player wont load file from your system unless you explicitly tell it to do so. Discord Player does this to prevent any unwanted security issues that may arise from loading files from your system. In conclusion, you can play local files by explicitly setting QueryType.FILE as the search engine.

How does it work?

  • Discord Player will validate your search query against the registered extractors. When using the default extractors provided by discord-player, only AttachmentExtractor will respond to your query if your search engine value is set to QueryType.FILE.
  • If the extractor responds to your query, Discord Player will use it to get track metadata.
  • AttachmentExtractor will search for the file in your system and return the metadata if it finds the file.
  • If the file is found, Discord Player will again run validation against it to make sure the file is playable.
  • If the file is playable, Discord Player will load the file and play it.

Example

playLocalFile.js
// Relative path also works, but absolute path is recommended
// this is a path to your local file which you want to play
// you can also use file:// urls to play local files
const filePath = 'E:/path/to/music.mp3';
 
await player.play(channel, filePath, {
  // in order to play local files, we need to explicitly tell discord-player to search that path in our file system
  searchEngine: QueryType.FILE, // QueryType.FILE tells discord-player to play from our file system,
  // ... (other options if you need)
});

This does not show the metadata of the track you are playing. In order to add support for the metadata, you need to install mediaplex on your system.

npm install --save mediaplex

Discord Player will automatically attempt to parse file metadata using mediaplex. The metadata includes title of the track, author of the track, duration of the track, etc.

Probing metadata in mediaplex is experimental and it may not 100% work.

On this page