简体   繁体   English

咆哮JS暂停/恢复故障

[英]Howler js pause/resume trouble

I'm using the Howler js library to set a player in an app running through Electron. 我正在使用Howler js库在通过Electron运行的应用程序中设置播放器。 First everything seemed to work well, but after a few weeks using the app, a bug occurs repeatedly, but not constantly : the pause() function doesn't work. 首先,一切似乎都运行良好,但是使用该应用程序几周后,便反复出现了一个错误,但并没有持续发生: pause()函数不起作用。 Here's some piece of code : 这是一些代码:

Initialization : 初始化:

    var is_paused = true;
    var currentTrack = "track1";

    var tracks =    {"track1" :  new Howl({urls: ['path/to/track1.mp3']}),
                    "track2" : new Howl({urls: ['path/to/track2.mp3']}),
                    "track3" : new Howl({urls: ['path/to/track3.mp3']})
    };

Then I have a few buttons for play/resume, pause, stop, and play a specific track : 然后,我有几个按钮用于播放/恢复,暂停,停止和播放特定曲目:

$('#playS').click(function(){
        if (is_paused){
            tracks[currentTrack].play();
            is_paused = false;
        }
    });

$('#pauseS').click(function(){
        tracks[currentTrack].pause();
        is_paused = true;
    });

$('.trackBtn').click(function(){
        tracks[currentTrack].stop(); 
        currentTrack = $(this).attr('id');
        tracks[currentTrack].play();
        is_paused = false;
    });

The problem is that sometimes (generally after 40-45 min of a track playing), the pause() function just do nothing, which is really annoying cause I need to pause the track and play another 30 sec file and then resume the current track. 问题是有时(通常在播放曲目40-45分钟之后), pause()函数什么都不做,这真是令人讨厌,因为我需要暂停曲目并播放另外30秒的文件,然后恢复当前曲目。 I checked the console while the bug occurs, it says absolutely nothing. 发生错误时,我检查了控制台,它什么也没说。 I have no idea where the bug comes from, there's not a lot of information about how works the library. 我不知道该错误来自何处,关于该库的工作原理信息不多。 I really need some help here, thank's in advance. 我真的需要一些帮助,谢谢。

EDIT : one more thing, when pause() doesn't work, if I click play() the track plays from the begining, and I have control on this second instance of the track. 编辑:还有一件事情,当pause()不起作用时,如果我单击play() ,轨道将从头开始播放,并且我可以控制轨道的第二个实例。 It's like the first instance has reached end, but still playing. 就像初审已到尽头,但仍在播放。

Without knowing what version of Howler you're using, or what other code might be messing things up, there is one thing I think might help: You don't need to track the paused state. 在不知道您使用的是哪个版本的Howler或其他什么代码可能使事情混乱的情况下,我认为可能会有所帮助:您无需跟踪暂停状态。 The "playing" method takes care of that. “播放”方法可以解决这一问题。 I've made it work using something like this: 我已经使用以下方法使其工作:

// If it's paused, it's not playing, so... paused = !myHowlInstance.playing();

Another thing I noticed is that you have currentTrack = $(this).attr('id'); 我注意到的另一件事是,您有currentTrack = $(this).attr('id'); in your (I think it's a) stop button. 在您的(我认为是)停止按钮中。 Unfortunately I don't know JQuery well enough to know if there's anything wrong with that (I'm more of a Dojo fan myself). 不幸的是,我对JQuery的了解还不足以知道这是否有什么问题(我自己是Dojo爱好者)。 But it looks like currentTrack may be set to some value not in your list (which would break tracks[currentTrack] ). 但是看起来currentTrack可能被设置为不在列表中的某个值(这会破坏tracks[currentTrack] )。 You might want to go into the console and type tracks["track1"] , currentTrack etc. to see their values. 您可能需要进入控制台并键入tracks["track1"]currentTrack等以查看其值。 You can even do tracks[currentTrack].play(); 您甚至可以做tracks[currentTrack].play(); and see what happens. 看看会发生什么。 I wasn't sure if you knew you could do that (it was a huge help to me when I found out about it). 我不确定您是否知道可以做到这一点(当我发现这对我有很大帮助)。

And as far as the "un-pausing" starting from the beginning, I'm currently struggling with it myself; 至于从一开始就开始的“暂停”,我目前正在自己​​中挣扎。 at this time there's no clear way to do this (no resume() , pause(false) etc.), and I've seen a few more questions on the subject on Google and SO, so you're not alone. 目前,尚无明确的方法来执行此操作(没有resume()pause(false)等),而且我在Google和SO上还看到了关于该主题的更多问题,所以您并不孤单。 I've experimented with the seek method, but with no luck. 我已经尝试过seek方法,但是没有运气。 I'll post a comment if/when I reach a breakthrough on that. 如果/当我取得突破时,我会发表评论。 :) :)

EDIT: I figured out the play-from-beginning thing. 编辑:我想通从头开始。 It does involve "seek", and also the whole "instance ID" concept (which I never really understood the importance of from the documentation). 它确实涉及“搜索”,还涉及整个“实例ID”概念(我从文档中从未真正理解过它的重要性)。

Here's an example from a project I'm working on (also a game); 这是我正在从事的项目(也是游戏)的示例; it doesn't involve JQuery (sorry), but it should give you the gist of how to fix the problem. 它不涉及JQuery(很抱歉),但是它应该为您提供解决问题的要点。

var myBgMusic = new Howl(...);
var myMusicID = myBgMusic.play();   // The docs say play() returns an ID, and I'll be passing that to "seek" later.
var paused = false;
var saveSeek;
function TogglePause() {

    if (paused) {
        myBgMusic.play(myMusicID);
        myBgMusic.seek(saveSeek, myMusicID);
    } else {
        myBgMusic.pause();
        saveSeek = myBgMusic.seek(myMusicID);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM