问题
I have a mp3 track at a URL http://mysite.com/MP3/track1.mp3. Downloading it to a local path with NSURLConnection and NSDocumentDirectory. I would like to start playing this track as soon as possible and not after it is completely downloaded. Is there a while to accomplish this? I have tried setting a "X" size limit, and once the download hits that number to start playing the track. But this only plays up to the point where it was initialized at the "X" size. Thanks in advance.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.inComingData appendData:data];
//NSLog(@"inComingData: %d", [inComingData length]);
CGFloat dataLen = [[NSString stringWithFormat:@"%d",[self.inComingData length]] floatValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
appDelegate.path = [documentDirectory stringByAppendingPathComponent:@"track.mp3"];
[self.inComingData writeToFile:appDelegate.path atomically:YES];
// NSInteger receivedLen = [self.inComingData length];
// if ((receivedLen >= 100000) && (!checked)) {
// //[appDelegate initAndPlay];
// [appDelegate playTrack];
// checked = TRUE;
// }
[appDelegate initPlayer];
}
回答1:
Instead of using AVFoundation, you should probably look into using CoreAudio or AudioToolbox.
Matt Gallagher has a good tutorial for this exact situation, using AudioToolbox: http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
来源:https://stackoverflow.com/questions/4248260/how-to-start-playing-avaudioplayer-while-downloading