Reputation: 7900
In my app i download an video from the web and i want to play will it downloaded.
I am using :
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
to save the data i get to a file with:
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++;
and then when i got to 50% from the video that was downloaded i want to start play it with 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;
}
any idea why it won't work ?
Upvotes: 2
Views: 774
Reputation: 677
you can't play a video like that. if you are downloading the way you do , you need to wait until finish.
if we think about the way you want , you are talking about streaming a video to your application. so to do that here's an example : http://geobray.com/2010/03/26/live-tv-streaming-to-iphone-with-http/
and it shows how to create streaming files i guess.
hope this helps..
Upvotes: 1