Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript createWriteStream完成,但文件不可用_Javascript_Node.js_Express_Watson - Fatal编程技术网

Javascript createWriteStream完成,但文件不可用

Javascript createWriteStream完成,但文件不可用,javascript,node.js,express,watson,Javascript,Node.js,Express,Watson,我的节点服务器使用Watson TTS api合成客户端使用POST发送的文本。使用计数器(1.ogg,2.ogg…)将TTs流写入名为的文件,完成后,返回文件名。客户端将其加载到音频标签并播放音频 虽然我在('finish')上使用createWriteStream,但有时会在客户端出现无法加载文件的错误,好像还没有加载文件一样: GET http://localhost/tts_server/audio/24.ogg 412 (Precondition Failed) Uncaught (i

我的节点服务器使用Watson TTS api合成客户端使用POST发送的文本。使用计数器(1.ogg,2.ogg…)将TTs流写入名为的文件,完成后,返回文件名。客户端将其加载到音频标签并播放音频

虽然我在('finish')上使用createWriteStream,但有时会在客户端出现无法加载文件的错误,好像还没有加载文件一样:

GET http://localhost/tts_server/audio/24.ogg 412 (Precondition Failed)
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
此外,如果计数器(以及文件名)为1,则它总是失败。如果要合成的文本很长,则通常会失败

客户端相关代码

$.post("http://192.168.0.4:3000", { text : text }, function(data){
  player.src = 'audio/' + data.count + '.ogg';
  player.oncanplaythrough = player.play();
}); 
var count = 10;

// function to TTS with Watson
function syntText(texto, cb){
    var tts = new TextToSpeechV1 ({
      username: '****',
      password: '****'
    });
    var params = {
      text: texto,
      voice: 'es-ES_EnriqueVoice',
      accept: 'audio/ogg;codecs=opus'
    };
    var ws = fs.createWriteStream('../audio/' + count + '.ogg');
    ws.on('finish', cb);
    tts.synthesize(params).pipe(ws);
}

// post 
app.post('/', function(req, res){
    syntText(req.body.text, function(){ //body parser here
        res.json({count : count});
        count++;
    }); 
});
服务器端相关代码

$.post("http://192.168.0.4:3000", { text : text }, function(data){
  player.src = 'audio/' + data.count + '.ogg';
  player.oncanplaythrough = player.play();
}); 
var count = 10;

// function to TTS with Watson
function syntText(texto, cb){
    var tts = new TextToSpeechV1 ({
      username: '****',
      password: '****'
    });
    var params = {
      text: texto,
      voice: 'es-ES_EnriqueVoice',
      accept: 'audio/ogg;codecs=opus'
    };
    var ws = fs.createWriteStream('../audio/' + count + '.ogg');
    ws.on('finish', cb);
    tts.synthesize(params).pipe(ws);
}

// post 
app.post('/', function(req, res){
    syntText(req.body.text, function(){ //body parser here
        res.json({count : count});
        count++;
    }); 
});

谢谢

可能是静态文件服务器没有检测到新文件或缓存它吗?@FaridNouriNeshat。我不知道你是什么意思。你能详细说明一下吗?谢谢,我认为无论是什么服务的文件,不能检测到新编写的文件,从而造成问题。但现在我想起来了,可能是浏览器缓存出现了问题。重置缓存后再尝试。或者,尝试使用随机数作为文件号。嗯。。。我要确保
tts.synthesis(params)
是否没有返回已经结束的流。。。这是我能想到的唯一一个在创建文件之前调用完成的回调的情况。。。