Hyperloop eventlistener example

204 views Asked by At

I try to make a flac file player with Appcelerator and Hyperloopusing the OrigamiEngine

It works as I can play, pause and stop the player. But I can't get the "addEventListener" mode working. I read some examples and something about the delegate methods. But I'm not a native iOS coder and all my tryings failed.

Can anybody tells me how to code the eventlistener for the didChangeState function?

This is my unfortunately not working code

//Application Window Component Constructor
function ApplicationWindow() {

Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK;

var NSURL = require('Foundation/NSURL');
var ORGMEngine = require('OrigamiEngine/ORGMEngine');
var audioPlayer = new ORGMEngine();

var self = Ti.UI.createWindow({
    backgroundColor : '#ffffff'
});

var startStopButton = Ti.UI.createButton({
    title : 'Start/Stop Streaming',
    top : 100,
    width : 200,
    height : 40
});

var pauseResumeButton = Ti.UI.createButton({
    title : 'Pause/Resume Streaming',
    top : 140,
    width : 200,
    height : 40,
    enabled : false
});

self.add(startStopButton);
self.add(pauseResumeButton);

startStopButton.addEventListener('click', function() {

    console.log('audioPlayer.currentState: ' + audioPlayer.currentState);

    // When paused, playing returns false.
    // If both are false, playback is stopped.
    if (audioPlayer.currentState == 1) {
        audioPlayer.stop();
        audioPlayer.release();
        pauseResumeButton.enabled = false;
    } else {
        var url = NSURL.URLWithString('http://faq.fantasticlibrary.de/test.flac');
        audioPlayer.playUrl(url);
        setTimeout(function(){
            console.log(audioPlayer.metadata());
        }, 10000);
        pauseResumeButton.enabled = true;
    }
});

pauseResumeButton.addEventListener('click', function() {
    console.log('audioPlayer.currentState: ' + audioPlayer.currentState);

    if (audioPlayer.currentState == 1) {
        audioPlayer.pause();
    } else {
        audioPlayer.resume();
    }
});

var StateChangeDelegate = Hyperloop.defineClass('MyDelegate', 'NSObject', ['ORGMEngineDelegate']);

StateChangeDelegate.addMethod({
selector: 'engine:didChangeState:',
instance: true,
arguments: ['ORGMEngine', 'ORGMEngineState'],
callback: function (engine, state) {
    if (this.didChangeState) {
        this.didChangeState(engine, state);
    }
}
});

var delegate = new StateChangeDelegate();

delegate.didChangeState = function(ORMEngine,state) {
    console.log('does the state change???');
};

self.addEventListener('close', function() {
    audioPlayer.stop();
    audioPlayer.release();
});

return self;
}

module.exports = ApplicationWindow;

Running demo project to download: http://lightapps.de/files/flactest.zip

1

There are 1 answers

0
Jórdan Luiz Bisato On

Try it:

delegate.on('didChangeState', function(ORMEngine,state){
    console.log('does the state change???');
});

I can't test this in the moment, but i think that will work for you.