PrEV
Thoughts from a NeXTStep Guy on Cocoa Development

Core Animation Movie Layers with NSControls overlayed

Nov 29, 2007 by Bill Dudney

Putting a movie into a view has been possible for while now but we could not put controls (like buttons etc) over that movie, until Core Animation. In fact it is possible now to have a quartz composer composition running in the background, movies playing on top of that and animating in 3d space and controls all on top of that. Crazy stuff that...

With the arrival of Core Animation we have a ton of flexibility in mixing and matching content. And probably the best part is the ease in which we can access that flexibility. The code to make it happen is so simple that its kinda hard to believe. So anyway lets dive right in. Here is the UI in action with the movie in the background and the NSButton siting on top.

Model

All this combination of parts is possible without Core Animation (CA) but man is it a pain in the neck. Now with CA its a snap. Here is the code to build the movie layer and make it part of the view. The first three lines are about getting the movie setup. Then we create a movie layer. There is a special layer subclass for QT movies (called QTMovieLayer) and all we have to do to make the layer able to play the movie is to initialize it with a valid QTMovie object. Next we set the background color and then set the layer up in its host view. And we are done...

- (void)awakeFromNib {

    NSString *urlString = 

    @"http://bill.dudney.net/roller/objc/resource/AnimationPodcast.m4v";

    NSURL *movieURL = [NSURL URLWithString:urlString];

    NSError *error = nil;

    movie = [QTMovie movieWithURL:movieURL error:&error];

    QTMovieLayer *layer = [QTMovieLayer layerWithMovie:movie];

    layer.backgroundColor = [self black];

    [self setLayer:layer];

    [self setWantsLayer:YES];

}

The button is put into the view in IB, no special tricks or anything. The view is not even marked as needing a layer in IB, the only thing that is done to the view is to specify its class.

Here is the code. Happy Mixing...



Comments:

The wwdc 2007 demo with the videowall playing 100ds of videos in parallel, do you think they needed a server cluster to make it work?

Or are these small loops all cached in memory.

Interesting what your thoights are.

Thanks for the tutorials.

Posted by Sebastian on November 04, 2009 at 07:43 PM MST #

Post a Comment:
  • HTML Syntax: Allowed