ladessa-ios
ladessa-ios

Reputation: 283

MPMoviePlayerController initWithContentURL delay the buttonClick

I have a for looping to load some videos in MPMoviePlayerController but I need stop it when the user click in backButton. The click event is detected only when the for with the movie loading finish. How could I resolve it?

There's my method which contains the loop

-(void) criarScrollMidias:(Evento *)evento {
    NSLog(@"criar scroll midias");

    NSMutableArray *listaImagens = evento.atracao.images;
    NSMutableArray *listaVideos = evento.atracao.videos;

    if([listaVideos count] > 0) {
        self.listaVideoPlayers = [[NSMutableArray alloc]init];
    }

    [self.scrollImVd setPagingEnabled:YES];

    if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
    {

        UIImageView *imgViewAtracao = [[UIImageView alloc]initWithImage:self.eventoAtual.atracao.imageLarge];
        [imgViewAtracao setContentMode:UIViewContentModeScaleAspectFit];
        [imgViewAtracao setFrame:CGRectMake(0, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
        [self.scrollImVd addSubview:imgViewAtracao];
    }

    for (int iCnt = 0; iCnt < [listaImagens count]; iCnt++) {
        NSString *imgURL = [listaImagens objectAtIndex:iCnt];
        UIImage *imagemAtracao = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];
        UIImageView *imgViewAtracao = [[UIImageView alloc]initWithImage:imagemAtracao];

        [imgViewAtracao setContentMode:UIViewContentModeScaleAspectFit];
        if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
            [imgViewAtracao setFrame:CGRectMake(0+self.scrollImVd.frame.size.width*(iCnt+1), 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
        else
            [imgViewAtracao setFrame:CGRectMake(0+self.scrollImVd.frame.size.width*iCnt, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];

        [self.scrollImVd addSubview:imgViewAtracao];
    }

    for (int iCnt = 0; iCnt < [listaVideos count]; iCnt++) {
        NSString *videoURLStr = [listaVideos objectAtIndex:iCnt];
        NSURL *videoURL = [NSURL URLWithString:videoURLStr];
        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
        [moviePlayer setShouldAutoplay:NO];
        [moviePlayer.view setBackgroundColor:[UIColor colorWithPatternImage:[self gerarPreviewVideo:videoURL]]];

        CGFloat posicaoInicial = 0.0;
        if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
            posicaoInicial = self.scrollImVd.frame.size.width * ([listaImagens count] + 1);
        else
            posicaoInicial = self.scrollImVd.frame.size.width * [listaImagens count];

        [moviePlayer.view setFrame:CGRectMake(posicaoInicial+self.scrollImVd.frame.size.width*iCnt, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];

        UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(moviePlayer.view.frame.origin.x+(self.scrollImVd.frame.size.width/2)-34, moviePlayer.view.frame.origin.y+(self.scrollImVd.frame.size.height/2)-33, 67, 66)];
        [playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        playButton.tag = iCnt;
        [playButton addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.scrollImVd addSubview:moviePlayer.view];
        [self.scrollImVd addSubview:playButton];
        [self.listaVideoPlayers addObject:moviePlayer];
    }

    if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
        [self.scrollImVd setContentSize:CGSizeMake(self.scrollImVd.frame.size.width * ([listaImagens count] + [listaVideos count] + 1), self.scrollImVd.frame.size.height)];
    else
         [self.scrollImVd setContentSize:CGSizeMake(self.scrollImVd.frame.size.width * ([listaImagens count] + [listaVideos count]), self.scrollImVd.frame.size.height)];
    [self.viewLoading setHidden:YES];
}

and there's the button click event:

-(IBAction)btnVoltarClick:(id)sender {
    [self.playerVideo stop];
    [self transicaoVoltar];
    [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}

I put a breakpoint in btnVoltarClick event and it is fired only when the method criarScrollMidias

finish

Upvotes: 0

Views: 38

Answers (1)

ladessa-ios
ladessa-ios

Reputation: 283

I solved my problem calling criarScrollViewMidias in a new thread

 [NSThread detachNewThreadSelector:@selector(criarScrollMidias:) toTarget:self withObject:self.eventoAtual];

Upvotes: 1

Related Questions