mattgallagher / AudioStreamer

A streaming audio player class (AudioStreamer) for Mac OS X and iPhone.
http://cocoawithlove.com
1.93k stars 553 forks source link

How to download the song when playing? #41

Open shingo1017 opened 12 years ago

shingo1017 commented 12 years ago

I wanna cache the song to file when playing, please who knows?

tomwilsn commented 12 years ago

in the - (void)handleReadFromStream:(CFReadStreamRef)aStream eventType:(CFStreamEventType)eventType callback just write the bytes to disk...

Of course then you will have the issue of this Streamer not supporting local file playback. I've managed to get it to do that, and it wasn't very hard - Basically replace the CFReadStream stuff with an NSURLConnection instead (which works fine with file URL's).

That said, AVQueuePlayer does a lot of what this does now, so if you don't mind only supporting iOS 4.0+ (which isn't really a big deal) You might be better off using that instead, unless you need the flexibility of this streamers low-level approach. Which I guess you might do if you wish to save the files as you play them, or any other number of reasons :)

foreveryh commented 12 years ago

it's helpful. I will try it.

emile818 commented 11 years ago

@Kabal @foreveryh how did you write the bytes to the disk? thx

    // USE this condition or something to validate
    NSString * escapedValue = [NSString stringWithFormat:@"%@", XXXXXX];
    NSData * mp3Ddata = [NSData dataWithBytes:bytes length:length];
    NSFileHandle* outputFile = [NSFileHandle fileHandleForWritingAtPath:escapedValue];
    [outputFile seekToEndOfFile];
    [outputFile writeData:mp3Ddata];
    [outputFile closeFile];

I tried this but didn't support all the kind of streams url for some reason

azone commented 11 years ago

@emile818 I just used C functions like fopen, fwrite, and fclose

bbalachandhar commented 9 years ago

@emile818 can you share the code to write audio into local directory?