macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Movies and Menus
Pages: 1, 2, 3

Implementing These Methods

With the Interface reconfigured to our needs, we return to Project Builder to implement these methods. If you've scanned over the NSMovieView documentation, you probably have a good idea of what's going to happen next. The first method we will implement is -playMovie:, and this is done with just one line of code:




- (IBAction)playMovie:(id)sender
{
    [movieView start:self];
}

Here we used the NSMovieView method start: to start playing the movie. The argument to the method is the message sender, which we set as self here. The method -pauseMovie: is nearly the same:


- (IBAction)pauseMovie:(id)sender
{
    [movieView stop:self];
}

You see here that all we are doing is invoking the stop method of NSMovieView; easy enough. The third method, -toggleLoopMode:, is slightly more complicated.

The idea is that we want to allow the user to toggle between not looping through the movie and looping through it. When the movie is in loop mode, there will be a check next to the Loop menu item. A check indicates that the menu item is in its on state; no check indicates that the menu item is in its off state. One can set and determine the state of a menu item using NSMenuItem's -setState and -state: methods, respectively.

A QuickTime movie has three play modes with respect to looping. The first mode is normal playback mode, the second is looping playback mode, and the third is loop back-and-forth playback mode; we will only toggle between the first two.

The idea of -toggleLoopPlayback is to check the state of the menu item and change the looping mode accordingly, in addition to changing the state to reflect the changed looping mode. Let's lay it out and take a look:


- (IBAction)toggleLoopMode:(id)sender
{
  if ( [sender state] == NSOnState ) {
	[sender setState:NSOffState];
	[movieView setLoopMode:NSQTMovieNormalPlayback];
  } else {
	[sender setState:NSOnState];
	[movieView setLoopMode:NSQTMovieLoopingPlayback];
  }
}

We checked to see whether or not the menu item was in its on state, and if so, we set it to the off state and set the playback mode to normal. If the menu item's state was off, then we changed it to on and set the playback mode to looping playback. NSQTMovieNormalPlayback and NSQTMovieLoopingPlayback are just constants used to represent these two modes.

Also in Programming with Cocoa

Understanding the NSTableView Class

Inside StYNCies, Part 2

Inside StYNCies

Build an eDoc Reader for Your iPod, Part 3

Now compile and run you application and play around with it. With a little bit of exploration, it shouldn't be long before you notice that Loop in the dock menu acts a little screwy. If you look at Project Builder's runtime error messages, you'll notice that you get something telling you that NSApplication doesn't respond to state messages.

The source of these error messages and the undesirable behavior in the dock menu has to do with the nature of the dock menu itself. The Dock is a separate application that displays dock menus. Communication happens between the dock and your application to send messages as a result of selecting custom dock menu items, such as Loop. Unfortunately, what this means for us is that when we expect the sender argument variable to be assigned to the menu item itself, which does indeed respond to state, it is really assigned to the application instance, since that is the object communicating with the Dock and the dock menu.

The moral of the story is that we can't rely on the otherwise convenient sender argument variable being the menu item when working with dock menus. A solution to this problem is to create an outlet in Controller that is connected to the menu item, and then we can communicate directly with the menu item, rather than relying on the sender argument. I'll leave this for you to work out, and you can see it in my project available for download here.

With that, we've come to the end of today's column. I hope you enjoyed it and have found something useful in it. I mentioned earlier the issue of movies being resized to the NSMovieView size, and I would like to make a request to address this issue. See you next time!


Return to the Mac DevCenter.