Wednesday, April 18, 2012

MediaPlayer Framework

In this post we will have a look at the MediaPlayer framework but before beginning I would like you to have a look at the AVFoundatioin framework post which will explain you how to add a framework into your current project, read that post first if you are a beginner else continue reading.

Design Phase: In this post we will try to run a video on the button touch and that video will be displayed in the iphone simulator, here's a look at the final output




Step 1: Open Xcode create a window based application project add a UIViewController subclass file and name it Myview, now you must be having two files into your class folder that’s Myview.h and Myview.m so now its time to add a video into the project
Refer AVFoundation post for this as well


Step 2: Now once you have added the MediaPlayer framework and a video file into your project its time to code now, declare an object of UIButton in the .h file of Myview also declare a function called PlayMovie which will play the movie for you. here's how the code looks

@interface Myview : UIViewController {

UIButton *btn;
}
-(void) PlayMovie;

@end



Step 3: Select the .m file of Myview and allocate memory for the UIButton object into the init method,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
//setting the button type
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//setting the button frame
[btn setFrameCGRectMake(105997237)];
//setting the title for the button
[btn setTitle:@"Play" forState:UIControlStateNormal];
//adding function to the button
[btn addTarget:self action:@selector(PlayMovie)forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

now its time to give body to the PlayMovie function here’s the code for that

-(void) PlayMovie
{
//specifying the location of the video file
NSURL *movieurl = [NSURL fileURLWithPath:[[NSBundlemainBundle]pathForResource:@"TAJ" ofType:@"mp4"]];
//creating the object of MPMoviePlayerController and suppling the URL of the file to the object of the movie player controller

MPMoviePlayerController *movieplayerController = [[MPMoviePlayerControlleralloc]initWithContentURL:movieurl];
//play the movie

[movieplayerController play];

}



Code Explanation: In the above code we have created the object of the MediaPlayerController and this class has a init method which takes a parameter of NSURL so in the NSURL object called movieurl I have supplied the location of the video file which is present into the bundle (groups & files) with the help of NSBundle class.

Step 4: Go to the loadView method present into the .m file of the Myview and add the Button view to the current view 


- (void)loadView {
[super loadView];
[self.view addSubview:btn];
}

after that go to the AppDelegate.m file of your project and add the current view to your iPhone window

#import "VideProjectAppDelegate.h"
#import "Myview.h"

@implementation VideProjectAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch

Myview * obj = [[Myview alloc]init];
[window addSubview:obj.view];
        [window makeKeyAndVisible];
}




Step 5: Press Build and Go and get the following output






I hope this post was helpful for you Happy iCoding and have a great Day.

No comments:

Post a Comment