Created
July 14, 2020 13:10
-
-
Save lassemt/77862745f724246667d074ac551c4ddb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================== | |
// 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'); | |
// Locate the playback controller in the Assets | |
const playbackController = Audio.getAudioPlaybackController('audioPlaybackController0') | |
// Play and loop the playback controller | |
playbackController.setPlaying(true); | |
playbackController.setLooping(true); | |
//============================================================================== | |
// Start and stop the audio by tapping on the screen | |
//============================================================================== | |
// Create a boolean to determine if the audio is playing | |
let 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 | |
playbackController.setPlaying(isAudioPlaying); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment