2012-01-18 21:18
浏览 243

div中的标题(“Content-Type:audio / mpeg”)

i have a file speakWord.php:

header("Content-Type: audio/mpeg");
$voice = file_get_contents('' . urlencode($_POST['language']) . '&q=' . urlencode($_POST['word']) . '');

<audio controls="controls" autoplay>
    <source src="<?php echo $voice; ?>"  />

Now i want to use jquery to load the result of this mp3 file and output it in a div:

.load("speakWord.php", {
    language: "nl",
    word: "test"

When i check the source of the div, it becomes something weird like:

<source src="��@��#a�F(��y2i���Ǵ1�L A�@���'!D����΂��8" �="">

I think the header information is lost in speakWord.php when loaded in a div, i think im missing a part..

图片转代码服务由CSDN问答提供 功能建议


 &lt;  ?php 
header(“Content-Type:audio / mpeg”); 
 $ voice = file_get_contents(''.wlenlencode($ _ POST ['language'])  。'&amp; q ='。urlencode($ _ POST ['word'])。''); 
&lt; audio controls =“controls”autoplay&gt; 
&lt; source src =“&lt;  ;?php echo $ voice;?&gt;“  /&gt; 
&lt; / audio&gt; 




 &lt; source src  =“��@��#a�F(��y2i���Ǵ1�LA�@���'!D����΂��8”�=“”&gt; 


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpiao0731 2012-01-18 21:28

    If you really have to proxy it over your script (if there was any Google API auth token), then package up the binary data into a base64 data: URL for the src= attribute.

       // Content-Type for whole PHP output remains text/html 
       $voice = file_get_contents('' . urlencode($_POST['language']) . '&q=' . urlencode($_POST['word']) . '');
    <audio controls="controls" autoplay>
        <source src="data:audio/mpeg;base64,<?php echo base64_encode($voice); ?>"  />

    However that's a larger transfer.

    The actual answer, as already given, is that you should separate content types. Your script should only return the audio data, not mixed html and binary content. In essence it should just be:

      header("Content-Type: audio/mpeg");
      readfile('' . urlencode($_POST['language']) . '&q=' . urlencode($_POST['word']) . '');

    So instead you had to construct the <audio> tag via jQuery (insted of ajax loading it):

    .html("<audio... src='speakWord.php?language=nl&word=test'></audio>");

    So this src= attribute retrieves the output from your speakWord script, which in turn pulls it over from the translate service.

    打赏 评论
  • douduanque5850 2012-01-18 21:21

    src= is meant to point to a URL, not be filled with binary data.

    You have to either

    • send binary data, with an appropriate header
    • OR send a HTML fragment, referencing (but not containing) the binary data.

    Not both at the same time.

    打赏 评论

相关推荐 更多相似问题