2016-08-09 63 views
1

問題:angularjs播放同一首歌曲的2倍,同時與它們之間的一些秒的延遲, 我第一次叫音樂,我不喜歡這樣寫道:html5音頻無法正確使用角度|離子

//preset sound 
    var sound = true; 
    var sound = localStorage.sound; 
    var sound = eval(sound); 

    $scope.icon = "ion-volume-mute"; 
    var audio = new Audio('sound/song.mp3'); 
    audio.loop = true; 

     //change icon & sound based on localstorage variables 
     if (sound == true){ 
      window.localStorage.setItem("sound", true); 
      audio.play(); 
      $scope.icon = "ion-volume-mute"; 
      console.log(sound); 
     } 
     else{ 
      window.localStorage.setItem("sound", false); 
      console.log("ëlse " + sound); 
      audio.pause(); 
      $scope.icon = "ion-volume-high"; 
     } 
    //let the user toggle the sound (MUTE | Play) 
    $scope.soundControl = function() { 

    if (sound == false){ 
     sound = true; 
     audio.play(); 
     $scope.icon = "ion-volume-mute"; 
     window.localStorage.setItem("sound", true); 
     console.log(localStorage.sound); 
    } 
    else{ 
     sound = false; 
     audio.pause(); 
     $scope.icon = "ion-volume-high"; 
     window.localStorage.setItem("sound", false); 
     console.log(localStorage.sound); 
    } 
    } 

這也設置了一個按鈕顯示靜音或播放圖標的用戶(也存儲在本地存儲中供下次使用)

在另一頁上,我想給用戶另一個選擇,以繼續使用音樂或停止播放(不斷播放整個頁面)。

我用的是同一類的代碼現在

//sound 
    var sound = true; 
    var sound = localStorage.sound; 
    var sound = eval(sound); 

    $scope.icon = "ion-volume-mute"; 
    var audio = new Audio('sound/song.mp3'); 
    audio.loop = true; 

     //audio switch (PLAY | PAUZE) 
    $scope.soundControl = function() { 

    if (sound == false){ 
     sound = true; 
     audio.play(); 
     $scope.icon = "ion-volume-mute"; 
     window.localStorage.setItem("sound", true); 
     console.log(localStorage.sound); 
    } 
    else{ 
     sound = false; 
     audio.pause(); 
     $scope.icon = "ion-volume-high"; 
     window.localStorage.setItem("sound", false); 
     console.log(localStorage.sound); 
    } 
    } 

奇怪的是,當你切換按鈕,音樂開始播放2次與一些延遲,而不是與它靜音的。

我嘗試刪除新的音頻部分,但後來因爲沒有找到音頻文件而崩潰了頁面。

理想情況下,它只會停止或開始播放現有的音樂文件。 (這是在2個不同的控制器)

+0

當你這樣做:var sound = true; var sound = localStorage.sound; var sound = eval(聲音);'你不是在重新分配單個變量的值嗎?所以在一個例子中它是'真實的',但突然它被存儲爲聲音,但不是真實的,然後最終被避開,但它不再被存儲爲聲音,也從未如此。 – zer00ne

+0

我試圖做一個如果/其他像這樣if(localStorage.sound === null) { var sound = true; } else { var sound = localStorage.sound; var sound = eval(sound); } 但是在其他頁面中,stil開始播放一首新歌曲,而不是使用第一個控制器中的一首(也刪除了來自第二個控制器的聲音= true;部分) –

+0

第一個播放頁面始終播放,但第二個似乎沒有認出它是同一個 –

回答

1

通過使用在我的觀點的控制器多數民衆贊成,我可以把我的東西在1個控制器,這似乎像一個魅力

<body ng-app="starter"> 
    <div ng-controller="complete-app"> 
     <ion-nav-view></ion-nav-view> 
    </div> 
    </body> 

而且,像這樣的控制器開始工作

app.controller("complete-app", function($scope) { 
    $scope.icon = "ion-volume-mute"; 
    var audio = new Audio('sound/song.mp3'); 
    audio.loop = true; 

    if (localStorage.sound === null) 
    { 
    var sound = true; 
    } 
    else { 
    var sound = localStorage.sound; 
    var sound = eval(sound); 
    } 

    //change icon & sound based on localstorage variables 
    if (sound === true){ 
     window.localStorage.setItem("sound", true); 
     console.log("sound " + sound); 
     audio.play(); 
     $scope.icon = "ion-volume-mute"; 
     console.log(sound); 
    } 
    else{ 
     window.localStorage.setItem("sound", false); 
     console.log("sound " + sound); 
     audio.pause(); 
     $scope.icon = "ion-volume-high"; 
    } 

    //let the user toggle the sound (MUTE | Play) 
    $scope.soundControl = function() { 

    if (sound === false){ 
     sound = true; 
     audio.play(); 
     $scope.icon = "ion-volume-mute"; 
     window.localStorage.setItem("sound", true); 
     console.log(localStorage.sound); 
    } 
    else{ 
     sound = false; 
     audio.pause(); 
     $scope.icon = "ion-volume-high"; 
     window.localStorage.setItem("sound", false); 
     console.log(localStorage.sound); 
    } 
    } 
}); 
相關問題