Last active
November 13, 2017 15:56
-
-
Save hansemannn/771a58e13b0cca8c2d7d8f2946f410ce to your computer and use it in GitHub Desktop.
Use the AVAudioPlayer with Hyperloop.
This file contains 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
var AVAudioEngine = require('AVFoundation/AVAudioEngine'); | |
var AVAudioPlayerNode = require('AVFoundation/AVAudioPlayerNode'); | |
var AVAudioFile = require('AVFoundation/AVAudioFile'); | |
var NSBundle = require('Foundation/NSBundle'); | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#fff' | |
}); | |
var btn = Ti.UI.createButton({ | |
title: 'Play Audio' | |
}); | |
btn.addEventListener('click', function() { | |
var audioUrl = NSBundle.mainBundle.pathForResourceOfType('sounds/hit', 'mp3'); | |
var audioEngine = new AVAudioEngine(); | |
var myPlayer = new AVAudioPlayerNode(); | |
audioEngine.attachNode(myPlayer) | |
var audioFile = AVAudioFile.alloc().initForReadingError(audioUrl, null); | |
var audioError = null; | |
audioEngine.connectToFormat(myPlayer, audioEngine.mainMixerNode, audioFile.processingFormat) | |
myPlayer.scheduleFileAtTimeCompletionHandler(audioFile, null, null); | |
// NSError pass by reference? | |
audioEngine.startAndReturnError(audioError); | |
if (error) { | |
console.log(audioError.localizedDescription); | |
} | |
}); | |
win.add(btn); | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment