Re: Caching Streaming video
Re: Caching Streaming video
- Subject: Re: Caching Streaming video
- From: Nathan Day <email@hidden>
- Date: Tue, 28 Jun 2011 03:17:19 +1000
OK here is a more detailed explanation of what I am doing
I start downloading the video file with a NSURLConnection
I then implement the received data delegate method like the following.
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)aData
{
bytesFetched += aData.length;
if( bytesFetched > kBytesRequiredBeforeStart && !hasCachedData ) // kBytesRequiredBeforeStart = 160000
{
[[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheHasDataNotification object:self];
hasCachedData = YES;
}
[self.fileHandle writeData:aData]; // this file handle is not closed until after the video has finished downloading
}
The fileHandle is created like this
- (NSFileHandle *)fileHandle
{
if( fileHandle == nil )
{
NSError * theError = nil;
cachedURL = [[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingFormat:@"/%@", kTempFileName]] retain];
[[NSFileManager defaultManager] createFileAtPath:[self.cachedURL path] contents:nil attributes:nil];
fileHandle = [[NSFileHandle fileHandleForWritingToURL:self.cachedURL error:&theError] retain];
if( fileHandle == nil )
[[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheErrorOccuredNotification object:self];
}
return fileHandle;
}
I also have a did finish delegate handling method like this
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if( hasCachedData == NO )
{
[[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheHasDataNotification object:self];
hasCachedData = YES;
}
hasFinishedCaching = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheDidFinishLoadingNotification object:self];
}
I then have a method to observer the notification like the following
- (void)videoURLCacheHasDataNotification:(NSNotification *)aNotification
{
[self.videoController play];
}
where the videoController is an instance of MPMoviePlayerController created like below, cachedURL is the same one defined above.
- (MPMoviePlayerController *)videoController
{
if( videoController == nil )
{
NSURL * theURL = self.videoURLCache.cachedURL;
NSLog( @"Video URL = '%@'", theURL );
videoController = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
videoController.shouldAutoplay = NO;
[videoController setFullscreen:NO animated:NO];
videoController.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight;
videoController.repeatMode = MPMovieRepeatModeOne;
videoController.controlStyle = MPMovieControlStyleEmbedded;
videoController.view.frame = self.videoView.bounds;
[videoView addSubview:self.videoController.view];
}
NSParameterAssert(videoController != nil);
return videoController;
}
Now this work fine in the iPhone simulator but on my actually iPodTouch it stops at what I am assuming is 160000, (I haven't check this). To get the entire video to play on my iPodTouch I have to move the [self.videoController play]; to a notification observer for the video did finish loading notification.
On 26/06/2011, at 8:05 PM, Nathan Day wrote:
> I am trying to playback video in my iOS app while I am loading and caching it at the same time. I fetch the video using a NSURLConnection and then store it in a local file, I start video playback of the local video file after a certain number of bytes are received. I have it working great in the simulator, I can start playing the video before I have received all of it, but when I go to run my app on my iPodTouch, I can only seem to play up to the number of bytes I had already received before I started playback. I can only play the entire video if I wait until I have receive the entire file before I start playback. I can also get the video to play completely if I stop the failed attempt with a [video stop] message and then start playing it again, pausing the video only doesn't work.
>
> Has anybody got this working, is it possible.
>
>
> Nathan Day
> Software Engineer
> home page: http://homepage.mac.com/nathan_day/
Nathan Day
Software Engineer
home page: http://homepage.mac.com/nathan_day/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden