我正在尝试播放部分下载的文件(我希望音频在有足够的下载数据时开始播放)。
我尝试了以下方法:
self.mutableData = nil;
self.mutableData = [NSMutableData data];
self.mutableData.length = 0;
GTMHTTPFetcher *fetcher =
[[self driveService].fetcherService fetcherWithURLString:song.filePath];
[fetcher setReceivedDataBlock:^(NSData *dataReceivedSoFar) {
[self.mutableData appendData:dataReceivedSoFar];
if (!self.audioPlayer.playing && self.mutableData.length > 1000000)
{
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:self.mutableData error:nil];
[self.audioPlayer play];
}
}];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil)
{
NSLog(@"NSURLResponse: %@", fetcher.response.);
}
else
{
NSLog(@"An error occurred: %@", error);
}
}];
但它开始播放前 1-3 秒,然后应用程序要么崩溃,要么反复播放 1-3 秒,直到下载完成,然后播放整首歌曲。
我能以某种方式实现这一点AVPlayer
,或者解决这个问题吗?