How to play Video in iPad

This is the very simple application . In this application we will see how to play video in the iPad.

Step 1: Create a View base application using template. Give the application name “VideoPlay_iPad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the VideoPlay_iPadViewController class for you. Expand Resources and notice the template generated a separate nib,VideoPlay_iPadViewController.xib, for the “VideoPlay_iPad”.

Step 4: We need to add MediaPlayer.framework in the Frameworks folder. Select-> Frameworks folder -> Add ->Existing Frameworks -> then select MediaPlayer.framework.

Step 5: In the VideoPlay_iPadViewController.h file, we have created instance of MPMoviePlayerController class, that manage the playback of a movie from a file or from the network, and create a instance of NSURL class . So make the following changes in the file.

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface VideoPlay_iPadViewController : UIViewController {

        MPMoviePlayerController *videoPlayer;
        NSURL *videoURL;
       
}

Step 6: Open the VideoPlay_iPadViewController.m file and make the following changes in the file.

(void)viewWillAppear:(BOOL)animated
{
        NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3idiots.mov" ofType:nil];
        NSURL *url = [NSURL fileURLWithPath:urlStr];
        videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [self.view addSubview:videoPlayer.view];
        videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);  
        [videoPlayer play];
}

Step 7: Now compile and run the application in the Simulator.

You can Download SourceCode from here VideoPlay_iPad

Image change using touch function in iPhone

In this application we will see how to images change using touch function.

Step 1: Create a Window base application using template. Give the application name “MovingBall12”.

Step 2 : Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add UIView class i the project. Select Classes -> Add -> New File -> Cocoa Touch Class -> Objective C class -> select UIView from the Subclass of. Give the file name “BallView”.

Step 4: We have added two resources in the Resources folder. Give the name of the resource “1.png”,”2.png”.

Step 5: In the MovingBall12AppDeleagte.h file, we have added BallView class, NSTimer , so make the following changes in the file.

#import <UIKit/UIKit.h>

@class BallView;

@interface MovingBall12AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
        BallView *ballView;
        int count;
        NSTimer *ViewTimer;
       
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet BallView *ballView;
@property (nonatomic, retain) IBOutlet NSTimer *ViewTimer;

Step 6: Open the BallView.h file, we have added UIImageView, UILabel, NSTimer, so make the following chnages:

#import <UIKit/UIKit.h>

@interface BallView : UIView {
       
        NSTimer *ViewTimer;
        UIImageView *theBall;
        UILabel *touchMe;
        CGRect touchMeFrame;
        int count,switcher;
        int directionX, directionY;
        BOOL gameRunning;
        NSArray *images;
}

@property(nonatomic,retain) NSTimer *ViewTimer;
@property(nonatomic,retain) IBOutlet UIImageView *theBall;
@property(nonatomic,retain) IBOutlet UILabel *touchMe;
@property(nonatomic,retain) NSArray *images;

@property CGRect touchMeFrame;
@property int directionX, directionY;
@property BOOL gameRunning;

Step 7: Double click the MainWindow,xib file and open it to the Interface Builder. Double click the window icon from the main window and open it , first drag the View from the library and place it to the window. Next drag label and ImageView from the library and place it to the view window. Select the View from the main window and bring up Identity Inspector and change the class name into BallView. Connect BallView to the label and select touchMe,and connect BallView to the ImageView and select the theBall (See the figure below). Now save MainWindow.xib file, close it and go back to the Xcode.

Step 8: Open the BallView.m fie make the following changes in the file.

(void)onTimer {
        CGPoint center = theBall.center;
       
        float newX = center.x + directionX;
        float newY = center.y + directionY;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        int screenX = self.frame.origin.x + self.frame.size.width;
        int screenY = self.frame.origin.y + self.frame.size.height;
       
        BOOL changed = NO;
        if (newX < 0) {
                directionX = rndX;
                newX = 0;
                changed = YES;
        } else if (newX > screenX) {
                directionX = rndX;
                newX = screenX;
                changed = YES;
        }
       
        if (newY < 0) {
                directionY = rndY;
                newY = 0;
                changed = YES;
        } else if (newY > screenY) {
                directionY = rndY;
                newY = screenY;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionY *= 1;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionX *= 1;
                changed = YES;
        }
       
        if (changed == YES) {
                int img = switcher++ % (int)[images count];
                theBall.image = [images objectAtIndex:img];
               
        }
       
        CGPoint newCenter = CGPointMake(newX, newY);
        [theBall setCenter:newCenter];
       
}

(void)toggleTimer:(int)withCount {
        if (withCount % 2 != 0) {
                [ViewTimer invalidate];
                touchMe.backgroundColor = [UIColor whiteColor];
                touchMe.textColor = [UIColor blackColor];
               
                               
                gameRunning = NO;
               
        } else {
                ViewTimer = [NSTimer scheduledTimerWithTimeInterval:0.0075 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];  
                touchMe.backgroundColor = [UIColor blackColor];
                touchMe.textColor = [UIColor whiteColor];
               
                gameRunning = YES;
        }
}

(void)doInitialSetup {
        sranddev();
       
        count = 0;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        directionX = rndX;
        directionY = rndY;
       
        UIImage *img1 = [UIImage imageNamed:@"1.png"];
        UIImage *img2 = [UIImage imageNamed:@"2.png"];
       
        self.images = [NSArray arrayWithObjects:img1, img2, nil];
       
        int img = (int)count % (int)[images count];
        theBall.image = [images objectAtIndex:img];
       
        gameRunning = NO;
}

(BOOL) isPointInTouchMe:(CGPoint)point {
        return (point.x >= touchMe.frame.origin.x
                        && point.x <= touchMe.frame.origin.x + touchMe.frame.size.width
                        && point.y >= touchMe.frame.origin.y
                        && point.y <= touchMe.frame.origin.y + touchMe.frame.size.height);
}

(BOOL) isPointInTheBall:(CGPoint)point {
        return (point.x >= theBall.frame.origin.x
                        && point.x <= theBall.frame.origin.x + theBall.frame.size.width
                        && point.y >= theBall.frame.origin.y
                        && point.y <= theBall.frame.origin.y + theBall.frame.size.height);
}

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
       
        if ([self isPointInTouchMe:point]) {
                [self toggleTimer:count++];
        }
       
}
(void) awakeFromNib {
        [self doInitialSetup];
}

(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
                [self doInitialSetup];
    }
    return self;
}

(void)drawRect:(CGRect)rect {
        [super drawRect:rect];
}

Step 9: Now compile and run the application on the Simulator.

You can Download SourceCode from here MovingBall12

Shape5.com

This is advertising article.

Shape5.com produces many great Joomla Templates and Joomla Extensions.Their
S5 Horizontal Acorrdion module contains up to 10 module positions that
appear in
a horizontal jQuery slide function so you can publish any of your favorite
modules or custom html modules to one of the slides and keep your site
clean and
consolidated while giving it some eye candy.

read more

1 Week Apps

Things have been pretty heavy over the last few weeks. Work has been busy and the very last edits for “Learning iOS Game Programming” have come in, plus working on Particle Designer 1.3. To try and spice things up a bit, Tom came up with the idea of trying to create apps in just one […]

1 Week App Competition

Having finally formalised the approach to the 1 week app competition (sorry for the delay), this post is officially kicking things off. I did make an update to the previous post, but as pointed out, not having an official kickoff with the rules was confusing. I’m also going to extend the time period as well. […]