Playing video from the server, application bundle and you tube embed code in iOS Applications

5 minutes read

We have 3 ways to play videos in iOS applications, which is given below.

1. To play video which resides on the server.
2. To play video which reside in our system or our application bundle.
3. To play video using “you tube” embed code.

 

1. To play video, which resides on the server:

 

To play video from the server in iOS, we have to first import the following framework –

“MediaPlayer.h” in the .h file.

Then we have to first declare the object of the “MPMoviePlayerController”. and declare one IBAction “playVideo” method and write the following code for playing video within the body of .m file.

 

MPMoviePlayerController * moviePlayer;
-(IBAction)playVideo:(id)sender
{
NSString *filePath= @”http://www.ebookfrenzy.com/ios_book/movie/movie.mov”;
NSURL *url = [NSURL URLWithString: filePath];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(videoPlayDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotificationobject:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

 

The above method declare a NSURL object, which is used for storing the path of web based video file. This URL object is used in the creation of a new instance of MPMoviePlayerController class. An observer is to be configured here and one method is to be add with this observer.

 

This method is to be called, when observer finds playing video file has finished playing. Next to ensure that the standards movie player controls are available to user and that video file automatically starts playing once if it is ready. Finally the movie player object is added as the sub-view to the current view or current user interface. Finally it should be displayed to the user in full screen mode.

 

– (void) videoPlayDidFinish: (NSNotification*) notification
{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}

 

The playVideo action method have declared that when the video has finished playing, the movie player object is to invoke the method name “videoPlayDidFinish” to cancel the notification and remove the moviePlayer interface from the user screen.

 

2.  To play video, which reside in our system or in our application bundle:

When we are playing video in iOS, which reside in our system or our application bundle. Now we have to use the following code to play video in iOS.

 

The above all code, which is used in the first method to play video is used same as in this method also for playing video. Procedure is same in both the method, only path creation of video file is to be different, this is because in the first method, video file is to be reside on the server side, and now in this method video file is to be reside in our application bundle. To make the path for playing the video file, which reside in our application bundle is given below –

 

NSString *filepath = [[NSBundle mainBundle] pathForResource:@”big-buck-bunny-clip” ofType:@”m4v”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL: fileURL];

 

The above code is used instead of given below following code and remaining all the code for playing video is same as, i.e. above used in the first point.

 

NSString *filePath= @”http://www.ebookfrenzy.com/ios_book/movie/movie.mov”;
NSURL *url = [NSURL URLWithString: filePath];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];

 

3.To play video, using “you tube” embed code:

 

When we are playing video using embed code, the following steps is be consider for playing video using you tube embed code.

 

1. First we have to add the web view to our “User Interface”, and then declare its outlet in .h file and then connects its outlet to the webView of user interface.

 

2. Write the following below code to the “viewDidLoad” event, when we are running our project, then this view is to be loaded, the web view will starts playing video.

 

@property(nonatomic,strong) IBOutlet UIWebView * webView;
NSString *videoURL;
videoURL =[NSString stringWithFormat:@”<html>\
<head>\
<style type=\”text/css\”>\
body {margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\”560\”\
height=\”370\”\
src=\%@\
frameborder=\”0\”\
allowfullscreen></iframe>\
</iframe> \
</body>\
</html>”, @”//www.youtube.com/embed/yPyQxSozAwM”];
[webView setScalesPageToFit:YES];
[webView loadHTMLString:videoURL baseURL:nil];

 

Note: –

 

Embed Code : Embed code is the code, which is the available with each video file on the www.youtube.com, and each video file have the unique embed code. And when we pass this code to web view in iOS, the web view directly play this video without using any user intervention.

 

The following steps is to follow for getting the embed code of video, which is given below.

 

1. Open the you tube website “www.youtube.com”
2. Click the share option, which is given below of each video.
3. Click on the embed option, which is just appear after clcking on the share option.
4. Get the embed code from here.

 

Note :- When user is playing video from web-based video file, and user wants to play video using direct data streaming from server, we have to set the following properties of moviePlayer controller.
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
All the rest procedure is same, i.e. normally used to play video from web based video file.

 

Related Posts...

Mobile AppsTechnologies

Tags: