build an API for streaming audio/video from GridFS mongodb

人盡茶涼 提交于 2019-12-08 08:04:34

问题


I have tried the solution given in the link stream audio/video files from gridFS on the browser

Still when I use my code the file gets downloaded or it plays with the default browser player. My code is as:

header('Content-type: audio/mpeg;');

$stream = $file->getResource();
while (!feof($stream)) {
      echo fread($stream, 8192);
      }

I actually want a solution to build an API so that I can retrieve the audio/video from mongodb GridFS and play it streaming from a phone application.

Help is urgently needed. Any help on the topic would be welcome.


回答1:


I found a way to make an API for playing the file straight from the mongoDB GridFS in a HTML audio video image widget. The only problem with it is that it uses the data retrieved from mongoDB as a base64 data. Now the problem that i am talking about is discussed in this link:

http://davidbcalhoun.com/2011/when-to-base64-encode-images-and-when-not-to.

I hope you find that useful before deciding to use the solution I am using. My solution is as follows:

$stream = $file->getResource();
$stringcoded = stream_get_contents($stream); //converts the stream to string data
$encoded = base64_encode($stringcoded);  //encodes string data to base64

Now that you have the audio, video or image data encoded in base64, you just have to echo the data in the 'src' portion of the html5 widget.

I got this solution from a very useful blog. If you need more help on it please go through it:

http://www.iandevlin.com/blog/2012/09/html5/html5-media-and-data-uri

Any enhanced solution to this problem is more than welcome.



来源:https://stackoverflow.com/questions/18206715/build-an-api-for-streaming-audio-video-from-gridfs-mongodb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!