TweetFollow Us on Twitter

Developing Applications With The QuickTime For Cocoa Kit

Volume Number: 21 (2005)
Issue Number: 6
Column Tag: Programming

QuickTime Toolkit

Back To The Future, Part II

by Tim Monroe

Developing Applications With The QuickTime For Cocoa Kit

In the previous QuickTime Toolkit article ("Back to the Future" in MacTech, May 2005), we began looking at Apple's new Cocoa framework for displaying and modifying QuickTime movies, called the QuickTime For Cocoa Kit or the QTKit. We saw how to open and display a movie in a window of a Cocoa document-based application; we also saw how to manipulate movies using command-line tools, which would be quite useful for batch processing via shell scripts or other scripting mechanisms.

Introduction

In this article, we'll continue our investigation of the QTKit framework, which is available in QuickTime 7 on both Panther and Tiger. We'll see how to extend the sample application KitEez that we began developing in the previous article to support the complete set of standard document behaviors (Save, Save As, Revert, and so forth). Toward the end of this article, we'll see how to restrict the file types displayed in the file-opening dialog box. Then in the next article, we'll wrap up our tour of the QTKit by looking at some advanced uses of its classes and methods.

Document Behaviors

So far, our sample application KitEez is able to open any kind of file that QuickTime can handle -- standard movies files, Flash files, MP3 files, MPEG-4 files, AIFF files, text files, and so forth -- and display the movie data in a Cocoa document window. Figure 1 shows a typical KitEez document window. As you can see, the movie and the movie controller bar completely fill the content region of the window. As you can also see from the hourglass shape of the thumb in the movie controller bar, the movie is editable using the items in the Edit menu or their standard keyboard shortcuts.


Figure 1. A window that contains a QTMovieView

Because movies opened using KitEez are editable, we'll want to support all the standard document behaviors: Undo/Redo, Save, Save As, Revert to the last saved version, and so forth. If you have developed Cocoa document-based applications before, then you'll know that Cocoa provides a sophisticated document architecture that tracks changes to a document and responds appropriately to user actions. For instance, if the user tries to close an edited but unsaved document window, Cocoa will display a sheet that prompts the user to save or discard those changes. The Cocoa document architecture, however, has no knowledge of what it means to save a QuickTime movie, so we'll need to override a few NSDocument methods to add that and other standard document behaviors to our sample application.

Supporting Editing Operations

It's actually trivially easy to support the standard undo and redo mechanism for edits made to a QuickTime movie that is displayed in a QTMovieView in a Cocoa document window. All we need to do is make the associated QTMovie object editable, and we saw how to do this in the previous article with this single line of code:

[movie setAttribute:[NSNumber numberWithBool:YES] 
                  forKey:QTMovieEditableAttribute];

Once we've executed this line of code, the movie can be edited using QTMovieView methods like cut:, copy:, paste:, trim:, and the like. Indeed, the default behavior of the Edit menu items is to issue these IB actions, so we need to write exactly 1 line of code (shown above) to have a full-fledged movie editing application.

QTKit provides the standard multiple-level undo and redo processing. A user can cut, copy, and paste to his or her heart's content and then undo all those editing operations. Also, QTKit automatically provides the standard drag-and-drop editing operations. A user can drag a movie segment out of a movie window and into any other object that can accept dragged movie data. Figure 2 shows the drag image being dragged out of a movie window.


Figure 2. A dragged movie segment

The performance of the dragging in QTKit-based applications is vastly improved over that provided by the standard movie controller. In some benchmarks, the creation of the drag image alone was over 3000 times faster in QTKit applications than in applications using the movie controller's drag-and-drop capability.

Saving an Edited Movie

When the user selects the Save item in the File menu, the first responder's saveDocument: method is executed. The NSDocument implementation of this method checks to see whether a file is already associated with the document; if not, it displays the file-saving sheet to elicit a filename and location from the user. This will never actually happen in KitEez, since we only ever open movies from existing files. But it might happen that an edited movie cannot be saved into the file it was opened from. For instance, KitEez is able to open MP3 files as QuickTime movies (transparently invoking its MP3 import component), which are then fully editable. But QuickTime does not provide an MP3 export component, so KitEez is unable to save the edited data back into the original MP3 file. In that case, we'd want to display the file-saving sheet so that the user can save the data as a QuickTime movie file (.mov).

The QTMovie class provides a method that we can use to determine whether a movie object can be saved into its associated file, canUpdateMovieFile. Listing 1 shows our override of the saveDocument: method.

Listing 1: Saving a document

- (IBAction)saveDocument:(id)sender
{
   if ([[movieView movie] canUpdateMovieFile])
      [super saveDocument:sender];
   else
      [super saveDocumentAs:sender];
}

This is simple enough: if the movie can be saved into the file it was opened from, then we just call NSDocument's saveDocument: method; otherwise we call its saveDocumentAs: method.

NSDocument's saveDocument: and saveDocumentAs: methods internally call writeWithBackupToFile:ofType:saveOperation: to actually write the document data into the associated file. We can override that method to save the edited movie data into the original movie file or into the newly-selected destination file. Listing 2 shows our override method.

Listing 2: Writing out the movie data

- (BOOL)writeWithBackupToFile:(NSString *)fileName 
      ofType:(NSString *)documentTypeName 
      saveOperation:(NSSaveOperationType)saveOperationType
{
   BOOL success = NO;

   if (saveOperationType == NSSaveOperation)
      success = [[movieView movie] updateMovieFile];
   else if (saveOperationType == NSSaveAsOperation) {
      success = [[movieView movie]writeToFile:fileName 
                                                withAttributes:nil];
      if (success) {
         QTMovie *newMovie = [QTMovie movieWithFile:fileName error:nil];

         [movieView setMovie:newMovie];
         [self initializeMovieWindow];
      }
   }
	
   return success;
}

When the requested operation is NSSaveOperation, we simply call the updateMovieFile method to update the movie atom in the movie file. When the requested operation is NSSaveAsOperation, we need to do a little more work. As you can see, we first call the QTMovie method writeToFile:withAttributes: with a nil dictionary of attributes. This has the effect of writing only the movie atom into the destination file. What we end up with in this case is a reference movie, that is, a movie whose media data is located in some other file. If instead we wanted a self-contained movie file, we could call writeToFile:withAttributes: with a dictionary that indicates that we want the movie to be flattened into the destination file, like this:

success = [[movieView movie] writeToFile:fileName
                                          withAttributes:[NSDictionary 
            dictionaryWithObject:[NSNumber numberWithBool:YES] 
            forKey:QTMovieFlatten]];

I'll leave it as an easy exercise for the reader to add an accessory view to the save panel that allows the user to determine whether a movie is saved as a reference movie or as a self-contained movie, like the one displayed by the QuickTime Player application (Figure 3).


Figure 3. An accessory view in a save panel

As you know, the standard behavior when performing a Save As operation is to replace the movie in the movie view with the movie in the new destination file. So, once we've called writeToFile:withAttributes:, we then need to open the movie in the new file and set it as the movie displayed in the movie view. Then we need to perform any movie configuration that must happen when a movie is opened. As you can see, we call the method initializeMovieWindow, defined in Listing 3. This code is lifted wholesale from the windowControllerDidLoadNib: method we considered in the previous article on QTKit.

Listing 3: Initializing a movie and its window

- (void)initializeMovieWindow
{
   QTMovie *movie = [movieView movie];

   // make the movie editable
	
[movie setAttribute:[NSNumber numberWithBool:YES] 
                forKey:QTMovieEditableAttribute];

   // use the QTKit's resize indicator, not NSWindow's

[[movieView window] setShowsResizeIndicator:NO];
   [movieView setShowsResizeIndicator:YES];

   // add a listener for size-changed notifications

   [[NSNotificationCenter defaultCenter] addObserver:self 
         selector:@selector(boundsDidChange:) 
         name:QTMovieSizeDidChangeNotification object:movie];
}

It's perhaps worth mentioning that the writeWithBackupToFile:ofType:saveOperation: method is deprecated in Mac OS X version 10.4 and later. Since however we want our application to run under all environments supporting QTKit, including Mac OS X version 10.3.9, we'll stick with this method.

Reverting to the Last Saved Version of a Movie

There remains just one document behavior for us to implement, namely reverting to the last saved version of a movie file. When the user selects the Revert item in the File menu, the Cocoa document handling code displays the sheet shown in Figure 4.


Figure 4. The Revert sheet

If the user presses the Revert button, the revertDocumentToSaved: method in the first responder is invoked. We won't override that method, so that method in the NSDocument class will be called. Rather, we'll override the revertToSavedFromFile:ofType: method, as shown in Listing 4.

Listing 4: Reverting to a saved movie file

- (BOOL)revertToSavedFromFile:(NSString *)fileName 
            ofType:(NSString *)type
{
   QTMovie *newMovie = [QTMovie movieWithFile:fileName error:nil];

   [movieView setMovie:newMovie];
   [self initializeMovieWindow];

   return YES;
}

As in the Save As case shown in Listing 3, we open the specified file and assign it to the movie view; then we perform the required initialization of the movie and movie window. Also, we return the value YES to make sure that the change count of the document is cleared.

So, we have successfully implemented a reasonably complete set of document-related behaviors in our sample application KitEez, using just a very few QTMovie methods: canUpdateMovieFile, updateMovieFile, and writeToFile:withAttributes:. At no point did we need to step outside the methods provided by QTKit into the underlying Carbon APIs. This stands in stark contrast to the work we needed to do in our earlier Cocoa movie-playing application MooVeez, which relied on the now-deprecated Cocoa classes NSMovie and NSMovieView for its QuickTime support. I invite you to take a second look at the article describing those classes ("The Cocoanuts" in MacTech, December 2002); by comparing the Carbon-laced methods we had to write there with the purely Cocoa-based methods we've seen here, you will get a very good appreciation of how big an advancement the QTKit is over those earlier classes.

Openable File Types

In the previous article introducing QTKit, we saw that we can very easily configure the file-opening dialog box to list all files openable by QuickTime by overriding the panel:shouldShowFilename: method and calling the canInitWithFile: class method. That method returns YES just in case a specified file can be used to initialize a QTMovie object. For certain purposes, however, we might want to restrict the types of files that the user can open to some more limited set. For instance, as you probably know, QuickTime can open text files with its text movie importer -- which converts the text into a series of frames in a text track. (See "Word is Out" in MacTech, November 2000 for more information on QuickTime's text-handling capabilities.) But it's doubtful that most applications that can open and display QuickTime movies would want to allow text files to be opened by the user. Ditto for HTML files. Ditto for PDF files.

QTKit provides an easy way to allow the user to open only the kinds of files that would normally be considered as media files and hence openable by a QuickTime application. Instead of using the canInitWithFile: method, we can use the movieFileTypes: method. This too is a class method, so you don't need to have an existing QTMovie object in order to call it.

The movieFileTypes: method returns an NSArray of file types and file extensions that can be opened by QuickTime. This method takes one parameter, which indicates the kinds of file types that you want to be included in the array. Currently these flags are defined for specifying file types:

typedef enum {
   QTIncludeStillImageTypes         = 1 << 0,
   QTIncludeTranslatableTypes       = 1 << 1,
   QTIncludeAggressiveTypes         = 1 << 2,
   QTIncludeCommonTypes             = 0,
   QTIncludeAllTypes                = 0xffff
} QTMovieFileTypeOptions;

Passing the value QTIncludeCommonTypes indicates that you want the array to contain only those file types and extensions that are commonly thought of as openable by QuickTime; this includes the extensions .mov and .mqv, as well as any file types that can be imported in place by QuickTime. (Recall that to be able to import a file in place is to be able to import the file without having to create a new file to hold the imported data.)

The QTIncludeStillImageTypes flag indicates that you want the array to include, in addition to the common file types, all types of files that can be opened by QuickTime using one of its available graphics importers. The QTIncludeTranslatableTypes flag indicates that you want the array to include also those file types that can be opened by QuickTime using one of its available movie importers, whether or not the file data can be imported in place; this array excludes however any file types that would require an aggressive importer. (An aggressive importer is one that can handle file types like text or HTML that would not normally be considered openable by QuickTime.) Finally, the QTIncludeAggressiveTypes flag indicates that you want the array to include file types that require an aggressive importer.

Obviously, the QTIncludeAllTypes flag sets all the bits in the options parameter and requests that the returned array include file types and file extensions for all files that QuickTime can open, using any available graphics or movie importer. Passing the array associated with the QTIncludeAllTypes flag to NSOpenPanel's runModalForTypes: method is essentially identical to using the canInitWithFile: method inside our panel:shouldShowFilename: override method.

It's important to realize that, for instance, the array returned when specifying the QTIncludeStillImageTypes flag will include all file types and extensions for still image files as well as all file types and extensions for the common movie types. That is to say, that array does not contain only the file types and extensions for still image types. If you wanted an array like that, you would need to use Component Manager functions to iterate through all installed components and select only those of type GraphicsImporterComponentType. Listing 5 shows how you might do that.

Listing 5: Finding still image types

0
fileTypes = [NSMutableArray array];
ComponentDescription findCD = {0, 0, 0, 0, 0};
ComponentDescription infoCD = {0, 0, 0, 0, 0};
Component comp = NULL;
OSErr err = noErr;

findCD.componentType = GraphicsImporterComponentType;
findCD.componentSubType = 0;
findCD.componentManufacturer = 0;
findCD.componentFlags = 0;
findCD.componentFlagsMask = cmpIsMissing | 
                                       graphicsExporterIsBaseExporter;

while (comp = FindNextComponent(comp, &findCD)) {
   err = GetComponentInfo(comp, &infoCD, nil, nil, nil);
   if (err == noErr) {
      if (infoCD.componentFlags & 
                        movieImportSubTypeIsFileExtension)
         [fileTypes addObject:[[[NSString 
            stringWithCString:(char *)&infoCD.componentSubType 
            length:sizeof(OSType)] 
            stringByTrimmingCharactersInSet:[NSCharacterSet 
               whitespaceCharacterSet]] lowercaseString]];
      else 
         [fileTypes addObject:[NSString 
            stringWithFormat:@"\'%@\'", [NSString 
            stringWithCString:(char *)&infoCD.componentSubType 
            length:sizeof(OSType)]]];
   }
}

Conclusion

In this article, we've added the standard document-handling behaviors to our KitEez sample application. We've seen how to use QTKit methods to support saving an edited movie into its original file or into a new file, and we've seen how to revert to the last saved version of a movie file. KitEez is now a reasonably complete multi-document movie opening and editing application.

In the next article, we'll take a look at some of the more advanced operations we can use the QTKit framework to perform. We'll see how to add images to an existing QuickTime movie, work with notifications and delegates, and execute QTKit methods on a secondary thread.


Tim Monroe is a member of the QuickTime engineering team at Apple. You can contact him at monroe@mactech.com. The views expressed here are not necessarily shared by his employer.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
Geekbench 6.2.0 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
Quicken 7.2.3 - Complete personal financ...
Quicken makes managing your money easier than ever. Whether paying bills, upgrading from Windows, enjoying more reliable downloads, or getting expert product help, Quicken's new and improved features... Read more
EtreCheckPro 6.8.2 - For troubleshooting...
EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more
iMazing 2.17.7 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more

Latest Forum Discussions

See All

‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »
Amanita Design Is Hosting a 20th Anniver...
Amanita Design is celebrating its 20th anniversary (wow I’m old!) with a massive discount across its catalogue on iOS, Android, and Steam for two weeks. The announcement mentions up to 85% off on the games, and it looks like the mobile games that... | Read more »
SwitchArcade Round-Up: ‘Operation Wolf R...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 21st, 2023. I got back from the Tokyo Game Show at 8 PM, got to the office here at 9:30 PM, and it is presently 11:30 PM. I’ve done what I can today, and I hope you enjoy... | Read more »
Massive “Dark Rebirth” Update Launches f...
It’s been a couple of months since we last checked in on Diablo Immortal and in that time the game has been doing what it’s been doing since its release in June of last year: Bringing out new seasons with new content and features. | Read more »
‘Samba De Amigo Party-To-Go’ Apple Arcad...
SEGA recently released Samba de Amigo: Party-To-Go () on Apple Arcade and Samba de Amigo: Party Central on Nintendo Switch worldwide as the first new entries in the series in ages. | Read more »
The “Clan of the Eagle” DLC Now Availabl...
Following the last paid DLC and free updates for the game, Playdigious just released a new DLC pack for Northgard ($5.99) on mobile. Today’s new DLC is the “Clan of the Eagle" pack that is available on both iOS and Android for $2.99. | Read more »
Let fly the birds of war as a new Clan d...
Name the most Norse bird you can think of, then give it a twist because Playdigious is introducing not the Raven clan, mostly because they already exist, but the Clan of the Eagle in Northgard’s latest DLC. If you find gathering resources a... | Read more »
Out Now: ‘Ghost Detective’, ‘Thunder Ray...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

Price Scanner via MacPrices.net

Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more
14″ M1 Pro MacBook Pros still available at Ap...
Apple continues to stock Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs for as much as $570 off original MSRP, with models available starting at $1539. Each model... Read more

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Retail Key Holder- *Apple* Blossom Mall - Ba...
Retail Key Holder- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 03YM1 Job Area: Store: Sales and Support Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.