在我的应用程序中,我从网上下载视频,我想播放它会下载。
我正在使用:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
将数据保存到文件中:
if (currentBytes == 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [NSString stringWithFormat:@"%@.mp4", [[paths objectAtIndex:0] stringByAppendingPathComponent:@"1"]];
[[NSFileManager defaultManager] createFileAtPath:fileName contents:data attributes:nil];
handle = [[NSFileHandle fileHandleForUpdatingAtPath:fileName] retain];
[handle seekToEndOfFile];
}else {
if (!data) {
NSLog(@"");
}
[handle writeData:data];
}
currentBytes++;
然后当我从下载的视频中获得50%时,我想开始用AVPlayer播放它:
if ((percentComplete > 50)&&(flag == NO)) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [NSString stringWithFormat:@"%@.mp4", [[paths objectAtIndex:0] stringByAppendingPathComponent:@"1"]];
audioPlayer = [[AVPlayer playerWithURL:[NSURL fileURLWithPath:fileName]] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:audioPlayer] retain];
[avPlayerLayer setFrame:self.view.bounds];
[audioPlayer play];
[[self.view layer] addSublayer:avPlayerLayer];
flag = YES;
}
知道为什么它不起作用?
答案 0 :(得分:1)
如果我们考虑你想要的方式,你就是在谈论将视频流式传输到你的应用程序。所以这是一个例子:http://geobray.com/2010/03/26/live-tv-streaming-to-iphone-with-http/
它显示了我猜测如何创建流媒体文件。
希望这会有所帮助..