UITableViewCell - Play Multiple Movie Files from an Array

Discussion in 'iPhone/iPad Programming' started by CeiJay, Jul 17, 2010.

  1. macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #1
    Hello All

    iOS4 - iPhone 4 & 3GS

    I have a Table based application using Custom UITableViewCell's. The custom Cell has a UIButton within it for playing a movie file. At the moment I have Arrays to populate the information within each cell depending on the Indexpath. All information within the Cell is placed within cellForRowAtIndexPath and all actions are within didSelectRowAtIndexPath.

    The UIButton within each cell plays a movie using the standard MoviePlayerViewController like so (other code not shown)

    Code:
    - (void)loadMoviePlayer
    
    {
    
    // Play movie from the bundle NSString *path = [[NSBundle mainBundle] pathForResource:@"Mov2" ofType:@"mp4" inDirectory:nil];
    
    // Create custom movie player
    moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:path] autorelease];
    
    // Show the movie player as modal [self presentModalViewController:moviePlayer animated:YES];
    
    // Prep and play the movie [moviePlayer readyPlayer];
    }
    The UIButton within the CustomCell is linked to the following:

    Code:
    -(IBAction)playMovie {
    
    [self loadMoviePlayer];
    }
    Here is an example of Audio being played within didSelectRowAtIndexPath. A Array controllers the stringWithFormat using the TabelView IndexPath. Each Cell plays a certain file...

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSUInteger row = [indexPath row]; NSString *rowTitle = [filmSound objectAtIndex:row];
    
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], rowTitle]]; NSError *error; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    if (player != nil) { [self setTheAudio: player]; [player release], player = nil;
    
    [self.theAudio setDelegate:self];
    
    [self.theAudio play]; }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
    
    I need to be able to do the same the the movie file actioned by the UIButton in the CustomCell. The - (void)loadMoviePlayer code will not work as an Indexpath is not defined. Have it in didSelectRowAtIndexPath still will not allow me to get the Array working.

    Options in my brain right now... 1. Change the - (void)loadMoviePlayer to include the indexPath as a ref and change the pathForResource to handle the Array. 2. Change the - (void)loadMoviePlayer to include the indexPath as a ref and use an if / else statement will blocks of the MoviePlayer code to repeat the process using (indexPath.row % 2 == 0)

    Recap... UITableView with CustomCell using Array to populate cell information and didSelectRowAtIndexPath need MoviePlayer which is attached to a UIButton within the CustomCell to be played depending on the IndexPath / Cell.

    Your help would be gratefully appreciated. Any question please feel free to ask. Thank you in advance.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I'd suggest scrapping the UIButton and just have the didSelectRowAtIndexPath trigger the movie file to start playing, just like how the iPod and YouTube apps work.
     
  3. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #3
    Options

    Thank you for your input - You got my brain thinking. Using what you said and the fact that a Audio and a Video file both need to be played depending on the users requirements. So, what about any of the following options and the process of avenue to achieve them?

    1: UIAlertView with Options
    2: Double Tap Cell
    3: Two Finger Tap Cell
    4: Swipe Cell

    Thoughts?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    If you had mentioned that you needed two controls that serve different purposes in your OP, I probably wouldn't have suggested what I did. Now, I'm gonna suggest using two UIButtons in each cell but then having each tagged with the row number so you can use that in the action method to choose the correct file.
     
  5. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #5
    Almost There

    My Apologies. Good idea. Any pointers on having each tagged with the row number. Looking through the Docs...
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    UIView, of which UIButton is a subclass, has a property called, appropriately, tag.
     
  7. thread starter macrumors newbie

    CeiJay

    Joined:
    Jul 17, 2010
    #7
    Thank you for your help.

    The solution was the following...

    Code:
    NSInteger row = indexPath.row;
    	NSString *rowTitle = [filmMovie objectAtIndex:row];
    
    		NSString *path = [[NSBundle mainBundle] pathForResource:rowTitle ofType:nil];
     

Share This Page