I am making a program that loads videos from a server and then saves them locally. The programmed worked fine when i was doing it synchronizely. When I changed it to do it asynchronously , it would crash when the person return to the pre screen and try to press any of the buttons with a sig bad memory error.
What happens is the vedio loads and plays ok. When they click the done button the methed
- (void) moviePlayBackDidFinish:(NSNotification*)notification;{
[ mp stop];
// [ mp release];
[self dismissModalViewControllerAnimated: true];
}
exicutes and it goes to the pre screen. Now on the pre screen, if they click on any button, i get a
I did the following
1. the file is big so I chnage my
[[NSMutableData alloc] initWithLebngth:0]; to [[NSMutableData alloc] initWithCapacity:200000];, did not work.
- (void)viewDidLoad {
[super viewDidLoad];
// construct the url
NSString *mServerName=[ [ NSString alloc] initWithString:@"http://www.besttechsolutions.biz/projects/golfflix/" ];
NSString *mFullName=[ mServerName stringByAppendingString: mVedioName ];
NSURL *movieUrl=[[NSURL alloc] initWithString:mFullName];
// start the transmision
NSURLRequest *theRequest = [NSURLRequest requestWithURL:movieUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithCapacity:100000000];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
[movieUrl release];
[mServerName release];
}
///////////////////////////////////////////////////////////////////////////////////////////////
// background loading
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[receivedData setLength:0];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
}
- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
/////////////////////////////////////////////////////////////////////////////////
// Save vedio
NSFileManager *mFile= [NSFileManager defaultManager];
NSString *filename=[ NSHomeDirectory() stringByAppendingPathComponent:mVedioName];
[mFile createFileAtPath:filename contents: receivedData attributes:nil];
[ receivedData release ];
// play it
NSURL *fileUrl=[ NSURL fileURLWithPath:filename];
if (mp==nil)
{
mp=[[MPMoviePlayerController alloc] initWithContentURL: fileUrl];
[mp.view setFrame: self.view.bounds];
[self.view addSubview: mp.view];
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
[mp play];
}
else {
[mp stop];
mp.contentURL=fileUrl;
[mp play];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// movie reced save to file
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification;{
[ mp stop];
// [ mp release];
[self dismissModalViewControllerAnimated: true];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
any thoughts on this would be GREAT, ben stuck for days!!!!
Ted
If you are coming back to the pre screen then try to shift your code to
because the function viewDidLoad will be called only one time when the view is loaded.
but the function viewWillAppear will be called whenever that view will appear.