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 soundtrack has finished playing, the OnComplete callback will 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() → Number: seconds
GetType() → String: “MediaPlayer”
Pause()
Play()
Stop()
Boolean
Number: factor
Number: float
Number: percent
String: path to file ( “/absolute/...” or “relative/...” )
Object: { COMMAND }
function()
ply.Batch
Batch method calls to be able to set all object's properties at once.
Note that you need to specify each parameter (use “” or null to leave some out)
Inherited methods can be called by appending an underscore to the function name (ie. txt.Batch({ SetBackColor_: [“red”] })
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 the elapsed playtime in seconds.
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 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.SetPitch
Change the pitch of the playing song.
ply.SetSpeed
Change the speed of the playing song.
ply.SetVolume
Change the volume of the playing song.
ply.Stop
Stop playing a song.