2011-11-17 15 views
0

播放mp4文件后崩溃我正在制作一个程序,从服务器加载视频,然后将其保存到本地。编程工作正常,当我同时做它。当我将其更改为异步执行时,它会在人员返回到预先屏幕时尝试按下任何带有内存不良错误的按钮时崩溃。iPhone我的程序使用

会发生什么是视频加载和播放好。当他们点击完成按钮时,会出现

- (void) moviePlayBackDidFinish:(NSNotification*)notification;{ 
    [ mp stop]; 
// [ mp release]; 
    [self dismissModalViewControllerAnimated: true]; 
} 

exicutes并进入预览屏幕。现在,前屏幕上,如果他们点击任何按钮,我得到一个

我做了以下 1.文件特别大,所以我chnage我 [NSMutableData页头] initWithLebngth:0];到[[NSMutableData alloc] initWithCapacity:200000];不起作用。

- (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; 
} 

对此的任何想法都将是伟大的,本卡住了几天! 特德

回答

0

如果你回来的前屏幕,然后尝试你的代码转移到

-(void)viewWillAppear:(BOOl)animated 
    { 
     //code 
    } 

因为功能viewDidLoad中会调用视图时加载一次。 ,但函数viewWillAppear将在该视图出现时被调用。

+1

谢谢你我从来没有听说过这种方法,总是学习新的东西:) –