@dderevjanik/termux-api
Version:
This library allows you to interact with your Android device from Node.js using termux-api
68 lines (64 loc) • 1.62 kB
text/typescript
import { exec } from "child_process";
export interface MediaPlayerOptions {
/**
* Command to execute
*/
command: "player" | "pause" | "stop" | "stop";
/**
* File to play, only if `command` is "player"
*
* @example "file_example_MP3_700KB.mp3"
*/
file: string;
}
/**
* Displays current playback information
* @returns
* `Status: Playing`
*
* `Track: file_example_MP3_700KB.mp3`
*
* `Current Position: 00:06 / 00:42`
*
* or
*
* `No track currently!`
*/
export async function mediaPlayerInfo(): Promise<string> {
// TODO: Return structured data
return new Promise<string>((resolve, reject) => {
const command = `termux-media-player info`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
const output = stdout.trim();
return resolve(output);
});
});
}
/**
* Play specified file using Media Player API
*/
export async function mediaPlayer(options: MediaPlayerOptions): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (options.command === "player" && !options.file) {
return reject(`Error: File path is required when using "player" command`);
}
const command = `termux-media-player ${options.command} ${
options.file || ""
}`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
return resolve();
});
});
}