Question

I'm trying to put together a simple app that lists all videos in the iPod library (movies, TV shows, etc) and allows the user to play any video they choose. The app is using a storyboard that has a NavigationController with the UITableView in it.

It looks like the query is actually getting the MPMediaItems, but the cells aren't being updated with the title of each movie. Can you take a look at this and tell me what I'm doing wrong?

Here's the .h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MovieListViewController : UITableViewController
{
    MPMediaQuery * _movieQuery;
    NSArray * _movieArray;
}

@property (nonatomic,strong) MPMediaQuery *movieQuery;
@property (nonatomic,strong) NSArray *movieArray;

@end

And here's the .m:

#import "MovieListViewController.h"

@interface MovieListViewController ()

@end

@implementation MovieListViewController

@synthesize movieQuery = _movieQuery,
            movieArray = _movieArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType];

    MPMediaQuery *movieQuery = [[MPMediaQuery alloc] init];
    [movieQuery addFilterPredicate:predicate];

    NSLog(@"movieQuery got back %u results",[[movieQuery items]count]);

    self.movieArray = [movieQuery items];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Dispose of any resources that can be recreated.
    self.movieQuery = nil;
    self.movieArray = nil;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

/*- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"UITableView has %u rows",[self.movieArray count]);
    return [self.movieArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    MPMediaItem *item = [[self.movieQuery items]objectAtIndex:[indexPath row]];
    cell.textLabel.text = [item valueForProperty:MPMediaItemPropertyTitle];
    return cell;
}
Was it helpful?

Solution

Issue is with this line:

MPMediaItem *item = [[self.movieQuery items]objectAtIndex:[indexPath row]];

Change that to:

MPMediaItem *item = [movieArray objectAtIndex:[indexPath row]];

You didn't added anything to movieQuery object, you created a local MPMediaQuery object in viewDidLoad and used that.

Another solution:

Change the viewDidLoad like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType];

    self.movieQuery = [[MPMediaQuery alloc] init];
    [movieQuery addFilterPredicate:predicate];

    NSLog(@"movieQuery got back %u results",[[self.movieQuery items]count]);

    self.movieArray = [self.movieQuery items];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top