How to start playing AVAudioPlayer while downloading?

亡梦爱人 提交于 2019-12-23 15:20:00

问题


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

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