Thursday, March 10, 2011

Movie file playback stored in Project Resources





Introduction


This blog entry adds methods to the UIViewController class for presenting and dismissing a movie player using a specific set of animations. The transitions used by these methods are the same ones used by the YouTube and iPod applications to display video content.


Steps :-


1. Add "MediaPlayer.framework " as existing framework

2. Make another viewController class, suppose we name it "MoviePlayerViewController"

3. In "MoviePlayerViewController.h", add the below code,

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MoviePlayerViewController : UIViewController 
{ 
MPMoviePlayerController *mp;
NSURL *movieURL;
}
- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration;
@end
4. In "MoviePlayerViewController.m", add the below code,
#import "MoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
@implementation MoviePlayerViewController
-(void)viewWillDisappear:(BOOL) animated
{  
if(mp) {
[mp stop];
}
}
- (id)initWithPath:(NSString *)moviePath
{ 
mp.controlStyle =  MPMovieControlStyleNone;
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL fileURLWithPath:moviePath];    
[movieURL retain];
}
return self;
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 
{   
[mp setControlStyle:MPMovieControlStyleFullscreen];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
NSString *deviceModel = [NSString stringWithFormat: @"Device Type: %@\n", [[UIDevice currentDevice] model]];    
NSRange range = [deviceModel rangeOfString:@"iPad"];
if(range.location != NSNotFound){
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter     defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
//view bounds are defined as par iPad screen, so adjust accordingly
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
}
[[self view] addSubview:[mp view]];   
[mp play];
}
}
else
{  
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter     defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setCenter:CGPointMake(160, 240)];
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[self view] addSubview:[mp view]];   
[mp play];
}
}
#endif
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged: 
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification 
{   
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[mp play];
}
/*---------------------------------------------------------------------------
* 
*--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissModalViewControllerAnimated:NO];    
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) readyPlayer
{   
[[UIApplication sharedApplication] setStatusBarHidden:YES];
mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mp.scalingMode= MPMovieScalingModeAspectFit;
if ([mp respondsToSelector:@selector(loadState)]) 
{
// Set movie player layout
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 
//[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setControlStyle:MPMovieControlStyleNone];
[mp setFullscreen:YES];
#endif
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
#endif
}  
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
}
// Register to receive a notification when the movie has finished playing. 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
/*---------------------------------------------------------------------------
* 
*--------------------------------------------------------------------------*/
- (void) loadView
{ 
isFirstLoad = YES;
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
[[self view] setBackgroundColor:[UIColor blackColor]];
self.view.clipsToBounds = YES;
self.view.autoresizesSubviews = YES;
if(mp) {
[mp stop];
}
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}        
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)dealloc 
{
[mp release];
[movieURL release];
[super dealloc];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark InterfaceOrientationMethods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
//[self changeTheViewToPortrait:NO andDuration:duration];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[self changeTheViewToPortrait:NO andDuration:0.0];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
}
else if (isFirstLoad){
isFirstLoad = FALSE;
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
NSLog(@"UIInterfaceOrientationLandscapeRight");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
}
}
[UIView commitAnimations];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
@end

5. Now wherever , you want to play the movie file, add the below code,
NSString *movieFile=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
    
 // Create custom movie player   
 moviePlayer = [[[MoviePlayerViewController alloc] initWithPath:movieFile] autorelease];
    
 [self presentModalViewController:moviePlayer animated:NO];
 [moviePlayer readyPlayer]; 
In this class, import "MoviePlayerViewController.h" & "moviePlayer" is a global object of MoviePlayerViewController

Wednesday, March 9, 2011

iAds Integration


Introduction


iAd allows your application to earn revenue by displaying advertisements to the user. Your application dedicates a portion of its user interface to display banner advertisements and in turn you receive revenue when users view or click those advertisements.
While you are developing your application, iAd sends test advertisements to help you verify your implementation is correct. To receive advertisements from iAd in a release application, you need to select the advertising network option for your application before publishing it.



KeyPoint

iAds are supported on ios 4.0 and above

Steps for iAds integration



1. First of all add iAd.framework as existing framework

2. In your .h file

add the below code,


#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface testViewController : UIViewController < ADBannerViewDelegate>{
id adView;
BOOL adBannerViewIsVisible;
}
@property(nonatomic, retain) IBOutlet id adView;
- (void)fixupAdView;
- (int)getBannerHeight;
- (void)createAdBannerView;
@end
3) In your .m file

add the below code,


@synthesize adView;

4) In your viewDidLoad method, add the below code
[self createAdBannerView];

5) In your .m flee also add the below code,



#pragma mark -
#pragma mark iAdMethods
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (adBannerViewIsVisible) {        
adBannerViewIsVisible = NO;
[self fixupAdView];
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!adBannerViewIsVisible) {                
adBannerViewIsVisible = YES;
[self fixupAdView];
}
}
- (void)createAdBannerView {
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil) {
self.adView = [[[classAdBannerView alloc] 
initWithFrame:CGRectZero] autorelease];
[adView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: 
ADBannerContentSizeIdentifier320x50, nil]];
[adView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[adView setFrame:CGRectMake(0.0, 436.0, 320.0, 50.0)];
[adView setDelegate:self];
[self.view addSubview:adView];        
}
}
- (int)getBannerHeight {
return 50;
}
- (void)fixupAdView {
if (adView != nil) {        
[adView setCurrentContentSizeIdentifier: ADBannerContentSizeIdentifier320x50];
if (adBannerViewIsVisible) {
CGRect adBannerViewFrame = [adView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 386;
[adView setFrame:adBannerViewFrame];
} 
else {
[UIView beginAnimations:@"fixupViews" context:nil];
CGRect adBannerViewFrame = [adView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 436;
[adView setFrame:adBannerViewFrame];
[UIView commitAnimations];
}
}   
}