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 setFrame: CGRectMake(105, 99, 72, 37)];
//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:[[NSBundle mainBundle]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 = [[MPMoviePlayerController alloc]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.

Comments

  1. Will you please post article on Delegates and Protocols?

    ReplyDelete
  2. Yes sure vibha it will be present by this monday 13th december 2010

    ReplyDelete
  3. Ravi, what you have done here on the blog is fantastic, you have this immense potential..
    Could you please make a section within your blog for all programs using Interface builder. This would be really great for us to know programs written through coding as well as by using IB.

    Thanks
    Niraj

    ReplyDelete
  4. Hello Sir, Can you please specify the orientations as well with respect to videos, means how do we set orientation or change orientations of the video clip in simulator as well as in the case when we configure on real device/ iPad.

    Thankyou

    ReplyDelete
  5. @Arpita: In the earlier SDK the movie used to be played in the landscape orientation mode but in the new SDK it will not be the case, i have tried this same code on the new SDK and found that it's not working so i came up with an idea and implemented it to play video which i will be updating it in the above post soon and your query will also be solved,
    For orientation what i feel is that you try to rotate the view in the should autorotateToInterfaceOrientation method of the viewController class

    ReplyDelete
  6. Hello Sir , Can you please post the blog for playing online video in iphone application?

    ReplyDelete
  7. @Swati: Yes swati but tell me the location of the video file is it from the youtube or any video file present in some server, let me know and i will post for the same

    ReplyDelete

Post a Comment