代码之家  ›  专栏  ›  技术社区  ›  Scott Robinson

钛合金-“全球”监听器

  •  0
  • Scott Robinson  · 技术社区  · 7 年前

    我有多个“需要”livebar的窗口,这样整个栏就可以在所有窗口中保持。然而,无论何时调用“change”函数,它都会工作并记录,然而我的$。livebar_datalbl。text失败,并出现以下错误:“未定义不是对象(正在计算“$.livebar\u datalbl.text=”状态:“+e.description+”(“+e.State+”)“”)

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

    指数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 + ')');
    });
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Dieskim    7 年前

    这个 audioPlayer.addEventListener 在本例中,事件将仅侦听您在其中创建audioPlayer的控制器中的事件 指数js公司 . 在您的示例中 音频播放器。addEventListener 中的事件 livebar。js公司 没有任何效果,因为没有可添加事件的audioPlayer。

    如果你想要音频播放器 指数js公司 然后更新livebar,并将livebar保持在自己的视图+控制器中,您需要跨控制器触发事件。要做到这一点,你可以利用 Ti.App.fireEvent

    你可以在这里阅读更多-搜索 “应用程序级事件” 部分

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

    你可以做下面这样的事情。

    当您通过下面的功能完成这些操作时

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

    指数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 + ')');
    
    }