【问题标题】:.play() not firing within directive using $watch.play() 没有在使用 $watch 的指令内触发
【发布时间】:2016-09-22 05:09:21
【问题描述】:

我正在使用 buzz 作为我的声音并且非常熟悉它的功能。但是,我自己和导师无法弄清楚为什么我的声音文件没有在我的指令中触发。

我的指令:

(function() {
    function clockTimer($interval, $window, STOP_WATCH) {

        return {
            templateUrl: '/templates/directives/clock_timer.html',
            replace: true,
            restrict: 'E',
            scope: {},
            link: function(scope, element, attributes) {

                scope.STOP_WATCH = STOP_WATCH; //see constants in app.js
                scope.startButton = 'Start Work';
                scope.breakButton = 'Take Break';
                scope.onBreak = false;  //boolean for alternating displaying of work-time or break
                var mySound = new buzz.sound("/sounds/blue.mp3", {
                  formats: ['mp3'],
                  preload: true
                });

                // @desc initiates holder of completed work sessions.  Increments by 1 once timer hits 0
                var completedWorkSessions = 0;
                // @desc Holds state for $interval call
                var promise;

                scope.$watch('STOP_WATCH.totalWorkTime', function() {
                  if (scope.STOP_WATCH.totalWorkTime === 0) {
                    mySound.play();
                    console.log(mySound);
                    console.log("im listening");
                  }
                });

我的观点

<section class="clock-container">
<div ng-show="!onBreak === true"> {{ STOP_WATCH.totalWorkTime | clockFormat }}
  <button ng-click="startStopButton(startButton)" ng-show="!onBreak === true">{{ startButton }}</button>
</div>
<div ng-hide="onBreak === false"> {{ STOP_WATCH.totalBreakTime | clockFormat }}
  <button ng-click="takeBreakButton(breakButton)">{{ breakButton }}</button>
</div>
</section>

Buzz 库包含在我的索引中,我尝试将 scope 添加到 mySound 的开头,在测试应用程序中测试了 .mp3(运行良好),但仍然无法播放声音。

这是我的控制台,它表明$watch 正在触发:

sound {sound: audio, volume: 80}
clockTimer.js:51 im listening

有什么想法或解决方案吗?我没有任何控制器,并且认为我不应该需要一个,因为它在控制台中成功触发?

【问题讨论】:

  • 我认为手表只会在变量STOP_WATCH.totalWorkTime 发生变化时触发。播放声音时会发生这种情况吗?
  • 正确。在我的应用程序中,它仅在我的倒计时时钟达到零时触发并运行该功能。所以它似乎应该在它应该触发的时候触发。
  • 您比任何人都更了解您的代码。您可以尝试使用调试器来查看实际发生的情况吗?
  • 我正在使用 atom,但对许多其他调试器并不熟悉。我确实创建了一个小型测试应用程序来证明我的声音文件在相同条件下工作。但是,此测试仅在带有脚本的静态 index.html 中完成。

标签: javascript angularjs


【解决方案1】:

我从 Buzz 对象中删除了 formats: ['mp3'],。您可以使用formats: ['mp3'], 或.mp3,但不能同时使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2016-07-17
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多