2014-12-05 76 views
0

我嘗試使nodeJS中的音頻文件流式傳輸。但是,如果我從瀏覽器調用,但仍然可以正常工作,但是如果由於範圍未找到而導致來自任何設備的調用不起作用。req.headers.range無法找到來自android設備的調用?

//If call from Browser req.headers output like 
{ host: 'localhost:7060', 
    connection: 'keep-alive', 
    'user-agent': 'stagefright/1.2 (Linux;Android 4.4.4)', 
    'accept-encoding': 'gzip,deflate', 
    range: 'bytes=1-696319' } 

//If call from devices 
{ host: 'localhost', 
'user-agent': 'stagefright/1.1 (Linux;Android 2.3.6)' } 

只有輸出發生我不能在這裏找到任何範圍我可以做什麼呢?

app.js文件

var fs = require('fs'), 
queryString = require('querystring'); 

var query, URI; 

exports.streamMP3 = function(req, res){ 
res.header("Access-Control-Allow-Origin", "*"); 
res.header("Access-Control-Allow-Headers", "X-Requested-With"); 
res.header("Access-Control-Allow-Credentials", "true"); 
res.header("crossDomain","true"); 
res.header("Range","bytes=1-999"); 
//console.log(req); 
try{ 
    query = URI = null; 
    //console.log("METHOD:", req.method); 
    if(req.method=='POST') { 
     query = req.body; 
    }else{ 
     URI = decodeURIComponent(req.url); 
     if (req.url.indexOf('?') >= 0) { 
      query = queryString.parse(req.url.replace(/^.*\?/, '')); 
     } 
    } 
    console.log("QUERY:", query); 

    var audio_file_path = null; 
    var audio_file_id = null; 
    if(query && query.id){ 
     audio_file_id = query.id; 
     audio_file_path = './mp3/'+audio_file_id+'.mp3'; 
    }else{ 
     audio_file_path = './mp3/tmp.mp3'; 
    } 

    fs.readFile(audio_file_path, "binary", function(err, file) { 

     var header = {}; 
     // add content type to header 
     //req.headers.range = 'bytes=1-696319'; 

     console.log(req.headers); 
     //TODO: any more clean solution ? 
     if(typeof req.headers.range != 'undefined'){ 
      // browser wants chunged transmission 
      var range = req.headers.range; 
      var parts = range.replace(/bytes=/, "").split("-"); 
      var partialstart = parts[0]; 
      var partialend = parts[1]; 

      var total = file.length; 

      var start = parseInt(partialstart,10); 
      var end = partialend ? parseInt(partialend,10) : total-1; 

      var diff = end - start; 
      var value = parseInt((partialend*20)/100,10); 
      if(diff > value){ 
       end = start + value; 
      } 
      var chunksize = (end-start)-10000; 

      console.log(start , end); 
      //var end = parseInt((partialend*20)/100, 10); 
      header["Content-Range"] = "bytes " + start + "-" + end + "/" + (total); 
      header["Accept-Ranges"] = "bytes"; 
      header["Content-Length"]= chunksize; 
      header['Transfer-Encoding'] = 'chunked'; 
      header["Connection"] = "close"; 
      //console.log(start, end); 

      res.writeHead(206, header); 
      // yeah I dont know why i have to append the '0' 
      // but chrome wont work unless i do 
      //res.end(file.slice(start, chunksize), "binary"); 
      res.write(file.slice(start, end)+'0', "binary"); 
     }else{ 
      console.log("Unchunked"); 
      // reply to normal un-chunked request 
      res.writeHead(200, header); 
      res.write(file, "binary"); 
     } 
     res.end(); 
    }); 
}catch(e){ 
    console.log("TRY ERROR:", e); 
    res.end(); 
} 
} 

回答

2

並非所有的客戶會要求一個特定的範圍內。事實上,大多數人不會。您的代碼應該能夠通過簡單地將整個資源從字節0返回到結尾來處理這些請求。

相關問題