2013-05-29 2 views
1

일부 텍스트를 말하고 싶습니다. 브라우저에서 제대로 형식이 설정된 URL을 입력하면 Google에서 오디오 파일 (mp3)을 가져올 수 있습니다. 나는 그것을 createSound하려고하면Soundmanager2에서 Google 번역 소리가로드되지 않습니다.

는하지만, 난 단지 불을 지르고에서 404 오류를 참조하십시오.

나는 이것을 사용하지만, 실패

soundManager.createSound( 
    {id:'testsound', 
    autoLoad:true, 
    url:'http://translate.google.com/translate_tts?ie=UTF-8&tl=da&q=testing'} 
); 

I 사전 인출 한 wget과 함께 고정 voiceprompts을, 그래서 그들은 페이지와 동일한 웹 서버의 로컬 MP3-파일로 있습니다. 하지만 나는 동적 프롬프트를 말하고 싶다.

답변

0

404 오류로 보아도 실제로는 도메인 간 제한이 적용됩니다. (

X-Content-Type-Options:nosniff 
X-XSS-Protection:1; mode=block 

그래서, 당신은 구글이하지 않는 한,이 클라이언트 측을 할 수 없습니다 :

그 (404)에서 응답 헤더 중 일부는 당신에게 무슨 일이 일어나고 있는지의 단서를 줄 것이다 아마도 그렇게하지 않을 것입니다). 오디오의 동적 로딩을하기 위해

, 당신은 어떤 Google 서버에서 최종 사용자에 의해 요청 된 파일 (wget과 또는 무엇이든을 통해) 다운로드 것 proxy on your own server을 설정하여이 X-도메인 제한 해결해야 어떤 데이터가 Google에서 오는 지간에 침을 뱉습니다.

soundManager.setup({ 
    url: 'swf', 
    onready: function() { 
     soundManager.createSound({ 
      id:'testsound', 
      autoLoad:true, 
      url:'http://translate.google.com/translate_tts?ie=UTF-8&tl=da&q=testing' 
     }); 
    } 
}); 

당신의 코드는 다음과 같아야합니다 :

soundManager.createSound({ 
    id:'testsound', 
    autoLoad:true, 
    url:'/audioproxy.php?ie=UTF-8&tl=da&q=testing' // Same domain! 
}); 

감사와 행운

코드 나는 문제를 재현하는 데 사용!

1

나는 이것이 오래 전에 질문을 받았다 볼 수 있지만, 나는 비슷한 문제에왔다, 내가하지만 오디오 태그와 함께, 그것은 크롬과 파이어 폭스에 대한 작동하게 할 수 있었다. 여기

는 나를 위해 트릭을 만든 코드 ...

var sayIt; 

function createSayIt() { 

    // Tiny trick to make the request to google actually work!, they deny the request if it comes from a page but somehow it works when the function is inside this iframe! 

    //create an iframe without setting the src attribute 
    var iframe = document.createElement('iframe'); 

    // don't know if the attribute is required, but it was on the codepen page where this code worked, so I just put this here. Might be not needed. 
    iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-pointer-lock'); 
    // hide the iframe... cause you know, it is ugly letting iframes be visible around... 
    iframe.setAttribute('class', 'hidden-iframe') 

    // append it to the body 
    document.body.appendChild(iframe); 

    // obtain a reference to the contentWindow 
    var v = iframe.contentWindow; 

    // parse the sayIt function in this contentWindow scope 
    // Yeah, I know eval is evil, but its evilness fixed this issue... 
    v.eval("function sayIt(query, language, cb) { var audio = new Audio(); audio.src = 'http://translate.google.com/translate_tts?ie=utf-8&tl=' + language + '&q=' + encodeURIComponent(query); cb && audio.addEventListener('ended', cb); audio.play();}"); 

    // export it under sayIt variable 
    sayIt = v.sayIt; 
} 

내가 우회 할 수 있었다고 생각 나는 여기

http://jsfiddle.net/royriojas/SE6ET/

을 만든 데모 페이지입니다입니다 제한. 앞으로 내가 모르는이 해킹을 잠재적으로 수정할 수 있습니다. 사실

당신은 또한 Text2Speech HTML5 API를 사용하려고 할 수 있습니다 ... 그렇지 않은 희망하지만,

IE 11이 해킹 작동하지 않습니다 ... 아직 시간이 매우 젊은이다 미래 나는 그것을 고치려고 노력할 수도있다