AudioModule Overview
AudioModule Overview
AudioModule Overview

AudioModule

The AudioModule class enables sound effects.

Example

//==============================================================================
// The following example demonstrates how to control audio playback by tapping
// on the screen.
//
// Project setup:
// - Import an audio file (M4A, Mono, 44.1kHz, 16 bit)
// - Create an audio playback controller and set the audio to the imported file
// - Insert a speaker and set the audio to the playback controller
// - Add the Tap Gesture capability to the project under Touch Gestures
//==============================================================================

// Load in the required modules
const Audio = require('Audio');
const TouchGestures = require('TouchGestures');

(async function() { // Enable async/await in JS [part 1]

  // Locate the playback controller in the Assets
  const audioController = await Audio.getAudioPlaybackController('audioPlaybackController0');

  // Play and loop the playback controller
  audioController.setPlaying(true);
  audioController.setLooping(true);

  // Create a boolean to determine if the audio is playing
  var isAudioPlaying = true;

  // Subscribe to tap gestures on the screen
  TouchGestures.onTap().subscribe(function() {

    // Switch the boolean controlling audio playback
    isAudioPlaying = !isAudioPlaying;

    // Start or stop the audio depending on the state of the boolean
    audioController.setPlaying(isAudioPlaying);
  });

})(); // Enable async/await in JS [part 2]

Properties

This module exposes no properties.

Methods

MethodDescription
getAudioPlaybackController
getAudioPlaybackController(name: string): Promise<PlaybackController>


Returns the playback controller with the specified name from the assets.
* name - the name of the audio playback controller as it appears in the Assets panel.

Classes

ClassDescription
PlaybackControllerEnables control of audio playback.
Ensure that the Audio Playback Controller in the Spark AR Studio project has a supported audio file assigned.

Was this article helpful?