Hyperloop eventlistener示例

时间:2017-12-19 10:20:04

标签: appcelerator appcelerator-hyperloop hyperloop

我尝试使用OrigamiEngine

创建一个AppceleratorHyperloop的flac文件播放器

它可以播放,暂停和停止播放器。但我不能让“addEventListener”模式工作。我阅读了一些关于委托方法的例子和内容。但我不是原生的iOS编码器,我的所有尝试都失败了。

有人能告诉我如何编写didChangeState函数的eventlistener吗?

这是我遗憾的不能正常工作的代码

//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;

运行演示项目下载:http://lightapps.de/files/flactest.zip

1 个答案:

答案 0 :(得分:0)

试一试:

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

我暂时无法测试,但我认为这对你有用。