﻿NSArray *titles = [AVMetadataItem metadataItemsFromArray:[mediaSelectionOption commonMetadata]
                                  withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
        if ([titles count] > 0) {
                // Try to get a title that matches one of the user's preferred languages.
                NSArray *preferredLanguages = [NSLocale preferredLanguages];

                for (NSString *thisLanguage in preferredLanguages) {
                        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:thisLanguage];
                        NSArray *titlesForLocale = [AVMetadataItem metadataItemsFromArray:titles withLocale:locale];
                        [locale release];
                        if ([titlesForLocale count] > 0) {
                                title = [[titlesForLocale objectAtIndex:0] stringValue];
                                break;
                        }

                }

                // No matches in any of the preferred languages. Just use the primary title metadata we find.
                if (title == nil) {
                        title = [[titles objectAtIndex:0] stringValue];
                }
        }