Class: MediaPlayer


CloudControl Pro 9 Docs / media / MediaPlayer

Class: MediaPlayer

media.MediaPlayer

Multimedia player. Provide more rich music playing management function. For example, play, pause, progress listener, etc.

Example

"nodejs";
const { MediaPlayer } = require("media");
const player = new MediaPlayer();
player.play("/sdcard/Music/test.mp3");
setInterval(() => {
    console.log(player.currentPosition);
}, 1000);

See

https://developer.android.com/reference/android/media/MediaPlayer

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new MediaPlayer()

Accessors

androidMediaPlayer

get androidMediaPlayer(): MediaPlayer

The native player of Android.

See

https://developer.android.com/reference/android/media/MediaPlayer

Returns

MediaPlayer


currentPosition

get currentPosition(): number

The current position of the music. Unit is millisecond.

See

https://developer.android.com/reference/android/media/MediaPlayer#getCurrentPosition()

Returns

number


duration

get duration(): number

The duration of the music. Unit is millisecond.

See

https://developer.android.com/reference/android/media/MediaPlayer#getDuration()

Returns

number


isPlaying

get isPlaying(): boolean

Whether the music is playing

See

https://developer.android.com/reference/android/media/MediaPlayer#isPlaying()

Returns

boolean

Methods

awaitForCompletion

awaitForCompletion(): Promise<void>

Wait for the music to be played

See

https://developer.android.com/reference/android/media/MediaPlayer#setOnCompletionListener(android.media.MediaPlayer.OnCompletionListener)

Returns

Promise<void>


pause

pause(): void

Pause playing

See

https://developer.android.com/reference/android/media/MediaPlayer#pause()

Returns

void


play

play(uri, volume?, looping?): Promise<void>

Play music. In 'async function' can be used keyword 'await' to wait for the music to be played.

Parameters

Name Type Description
uri string The music file path, or the URL.
volume? number The volume of the music, a float number between 0 and 1, default is 1.
looping? boolean Whether to loop the music, if looping is true, the music will be looped, default is false.

Returns

Promise<void>


prepare

prepare(): Promise<void>

Prepares the player for playback,. After setting the datasource and the display surface, you need to either call prepare() or prepareSync().

For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.

Example

"nodejs";
const { MediaPlayer } = require("media");
async function main() {
    const player = new MediaPlayer();
    player.setDataSource("https://www.example.com/test.mp3");
    await player.prepare();
    player.start();
    await player.awaitForCompletion();
}
main();

See

https://developer.android.com/reference/android/media/MediaPlayer#prepareAsync()

Returns

Promise<void>


prepareSync

prepareSync(): void

Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync().

For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.

If you call play() directly, you do not need to call prepareSync(), because play() will automatically call prepare().

Example

"nodejs";
const { MediaPlayer } = require("media");
const player = new MediaPlayer();
player.setDataSource("/sdcard/Music/test.mp3");
player.prepareSync();
player.start();
$autojs.keepRunning();

See

https://developer.android.com/reference/android/media/MediaPlayer#prepare()

Returns

void


release

release(): void

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around.

In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

See

https://developer.android.com/reference/android/media/MediaPlayer#release()

Returns

void


reset

reset(): void

Reset the player

See

https://developer.android.com/reference/android/media/MediaPlayer#reset()

Returns

void


seekTo

seekTo(msec): Promise<void>

Seeks to specified time position. Unit is millisecond.

See

https://developer.android.com/reference/android/media/MediaPlayer#seekTo(int)

Parameters

Name Type Description
msec number the offset in milliseconds from the start to seek to

Returns

Promise<void>


setDataSource

setDataSource(path): void

Set the data source, support local file path, or network url address.

See

https://developer.android.com/reference/android/media/MediaPlayer#setDataSource(java.lang.String)

Parameters

Name Type Description
path string The data source path, for example, /sdcard/test.mp3, http://www.example.com/test.mp3

Returns

void


setLooping

setLooping(looping): void

Set the looping

See

https://developer.android.com/reference/android/media/MediaPlayer#setLooping(boolean)

Parameters

Name Type Description
looping boolean Whether to loop the music, if looping is true, the music will be looped, default is false.

Returns

void


setScreenOnWhilePlaying

setScreenOnWhilePlaying(keep): void

See

https://developer.android.com/reference/android/media/MediaPlayer#setScreenOnWhilePlaying(boolean)

Parameters

Name Type Description
keep boolean Whether to keep screen on

Returns

void


setVolume

setVolume(leftVolume, rightVolume?): void

Set the volume

See

https://developer.android.com/reference/android/media/MediaPlayer#setVolume(float,%20float)

Parameters

Name Type Description
leftVolume number The volume of the left channel, a float number between 0 and 1.
rightVolume? number The volume of the right channel, a float number between 0 and 1, default is equal to left channel.

Returns

void


start

start(): void

Start playing

See

https://developer.android.com/reference/android/media/MediaPlayer#start()

Returns

void


stop

stop(): void

Stop playing

See

https://developer.android.com/reference/android/media/MediaPlayer#stop()

Returns

void