Highlighted

Save streamed MP3 file?

Participant ,
Apr 22, 2015

Copy link to clipboard

Copied

Our app includes a music player which streams MP3s from a server. We would like to be able to stream the MP3, and save it to a cache on disk once it has fully downloaded.

I am using the load() method of the Sound class to stream the MP3 so that playback can begin before the file is fully downloaded. I can listen for the Event.COMPLETE event to indicate that the MP3 has finished downloading, but the Sound class offers no way to access the original file and save it to disk (the extract() method returns uncompressed sound data which is much too large to write to the disk).

Is there any method to download an MP3 file that will allow us to stream the MP3 file as it is downloading, and save it to disk when the download is complete, without downloading it twice?

TOPICS
Development

Views

374

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Save streamed MP3 file?

Participant ,
Apr 22, 2015

Copy link to clipboard

Copied

Our app includes a music player which streams MP3s from a server. We would like to be able to stream the MP3, and save it to a cache on disk once it has fully downloaded.

I am using the load() method of the Sound class to stream the MP3 so that playback can begin before the file is fully downloaded. I can listen for the Event.COMPLETE event to indicate that the MP3 has finished downloading, but the Sound class offers no way to access the original file and save it to disk (the extract() method returns uncompressed sound data which is much too large to write to the disk).

Is there any method to download an MP3 file that will allow us to stream the MP3 file as it is downloading, and save it to disk when the download is complete, without downloading it twice?

TOPICS
Development

Views

375

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 22, 2015 0
Participant ,
Apr 30, 2015

Copy link to clipboard

Copied

Just wanted to double check on this one. Thanks if anyone has any tips!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 30, 2015 0
Engaged ,
May 01, 2015

Copy link to clipboard

Copied

Unless you can find an MP3 encoding library that could take the extract() data and convert it back with minimal effort, I think your only option is to save the mp3 using the File and FileStream classes. One thing you could try is to use a File object to download the file, and while it is downloading, you have the sound object play the data contents of the File object downloading the mp3. I just tried a quick test locally and it worked on my desktop. You will have to try it from an actual server to know how well it can work in that environment, but it is at least worth a shot.

import flash.filesystem.File;

import flash.filesystem.FileMode;

import flash.filesystem.FileStream;

import flash.events.ProgressEvent;

import flash.events.Event;

import flash.media.Sound;

import flash.net.URLRequest;

var file:File = File.applicationDirectory.resolvePath("gravity.mp3");

file.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);

file.addEventListener(Event.COMPLETE, onFileLoadComplete);

file.load();

var sound:Sound;

function onProgressEvent(e:ProgressEvent):void {

  if (sound == null) {

  file.removeEventListener(ProgressEvent.PROGRESS, onProgressEvent);

  sound = new Sound(new URLRequest(file.url))

  sound.play();

  }

}

function onFileLoadComplete(e:Event):void {

  file.removeEventListener(Event.COMPLETE, onFileLoadComplete);

  var saveFile:File = File.desktopDirectory.resolvePath("gravity2.mp3");

  var fs:FileStream = new FileStream();

  fs.open(saveFile, FileMode.WRITE);

  fs.writeBytes(file.data, 0, file.data.length);

  fs.close();

}

What this code is doing is creating a File object with, in this case, a path to an MP3 in the same folder as the test FLA I created. This would be a path to your server stored MP3. Then you setup the event listeners for progress and complete. I used progress to know that the file had actually started to be downloaded to then trigger the initialization of my Sound object. Depending on someones network connection and the size of the MP3, it could be a good idea to use progress to know that the file has gotten X percentage done before trying to play the file.

For testing purposes in my progress event, I just checked to see if the Sound object had been initialized yet, and if it hadn't then set sound equal to a new Sound object with the File.url property for the URLRequest() and then told it to play.

Once the File object dispatched the complete event to indicate that the download finished, I created a new File object for the purpose of saving it to the filesystem using the FileStream class. Using FileStream to save the file allows for no user interaction to save the MP3.

There may be better ways to handle my method, but I can't think of any at the moment. As far as I know, the second File object, which I named "saveFile", is required in order to actually save the downloaded MP3 object because Im not too familiar with File manipulation. The only downside I can think of is that you have a copy of the MP3 is memory and would need to null out the File object that loaded the MP3 to free up memory.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 01, 2015 0