theAndDev
theAndDev

Reputation: 672

build an API for streaming audio/video from GridFS mongodb

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.

Upvotes: 1

Views: 1926

Answers (1)

theAndDev
theAndDev

Reputation: 672

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.

Upvotes: 0

Related Questions