Back

MediaPlayer

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

Create a MediaPlayer object using the CreateMediaPlayer method of the app object:

 player = app.CreateMediaPlayer();

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

 player.SetFile( "/Sys/Snd/Poing.ogg" );

To play sound files from your App sound assets, the file name passed to the SetFile method must start with "Snd/". For example:

 player.SetFile( "Snd/MyAppSound.mp3" );

When the sound file is ready for playback, the OnReady callback function will be called. Use the SetOnReady method to set the name of your OnReady callback function.

Example

function OnStart()
{
    player = app.CreateMediaPlayer();
    player.SetOnReady( player_OnReady );
    player.SetFile( "/Sys/Snd/Trill.ogg" );
}

function player_OnReady()
{
    app.ShowPopup( "OnReady" );
    player.Play();
}
    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 );

When the sound file has finished playing, the OnComplete callback function will be called. Use the SetOnComplete method to set the name of your OnComplete callback function.

Example - OnComplete

function OnStart()
{
    lay = app.CreateLayout( "Linear", "FillXY,VCenter" );

    btn = app.CreateButton( "Play", 0.4, 0.1 );
    btn.SetOnTouch( btn_OnTouch );
    lay.AddChild( btn );

    app.AddLayout( lay );

    player = app.CreateMediaPlayer();
    player.SetOnComplete( player_OnComplete );
    player.SetFile( "/Sys/Snd/Poing.ogg" );
}

function btn_OnTouch()
{
    player.SeekTo( 0 );
    player.Play();
}

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

See 'CreateMediaPlayer for more informations and a complete function list