2014-04-27 4 views
0

Web Audio API을 사용하여 오디오 파일의 시각화에서 브라우저 간 지원을 위해 WAAPISim Polyfill을 사용하고 있습니다. polyfill은 "WebAudioAPI => AudioDataAPI => Flash"순서로 다음 메소드를 사용하려고 시도합니다. 내가 JS에이 같은 오디오 파일을로드하고 있습니다 :WAAPISim Polyfill - .wav 폴백을 사용하여 .mp3을로드하는 방법

polyfill의 문서에서 언급 한 바와 같이
// load the specified sound 
function loadSound(url) { 
    var request = new XMLHttpRequest(); 
    request.open('GET', url, true); 
    request.responseType = 'arraybuffer'; 

    // When loaded decode the data 
    request.onload = function() { 

     // decode the data 
     context.decodeAudioData(request.response, function(buffer) { 
      // when the audio is decoded play the sound 
      playSound(buffer); 
     }, onError); 
    } 
    request.send(); 
} 

loadSound("audio/bird.wav"); 

이 polyfill는이 방법에 대한 WAV 형식을 지원합니다. "ArrayBuffer에서 createBuffer를, decodeAudioData에서 wav 형식 만 지원합니다."

지금은 .wav 만로드 중이지만 지원할 브라우저 대신 .mp3 (작은 파일)을로드하고 싶습니다. 구현이 .mp3과 함께 작동하고 그에 따라 올바른 파일을로드하는지 여부를 어떻게 확인할 수 있습니까?

Full demo example

답변

0

나는 polyfill 개발자에서이 응답을 가지고 다음 polyfill 브라우저가 .WAV 재생을 필요로하는 경우에만 waapisimContexts를 작성하므로이 방법은 waapisimContexts을 찾고 그것을 경우 .WAV를로드

if (typeof window.waapisimContexts != 'undefined'){ 
      loadSound("audio/bird.wav"); 
     } else { 
      loadSound("audio/bird.mp3"); 
     } 

정의됩니다. 그렇지 않으면 .mp3을로드합니다.

1

는 경우

(new Audio()).canPlayType("audio/mp3") 

반환 "maybe" 또는 "probably", 브라우저는 decodeAudioData에 mp3 파일을 지원합니다.