media

October 22, 2022

media

Stability: 2 - Stable

The media module provides support for multimedia programming. Currently, only music playback and media file scanning are supported. It will be added later in conjunction with the UI for video playback and other features.

Note that music is played asynchronously in the background when using this module, and will automatically end when the script is finished, so you may need to insert a statement such as sleep() to keep the script running. For example.

//play music
media.playMusic("/sdcard/1.mp3");
//let the music finish playing
sleep(media.getMusicDuration());

media.scanFile(path)

  • path {string} media file path

Scans the media file with path path and adds it to the media library; or notifies the media library to remove the file if it is deleted as well.

Media libraries include albums, music libraries, etc., so this function can be used to add an image file to an album.

// Request Screenshot
requestScreenCapture(false);
//Screenshot
var im = captureScreen();
var path = "/sdcard/screenshot.png";
//Save the image
im.saveTo(path);
//add the image to the album
media.scanFile(path);

media.playMusic(path[, volume, looping])

  • path {string} the path to the music file
  • volume {number} the volume to be played, a floating point number from 0 to 1, default is 1
  • looping {boolean} if or not to loop, if looping is true then loop, default is false.

Play the music file path. this function will not display any music playback screen. Throws an UncheckedIOException if the file does not exist or if the file is not in a supported music format.

// Play music
media.playMusic("/sdcard/1.mp3");
//let the music finish playing
sleep(media.getMusicDuration());

If you want to loop the music, use the looping parameter.

// Pass the third parameter as true to loop the music media.playMusic("/sdcard/1.mp3", 1, true); //wait for three plays sleep(media.getMusicDuration() * 3);

To play music using the music player, call the app.viewFile(path) function.

media.musicSeekTo(msec)

  • msec {number} the number of milliseconds, indicating the music progress

Adjust the current playback progress to the time msec. If no music is currently playing, calling the function will have no effect.

For example, to adjust the music to 1 minute as media.musicSeekTo(60 * 1000).

// Play music
media.playMusic("/sdcard/1.mp3");
//adjust to 30 seconds
media.musicSeekTo(30 * 1000);
//wait for music to finish playing
sleep(media.getMusicDuration() - 30 * 1000);

media.pauseMusic()

Pause music playback. If no music is currently playing, calling the function has no effect.

media.resumeMusic()

Continue music playback. Calling this function has no effect if no music is currently playing.

media.stopMusic()

Stop music. If no music is currently playing, calling the function has no effect.

media.isMusicPlaying()

  • Returns {boolean}

Returns whether music is currently playing.

media.getMusicDuration()

  • Rtn {number}

Returns the duration of the current music. The unit is milliseconds.

media.getMusicCurrentPosition()

  • Rtn {number}

Returns the current music position (how long it has been playing) in milliseconds.

Last update:
Contributors: Bruce