2016-08-09 8 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); 
    } 
    } 

이 또한 버튼을 설정 음소거 또는 재생 아이콘이 표시된 사용자 (다음 사용을 위해 localstorage에도 저장 됨)

다른 페이지에 나는 음악을 계속 사용하거나 중지하도록 다른 선택을 사용자에게주고 싶습니다 (페이지 전체에서 계속 재생).

나는 음악이 음소거 대신 사이에 약간의 지연이 2 번 재생을 시작, 당신은 버튼을 전환 할 때

//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); 
    } 
    } 

이상한 것은 지금 그것을 위해 코드의 동일한 종류를 사용합니다.

오디오 파일이 없기 때문에 새 오디오 파트를 제거하려고했지만 페이지가 다운됩니다.

기존 음악 파일 재생을 중지하거나 시작하는 것이 이상적입니다. 내보기 주위 컨트롤러 그게 전부를 사용하여

+0

이렇게하면 : var sound = true; var sound = localStorage.sound; var sound = eval (sound);'단 하나의 변수의 값을 다시 할당하지 않습니까? 따라서 한 가지 예에서 '사실'이지만 모든 것이 갑자기 소리로 저장되지만 사실이 아니라 결국에는 평가되지만 더 이상 소리로 저장되지 않으며 사실이 아닙니다. – zer00ne

+0

나는 if/else를 이렇게 만들려고 시도했다. if (localStorage.sound === null) { var sound = true; } else { var sound = localStorage.sound; var sound = eval (사운드); } 그러나 다른 페이지에서 stil을 누르면 첫 번째 컨트롤러에서 작업하는 대신 새 노래가 시작됩니다 (또한 사운드가 삭제됨 = true, 두 번째 컨트롤러의 부분) –

+0

첫 번째 컨트롤러는 페이지 전체에서 계속 재생되지만 두 번째 것은 동일한 것으로 인식하지 못하는 것 같습니다 –

답변

1

(이것은 2 개의 다른 컨트롤러 건너편), 난 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); 
    } 
    } 
});