我是一个相对较新的程序员,试图在swift中创建一个ios视频流应用程序。我在node.js中编写了后端,并与mongoDB建立了mongoose连接。我已经能够使用以下代码和gridfs
将视频上传到数据库function (req, res, next) {
req.pipe(gfs.createWriteStream({ filename: 'filename'}));
res.send("success");
};
我尝试使用以下代码流式传输视频
gfs.findOne({_id: req.params.id}, function (err, file) {
if (err) {
return res.status(400).send(err);
} else if (!file) {
return res.status(404).send(' ');
} else {
res.header("Content-Type","video/mp4");
res.header("X-Content-Type-Options", "nosniff");
res.header("Accept-Ranges", "bytes");
res.header("Content-Length",file.length);
var readStream = gfs.createReadStream({_id: file._id});
readStream.on('open', function () {
console.log('Starting download...');
});
readStream.on('data', function (chunk) {
console.log('Loading...');
});
readStream.on('end', function () {
console.log('Video is ready to play');
});
readStream.on('error', function (err) {
console.log('There was an error with the download' + err);
res.end();
});
readStream.pipe(res);
}
});
当我在localhost上运行服务器并尝试通过谷歌浏览器访问视频时,我得到的只是默认播放屏幕,但没有视频。但是,最终我试图在ios应用程序上播放。当我在swift上尝试相同的事情时(使用传递给mpmovieplayer的本地主机url),也没有视频播放。我知道get请求正在进行,因为我的控制台使用filesize输出正确的响应。我甚至在前端摆弄了alamofire,并且可以在响应数据中看到视频的十六进制表示。任何人都可以帮助这个代码吗?我是否需要更新我的res.header以适应一些IOS规范?我是否应该为此使用alamofire?提前感谢您的回复。