A token for the file playing now (the same token again for the same
file as it is). Fails unsupported for a stream, an address or a
plugin's picture, stale when the source changed since ifCurrent.
Optionaloptions: Conditionalbusy (rejects) This plugin has 4 source requests or checks
running; try again when one has answered.
The range the user marked becomes a part of the plugin's list. The
token stands for the file; the plugin keeps it with the times, and the
name is what the list shows. There is no thumbnail API: while the file
plays, the plugin captures the part's first frame
(MediaCapture.capture) and
exports it into its own data directory; plugin.ui.image shows it
scaled down.
const file = await plugin.media.sources.remember();
// A new name each time: an export never overwrites a file.
const thumbnail = `${plugin.dataDirectory}/thumb-${file.token}-${Date.now()}.jpg`;
const snapshot = await plugin.media.capture({ at: start });
try {
await (await snapshot.export({ format: "jpeg", destination: thumbnail, quality: 80 })).done;
} finally {
await snapshot.dispose();
}
list.push({ token: file.token, name: file.name, start, end, thumbnail });
// In the row: plugin.ui.image(thumbnail, { maxSide: 64 })
The reference with its file looked at now: status says whether it
is still there as it was.
busy (rejects) This plugin has 4 source requests or checks
running; try again when one has answered.
At start the plugin checks each remembered file once. A part whose file
was moved or deleted reads missing, and its row offers Locate.
for (const token of tokens) {
const file = await plugin.media.sources.get(token);
if (file.status === "missing") console.log(`${file.name}: File missing`);
}
Ask the user where the file is now: the player shows its own file
dialog (named after the file, opened in its old folder) and, when the
user picks a file, the token stands for that file from then on.
Resolves null when the user cancels.
busy (rejects) Another dialog of the player is open, or this
plugin showed a Locate dialog less than 2 seconds ago.
The player forgets the token (an unknown one is ignored).
Every reference this plugin holds, each with its file looked at now.
busy (rejects) This plugin has 4 source requests or checks
running; try again when one has answered.
After the user cleared the list, the plugin forgets every file none of
its parts names. list gives all of the plugin's tokens, including
ones its own storage lost.
for (const file of await plugin.media.sources.list())
if (!used.has(file.token)) await plugin.media.sources.forget(file.token);
Open the remembered file in the player and play it from start
seconds (default its beginning): the user's "preview this part". It
is an ordinary open, as Player.open of that file would be (the
current source is replaced; the playlist treats it as any open). The plugin names the file by its token instead of keeping
its path; that is a convenience, not a boundary: once the file plays,
player.state.source shows its path, as for any
open. Resolves once the file is loaded. A file that is gone or changed
is refused before anything opens.
Optionaloptions: { start?: number }
Optionalstart?: numberSeconds of the file to start at; default 0.
source-missing (rejects) No file is at the token's path any
more; offer MediaSources.locate.
source-changed (rejects) The file changed since it was
remembered; offer MediaSources.locate.
Double-clicking a row of the plugin's list plays that part's file from the part's start. A file that has moved since is refused, and the row then offers Locate….
try {
await plugin.media.sources.open(part.token, { start: part.start });
} catch (error) {
// Moved or rewritten: the row shows "File missing · Locate…".
if ((error as { code?: string }).code === "source-missing") console.log("missing");
}
plugin.media.sources: remembered local files. The player gives a plugin a token for the file playing now; the token survives restarts and stands for that file in MediaCapture.export, MediaCapture.exportCheck and MediaSources.open.Uses beyond exports: a bookmarks or history plugin keeps tokens to reopen what the user watched at a position (MediaSources.open, with Locate… when a file moved), a resume plugin follows files across renames, a compare plugin switches between two remembered files, a chapters plugin keeps its marks per file without storing paths.
Remarks
Tokens hide paths as an API convenience, not as a security boundary: while the file plays,
player.state.sourceshows its path, and a plugin is an ordinary Node.js process, not sandboxed from the file system, that can find files by other means. What a token gives is a way to name the user's files without the plugin keeping their paths, and to find them again after a move.Only the player makes tokens, and only for a local file that is playing (the user opened it: the file dialog, a drop, the playlist, the command line); a stream, an address or a plugin's own picture is refused. A token belongs to the plugin that got it and means nothing to another. It gives the file's name, duration and stream facts, never its path or content. The player keeps the plugin's tokens in the profile until the plugin forgets them or is uninstalled, at most 500 per plugin. When a file has moved, MediaSources.locate lets the user find it again in the player's own file dialog.
Example: Remember the playing file and check it later
The plugin keeps the token in its storage and checks the file before using it.
rememberanswers for the file playing now;getlooks at the file on disk again, so a moved file readsmissing.Since
1.7.8