2011-11-17 3 views
0

Je crée un programme qui charge des vidéos à partir d'un serveur, puis les enregistre localement. Le programme a bien fonctionné quand je le faisais de manière synchronisée. Quand je l'ai changé pour le faire de manière asynchrone, il se bloquerait lorsque la personne reviendrait à l'écran pré et essayerait d'appuyer sur l'un des boutons avec une mauvaise erreur de mémoire. Qu'est-ce qui se passe est le vedio charge et joue ok. Quand ils cliquent sur le bouton terminé, leiPhone Mon programme se bloque après avoir lu un fichier mp4 en utilisant le

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

s'exécute et passe à l'écran préliminaire. Maintenant à l'écran avant, s'ils cliquent sur un bouton, je reçois un

Je ne le suivant 1. Le fichier est grand donc je chnage mon [[NSMutableData alloc] initWithLebngth: 0]; à [[NSMutableData alloc] initWithCapacity: 200000] ;, n'a pas fonctionné.

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

aucune idée sur ce serait génial, ben coincé pendant des jours !!!! Ted

Répondre

0

Si vous revenez à l'écran avant essayez de changer votre code pour

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

parce que la fonction viewDidLoad sera appelée une seule fois lorsque la vue est chargé. mais la fonction viewWillAppear sera appelée chaque fois que cette vue apparaîtra.

+1

Merci Je n'ai jamais entendu parler de cette méthode avant, toujours en train d'apprendre de nouvelles choses :) –

Questions connexes