Back

CreateMediaPlayer

Hello World
Content:
- Methods

The MediaPlayer object can be used to play sound files from the phone or tablet.

ply = app.CreateMediaPlayer() → app object: MediaPlayer

Use the SetFile method of the MediaPlayer object to set the sound file to play. Supported sound file types include .ogg and .mp3.

When the sound file is ready for playback, the OnReady callback function will be called. Then you can start playing using the Start method.
If the sundtrack has finished playing, the OnComplete callback fill be called. All together you code could look like that:

Example - Playing Audio

function OnStart()
{
    player = app.CreateMediaPlayer();
    player.SetOnReady(Play);
    player.SetOnComplete( player_OnComplete );
    player.SetFile( "/Sys/Snd/Poing.ogg" );

}

function Play()
{
    player.Play();
}

function player_OnComplete()
{
    app.ShowPopup( "OnComplete" );
}
    Copy     Copy All       Run      

The SeekTo method can be used to adjust the playback position by passing in the time in seconds. Passing in 0 will set the playback position to the beginning of the sound file: player.SeekTo( 0 );

Methods

The following methods are available on the MediaPlayer object:

Close()
GetDuration() → number: seconds
GetPosition( options ) → object: { left, top, right, bottom }
GetType() → string: “MediaPlayer”
Pause()
Play( from )
Stop()
boolean
number
number: float
number: percent
number: seconds
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: “screen”, “px”
function()
ply.Close
Close the media player and thus make it unuseable for further use.
ply.GetDuration
Returns the total duration of the currently loaded song in seconds.
ply.GetPosition
Returns data about the position and size of the control.
If the screen option is given the position on the screen will be returned. Otherwise relative to the parent control.
The px options turns the relative values into pixels.
ply.GetType
Returns the control class name.
ply.IsLooping
Checks if the media player will replay the song from the begining if it has finished.
ply.IsPlaying
Checks if the media player is currently playing.
ply.IsReady
Checks if the media player is ready for use.
ply.Pause
Pause the current playing song.
ply.Play
Play the current loaded song from the last paused time or from a given start time in in seconds.
ply.SeekTo
Seek the player to a given time in seconds.
ply.SetFile
Load a sound file to the media player.
ply.SetLooping
Define whether the media player should replay the song when completed.
ply.SetOnComplete
Define a callback function which is called when a playing sound file has finished playing.
ply.SetOnReady
Define a callback function which is called when the player is ready for use.
ply.SetOnSeekDone
Define a callback function which is called when a seeking process is done.
ply.SetVolume
Change the volume of the playing song.
ply.Stop
Stop playing a song.