2017-03-14 4 views
0

바이트 배열 형식으로 제공되는 오디오 파일 (wav/mp3)을 재생할 때 아래 서비스를 사용하십시오.AngularJS - 바이트 배열 및 재생 오디오 파일 (Wav/MP3)

myAudioService.getAudioTone(userid).then(function (data) { 

     var context; // Audio context 
     var buf;  // Audio buffer 
     $window.AudioContext = $window.webkitAudioContext; 
     context = new AudioContext(); 

     $timeout(function() { 

     $scope.playByteArray = function(){ 

      var arrayBuffer = new ArrayBuffer(data.length); 
      var bufferView = new Uint8Array(arrayBuffer); 
      for (i = 0; i < data.length; i++) { 
       bufferView[i] = data[i]; 
      } 

      context.decodeAudioData(arrayBuffer, function(buffer) { 
       buf = buffer; 
       play(); 
      }); 
     } 

     $scope.play = function(audioBuffer){ 

      // Create a source node from the buffer 
      var source = context.createBufferSource(); 
      source.buffer = buf; 
      // Connect to the final output node (the speakers) 
      source.connect(context.destination); 
      // Play immediately 
      source.start(0); 

     }  


     if(data.length !== '' || data !== ''){ 
      $scope.playByteArray(); 
     } 
     }, 3000); 

}); 

함수는 호출되지만 예외는 발생합니다.

Uncaught (in promise) DOMException: Unable to decode audio data

어떻게 크롬, FF와 IE에서 나는 그것을 실행합니까?

P. $window$timeout은 이미 컨트롤러에 정의되어 있습니다.

답변

0

오류 메시지 arrayBuffer에 포함 된 내용에 포함 된 내용이 포함되어 있지 않습니다. 배열의 바이트가 인코딩 된 wav/mp3 파일과 동일한 지 확인해야합니다.

+0

배열의 바이트가 인코딩 된 wav/mp3 파일과 같은지 확인하는 방법이 있습니까? – Slimshadddyyy

+0

데이터가 어디에서 왔는지 표시하지 않기 때문에 잘 모름. 그러나 mp3/wav 파일은'fetch (url) .then (response => {context.decodeAudioData (response);})와 같은 것을 사용하여 디코딩 될 수 있습니다. –