views:

1023

answers:

3

Hello everyone,

My application is a viewer for a custom format, a zip file with a well defined XML manifest and resources, such as images and movies. I use zlib to open up the zip file in memory and then proceed to display said resources.

One problem I've ran into is that I'm unable to properly display videos, apparently because QTMovie cannot determine the mime-type. Movie loaded from file ([QTMovie movieWithFile]) works perfectly. Loaded from memory ([QTMovie movieWithData]) refuses to work.

This makes sense, because lacking the file extension, QTMovie cannot determine the mime-type information. After a bit of a search, I've resorted to using QTDataReference in the following mannner:

NSData *movieData = ...read from memory...;
QTDataReference *movieDataReference = [[QTDataReference alloc] initWithReferenceToData:movieData name:fileName MIMEType:@"video/x-m4v"];
QTMovie *mov = [QTMovie movieWithDataReference:movieDataReference error:&err];

This works nicely, however hardcoding MIMEType is far from ideal. I have access to the filename and the extension, so I've attempted to find the mime-type using UTI (thanks to the nice folks on #macdev):

- (NSString*)mimeTypeForExtension:(NSString*)ext {

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)ext,NULL);
    return NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)UTI,kUTTagClassMIMEType));
}

This however doesn't work. Clearly, there's an internal OS X database of extensions and corresponding mime-types, somewhere. Otherwise from-disk movies wouldn't work. How do I get access to it?

Thanks!

+5  A: 

The problem your having is that m4v and m4p dont have a mime types registered with Launch Services (probably because the mime type for m4v and m4p is not standard). In any event, what you should probably do is handle edge cases like this and then check for nil when the function returns (in case the extension is both not registered and not handled by you).

The other thing is that you're leaking memory with your current use. I'm assuming you're using garbage collection, but the first call creates a CFString that is never released. Here is a suggested implementation of your method:

-(NSString*)mimeTypeForExtension:(NSString*)ext
{
    NSAssert( ext, @"Extension cannot be nil" );
    NSString* mimeType = nil;

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
     (CFStringRef)ext, NULL);
    if( !UTI ) return nil;

    CFStringRef registeredType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
    if( !registeredType ) // check for edge case
    {
     if( [ext isEqualToString:@"m4v"] )
      mimeType = @"video/x-m4v";
     else if( [ext isEqualToString:@"m4p"] )
      mimeType = @"audio/x-m4p";
     // handle anything else here that you know is not registered
    } else {
     mimeType = NSMakeCollectable(registeredType);
    }

    CFRelease(UTI);
    return mimeType;
}
Jason Coco
Thanks Jason, you're right on!
EightyEight
+2  A: 

Suggest that people change the return to [mimeType autorelease]; - some of us still use the ancient ways!

And thanks! This was a big help.

David H
+1  A: 

You can use NSWorkspace to have the system guess UTI of a file.

-(NSString *)mimeTypeForFileAtPath:(NSString *)path error:(NSError **)err {
  NSString *uti, *mimeType = nil;

  if (!(uti = [[NSWorkspace sharedWorkspace] typeOfFile:path error:err]))
    return nil;
  if (err)
    *err = nil;

  if ((mimeType = (NSString *)UTTypeCopyPreferredTagWithClass((CFStringRef)uti, kUTTagClassMIMEType)))
    mimeType = NSMakeCollectable(mimeType);

  return mimeType;
}
rsms