Thursday, 23 October 2008

IPhone MoviePlayer Sample Code

Deconstructing the MoviePlayer app to see how the Media Player Framework ticks.

MoviePlayer has 4 classes:
  • MoviePlayerAppDelegate
  • MyImageView
  • MyOverlayView
  • MyViewController
A new IPhone application automatically creates 2 classes, with project name MPC, MPCAppDelegate and MPCViewController. Therefore, MyImageView and MyOverlayView are additions while MyViewController has been renamed.



There are 2 different ways of including a file/class:
  • #import "MyViewController.h"
  • @class MyViewController;
The interface lists 3 instance variables of type NSInteger aside from the default UIWindow and ViewController:
  • backgroundColor
  • controlMode
  • scalingMode
The @property declares the accessor methods for the 5 variables. It has attributes which modify the accessors.

Example: @property (nonatomic, retain) UIWindow *window;

nonatomic means the accessor returns the value directly, the default is to lock, get value, unlock which is meant for multithreaded environments.

assign, retain or copy must be explicitly set if garbage collection is not used. assign is the default and means the setter uses a simple assignment. retain keeps the object variable in-memory and cannot be deallocated without your permission. copy means that a copy of the object should be used for assignment and the previous one is sent a release message.

3 instance methods:
  • -(void)setUserSettingsDefaults;
  • -(void)applicationDidFinishLaunching:(UIApplication *)application;
  • -(void)dealloc;


3 NSString keys:
  • kScalingModeKey = @"scalingMode"
  • kControlModeKey = @"controlMode"
  • kBackgroundColorKey = @"backgroundColor"
@synthesize generates the accessors according to the attributes listed. If implemented manually, the code must follow what has been specified in the @property. @synthesize will only generate accessors that don't exist so if a variable has a setter already, then only a getter will be generated.

Sets up a test calling NSUserDefaults to see if there's a match. Passing will set the scalingMode, controlMode and backgroundColor variables. Failing will create the values based on the settings bundle info and then set the variables:
  • Get the path to the main bundle of the project.
  • Append Settings.bundle to the path using stringByAppendingPathComponent, which takes care of adding separators.
  • Append Root.plist to the path, same as before.
  • Create a dictionary using dictionaryWithContentsOfFile, giving the path to Root.plist.
  • Create the prefSpecifierArray array from the dictionary using the key, PreferenceSpecifiers, which is a tag in Root.plist.
    • Root.plist is an XML file with dict, array and key tags. May be an easy way to read XML.
  • Declare 3 NSNumber variables and a dictionary, prefItem.
  • Loop prefSpecifierArray with the current value going into prefItem.
    • Set keyValueStr with the dictionary using the key, 'Key'.
    • Set defaultValue with the dictionary using the key, 'DefaultValue'.
    • Check if keyValueStr is equal to any NSStrings specified above.
    • Set the numbers with defaultValue if any is correct.
  • Create a dictionary containing the default values and their keys.
  • Register the dictionary with NSUserDefaults using registerDefaults.
  • Updates and writes to the persistent domains using synchronize.
-(void)applicationDidFinishLaunching:(UIApplication *)application
Add the ViewController's view as a subview of the UIWindow. Call self with setUserSettingsDefaults to get the default movie player settings. Lastly, call the ViewController with initMoviePlayer to create a movie player object.

Release the UIWindow and the ViewController. Call the super's dealloc method.

No comments: