钛 Alloy - 'Global' 听众

Titanium Alloy - 'Global' Listener

我有多个 windows 'require' 实时栏,因此整个栏在所有 windows 上持续存在。但是,每当调用 'change' 函数时,它都会工作并记录,但是我的 $.livebar_datalbl.text 失败并出现以下错误:"undefined is not an object (evaluating '$.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')'')"

我的代码结构有误还是遗漏了什么?

index.js

(function constructor() {
    audioPlayer = Ti.Media.createAudioPlayer({
        url: 'https://allthingsaudio.wikispaces.com/file/view/Shuffle%20for%20K.M.mp3/139190697/Shuffle%20for%20K.M.mp3',
        allowBackground: true
    });

    audioPlayer.addEventListener('progress', function(e) {
        Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
    });

    audioPlayer.addEventListener('change', function(e) {
        $.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')';
        Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
    });

    window = Alloy.createController('listen').getView();
    window.open();
})();

livebar.xml

<Alloy>
    <View class="livebar">
        <View class="livebar_livelblcontainer">
            <Label class="livebar_livelbl">LIVE</Label>
        </View>
        <Label class="livebar_datalbl" id="livebar_datalbl">HELLO WORLD</Label>
        <ImageView id="livebar_playpausebtn" class="livebar_playpausebtn"/>
    </View>
</Alloy>

livebar.js

$.livebar_playpausebtn.addEventListener('click', function(event) {
    if (audioPlayer.playing || audioPlayer.paused) {
        audioPlayer.stop();
        if (Ti.Platform.name === 'android')
        {
            audioPlayer.release();
        }
    } else {
        audioPlayer.start();
    }
});

audioPlayer.addEventListener('progress', function(e) {
    Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});

audioPlayer.addEventListener('change', function(e) {
    $.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')';
    Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});

audioPlayer.addEventListener 事件将只监听您在其中创建音频播放器的控制器中的事件,在本例中为 index.js。在您的示例中,livebar.js 中的 audioPlayer.addEventListener 事件没有效果,因为没有要向其添加事件的音频播放器。

如果您想在 index.js 中安装音频播放器,然后更新实时栏并仍将实时栏保留在自己的视图+控制器中,您将需要跨控制器触发事件​​。为此,您可以使用 Ti.App.fireEvent

您可以在此处阅读更多内容 - 搜索 "Application-Level Events" 部分

http://docs.appcelerator.com/platform/latest/#!/guide/Event_Handling

您可以执行如下操作。

记住要小心应用范围的事件侦听器,您应该始终删除 当你通过下面的功能完成它们时的那些

Ti.App.removeEventListener("eventlistenername", eventfunctionname);

index.js

(function constructor() {
    audioPlayer = Ti.Media.createAudioPlayer({
        url: 'https://allthingsaudio.wikispaces.com/file/view/Shuffle%20for%20K.M.mp3/139190697/Shuffle%20for%20K.M.mp3',
        allowBackground: true
    });

    audioPlayer.addEventListener('progress', function(e) {
        Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
    });

    audioPlayer.addEventListener('change', function(e) {

        // set livebareText
        var livebareText = 'State: ' + e.description + ' (' + e.state + ')';

        // fire app wide event
        Ti.App.fireEvent("app:updateLivebar",livebareText);

        Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
    });

    window = Alloy.createController('listen').getView();
    window.open();
})();

livebar.js

$.livebar_playpausebtn.addEventListener('click', function(event) {
    if (audioPlayer.playing || audioPlayer.paused) {
        audioPlayer.stop();
        if (Ti.Platform.name === 'android')
        {
            audioPlayer.release();
        }
    } else {
        audioPlayer.start();
    }
});

// Add App eventlistener to listen for updateSingleProgessBar
Ti.App.addEventListener("app:updateLivebar", updateLivebar);

function updateLivebar(livebarText){

    $.livebar_datalbl.text = livebarText;
    Ti.API.info('State: ' + e.description + ' (' + e.state + ')');

}