2

My Client Web(HTML5实现)从Web服务器(Node.js实现)获取MP3音频数据,音频数据有16位PCM编码数据和通用二进制数据两种。

即使我遵循了一些示例和解释,我也无法解决我遇到的问题。

现在我想知道如何播放这个你能指导我为什么我在方法'context.decodeAudioData'中得到一个错误吗?

提前谢谢你的帮助。祝你好运!~

                  ----   html5 source code ----

<!DOCTYPE html>
<html lang="en" manifest="web.appcache">
<head>
<meta charset="utf-8" />
<title>index.html</title>
<script src="sounds/ufo.js"></script>
<script src="js/base64-binary.js"></script>
    <!-- base64-binary.js : https://github.com/alexgibson/offlinewebaudio/blob/master/js/base64-binary.js-->
      <script type="text/javascript">
            var context = new webkitAudioContext();
            var ws = new WebSocket("ws://localhost:8080");
            ws.binaryType = 'arraybuffer';
            ws.onmessage = function (evt) {
                 var byteArray = Base64Binary.decodeArrayBuffer(evt.data);
                context.decodeAudioData( byteArray, function(buffer) {
                            console.log("Success");
                            var src = context.createBufferSource();
                            src.buffer = buffer;
                            src.connect(context.destination);
                            src.noteOn(0);  //play immediately
                            console.log('Finished decoding');

                        },   function(error) {
                            console.log("Error");
                        }
                );
            };
            <script>
</body>
</html>


                    ----Node.js source code ----

    var net = require('net');
    var http = require('http');
    var fs = require('fs');

var server = http.createServer( function(req, res){
    fs.readFile('index.html', function (error, data) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(data);
    });  
}); server.listen("8888", "127.0.0.1");

    var WebSocketServer = require('ws').Server;
    wss = new WebSocketServer({port: 8080});

    wss.on('connection', function(ws) {
        console.log("웹서버에 연결됨..");
        var readStream = fs.createReadStream("1.mp3",
            {'flags': 'r',
                'encoding': 'binary',
                'mode': 0666,
                'bufferSize': 64 * 1024});

        readStream.on('data', function(data) {        
            ws.send(data,{binary: true ,mask: false } );
        });
    });
4

0 回答 0