melonJS
    Preparing search index...

    Function playTrack

    • Play the specified audio track, replacing whatever track was playing.

      The clip loops, and its name becomes the current track for stopTrack / pauseTrack / resumeTrack / getCurrentTrack. The previous track is stopped first, so only one track is ever playing: there is a single current-track slot, and a track left running after the slot had moved on could no longer be reached by any of those four.

      Calling it with the name already playing restarts that track from the beginning, the same as play on any other clip. Scene code that wants the bed to carry on across a screen change should not call this again, rather than rely on it doing nothing.

      Use play for sound effects: they are meant to overlap, and nothing here applies to them. The track can be one region of the clip, from its sprite map. An MP3 encoder pads the end of its file and Safari plays the padding, so a looping MP3 drops out for a moment at each loop there: a region of the loop's exact length keeps it gapless.

      Parameters

      • sound_name: string

        Audio track name (case-sensitive).

      • OptionalvolumeOrOptions: number | TrackOptions

        Playback volume, 0.0..1.0 (defaults to the current global volume), or a TrackOptions object with volume and the sprite region to loop on.

      Returns number

      The sound instance ID.

      when no clip of that name is loaded, raised before anything is stopped, so a mistyped name cannot silence the music already playing.

      me.audio.playTrack("menu_theme");
      me.audio.playTrack("battle_theme"); // the menu theme stops
      // loop on a region of exactly 12 s, declared when the clip is loaded:
      // { name: "ambience", type: "audio", src: "sfx/", sprite: { loop: [0, 12000] } }
      me.audio.playTrack("ambience", { sprite: "loop", volume: 0.6 });