下载时如何开始播放AVAudioPlayer? [英] How to start playing AVAudioPlayer while downloading?

查看:61
本文介绍了下载时如何开始播放AVAudioPlayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在URL http://mysite.com/MP3/track1.mp3 . 使用NSURLConnection和NSDocumentDirectory将其下载到本地路径. 我想尽快开始播放此曲目,而不是在完全下载后开始播放.是否有一段时间可以做到这一点?我尝试设置"X"大小限制,一旦下载达到该数字,便开始播放曲目.但这只会起到将其初始化为"X"大小的程度.提前致谢.

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];

}

推荐答案

您可能应该考虑使用CoreAudio或AudioToolbox,而不是使用AVFoundation.

Instead of using AVFoundation, you should probably look into using CoreAudio or AudioToolbox.

Matt Gallagher使用AudioToolbox针对这种情况提供了很好的教程: http://cocoawithlove.com/2008/09/streaming -and-playing-live-mp3-stream.html

Matt Gallagher has a good tutorial for this exact situation, using AudioToolbox: http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html

这篇关于下载时如何开始播放AVAudioPlayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆