Media Player in iPhone or iPad

Introduction

In this article I will create a single view application. For this app first we add the Media Player Framework to implement MediaPlayer to watch a video in iPhone or iPad. We use a play button to play the video file.

To better understand, follow this procedure.

Step 1

Open Xcode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on "Next".

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on "Create".

Step 6

First we add the MediaPlayer framework that is required.

To import this framework we use the following.

Step 7

Click on the project and select "Build Phase".

Step 8

Click on the "+" icon to add a framework.
framework-in-iPhone.png

Step 9

Now click on the add button.

Step 10

Now we write the code.

AppDelegate.h

#import <UIKit/UIKit.h>

@class videosViewController;

@interface videosAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) videosViewController *viewController;

@end

AppDelegate.m

#import "videosAppDelegate.h"

#import "videosViewController.h"

@implementation videosAppDelegate

- (void)dealloc

{

[_window release];

[_viewController release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

self.viewController = [[[videosViewController alloc] initWithNibName:@"videosViewController_iPhone" bundle:nil] autorelease];

} else {

self.viewController = [[[videosViewController alloc] initWithNibName:@"videosViewController_iPad" bundle:nil] autorelease];

}

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

ViewController.h

#import <UIKit/UIKit.h>

#import <MediaPlayer/MediaPlayer.h>

@interface videosViewController : UIViewController

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;

-(IBAction) playMovie;

@end

ViewController.m

#import "videosViewController.h"

@interface videosViewController ()

@end

@implementation videosViewController

@synthesize moviePlayer;

-(void)playMovie

{

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]

pathForResource:@"movie" ofType:@"MOV"]];

moviePlayer = [[MPMoviePlayerController alloc]

initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlayBackDidFinish:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;

moviePlayer.shouldAutoplay = YES;

[self.view addSubview:moviePlayer.view];

[moviePlayer setFullscreen:YES animated:YES];

}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {

MPMoviePlayerController *player = [notification object];

[[NSNotificationCenter defaultCenter] removeObserver:self

name:MPMoviePlayerPlaybackDidFinishNotification

object:player];

if ([player

respondsToSelector:@selector(setFullscreen:animated:)])

{

[player.view removeFromSuperview];

}

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

Step 11

Click on the "Run" button to show the output.

Step 12

Output1 in iPhone:

Output1-in-iPhone.png

Output2 in iPhone:

Output2-in-iPhone.png

Output3 in iPhone:

Output3-in-iPhone.png

Output1 in iPad:

Output1-in-iPad.png

Output2 in iPad:

Output2-in-iPad.png

Output3 in iPad:

Output3-in-iPad.png


Similar Articles