How to play youtube videos inside iPhone app from the video url? - iphone

I am working on a project where I have to play some videos in my iPhone app. Is there any way so that I can play the videos directly using youtube video URL? I have to use lots of videos and I don't want to save them in my local iphone database. I am very new to this field.
In my project there will be a list of cancer disease. I mean blood cancer, bladder cancer, brain cancer etc. If a user clicks a particular cancer disease, then there should be a video regarding that cancer in the next view. I want to play that video directly (or something like that) using url. It should be like youtube video (streaming video). Is it possible?

This has been discussed many times on SO. Alternatively, theres an Open Source Project allowing this, that I recommend:
https://github.com/larcus94/LBYouTubeView

Add following frameworks: a) AVFoundation b) MediaPlayer
and write these import statements :
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
and then write code whenever you want
NSURL *fileURL = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];

Related

Adding videos to application

I want to store large number of videos in my application's directory. Will it make my application slow? Or will it take time to launch the application. I am loading videos only when necessary.
It will make large size of your application which is not good...& it will also take time to launch in place of save all ur video u can make user to download that video from server and after that it will be saved in ur doc directory so ur app will be not of large size.............
Where is it located locally
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"videoname" ofType:#"mov" inDirectory:#""]];
and play with that
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
EDIT:-
The exact answer to this question is NO, it will not make your App Slow,But yeah it ill take some time to load the videos in your App,Time will depend upon number of videos you are loading in your APP. The solution to this is you have to use blocks for ALAssetLibaries.
Have a look on this ALAssetLibary Apple's class reference.
It may help you if not do contact me. Thanks :)

Open movie stream in another view

I have in my app a tab view with many tabs, and in one of them I have a button that when is clicked, I want to show a movie stream in a view. I have this code:
NSString *moviePath = #"http://10.0.0.4/prog_index.m3u8";
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:moviePath]];
[theMovie.view setFrame:CGRectMake(0, 0, (self.view.frame.size.width), (self.view.frame.size.height))];
theMovie.view.backgroundColor = [UIColor grayColor];
theMovie.view.tag = 9999;
[self.view addSubview:[theMovie view]];
[theMovie play];
The view appears, but the video doesn't start. What is wrong?
You're passing to the MPMoviePlayerController an URL pointing to a m3u8 file. That is a playlist. To play media with MPMoviePlayerController, you have to set it an actual video file. You should parse the playlist to get the link to the real video and then init your MPMoviePlayerController with the real link.
Check M3U - Wikipedia and MPMoviePlayerController reference
Also check this related question
EDIT: Thanks to Hugo Silva, I realized that MPMoviePlayerController is able to play live streams in m3u8 format, since I've not seen anything wrong in your code, I suggest you to check if it's a problem of your stream. Try using one of the samples provided by Apple. Also make sure that your stream meets Apple's requirements for HTTP Streaming

Playing Multiple Videos on iPAD

I am facing some problem in playing multiple videos on iPAD. I am trying to play multiple thumbnail videos on the same view. You can say its much like the CCTV camera.Well, i have no clue. Please help me. Thanks in advance...
MPMoviePlayerController will allow multiple instances, but only one of them can be playing their movie at any given time.
It mentions it here: http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
From the article:
Note: Although you may create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time may play its movie.
You can't use the MKMediaFramework to play multiple videos. You can however do this with the lower level AVFoundation Framework. It's not as hard as you might think and I've made a tutorial that goes over it here: http://www.sdkboy.com/?p=66
Essentially what you need to do is extend UIView so it contains an AVPlayerLayer to which the output of an AVPlayer object is directed, then you can create multiple instances of this UIView that you feed video using AVPlayer instances.
This is actually pretty simple to do on the iPad.
You basically need multiple MPMoviePlayerController objects.
Each MPMoviePlayerController object has a view property, you just need to set the frames of the views on the different MPMoviePlayerController objects to match what you want it to look like.
Here is a simple example using two MPMoviePlayerController objects ans 2 different frames:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[[player view] setFrame: yourFrame1];
[myView addSubview: [player view]];
// ...
[player play];
MPMoviePlayerController *player2 =
[[MPMoviePlayerController alloc] initWithContentURL: myURL2];
[[player2 view] setFrame: yourFrame2];
[myView addSubview: [player2 view]];
// ...
[player2 play];
May be When Creating a WebView and using a HTML5 Video instance you can run multiple videos at the same time

Adding video to an iPhone application

How do I add a video to an iPhone app. using iPhone SDK 3.2.5?
I dont see the actual video but I can hear it playing.
Here is the code:
-(void) viewDidLoad{
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Facebook" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
[super viewDidLoad];
}
First off – find out what video format you are having. I assume it's not the one supported by iPhones.
So when you learn this you can go ahead and look for a third party player that is able to play back your video. I'm sure there are a lot of them there. Recommendation from me – try using VLC.
If you want to use default videos app on your iPhone (which, of course, is way better than any other alternative) you can try out Windows & Mac application WALTR 2: https://softorino.com/waltr/windows-guides/how-transfer-videos-to-iphone-without-itunes. It adapts any video & music for iPhone playback and pushes them to your default app on an iPhone.
You need to set the frame for you MPMoviePlayerController and add it to your view:
theMovie.view.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
[self.view addSubview:theMovie.view];

Pros and cons of MPMoviePlayerController versus launching UIWebView to stream movie

I have a client who has video content for the web in Flash format. My task is to help them show the videos in an iPhone app.
I realize that step one is to get these videos into the appropriate Quicktime format for the iPhone.
Then I'm going to have to help the client figure out how or where to host these files. If that's tricky I assume they can be hosted at YouTube.
My chief concern, though, is which approach to take to stream the video. What are the pros and cons of MPMoviePlayerController versus launching UIWebView with the URL of the stream? Is there any difference? Is one of them more or less forgiving? Is one of them a better user experience? Any gotchas I might expect to run into?
I'm assuming playing video is pretty easy on the iPhone. Is it reasonable to try both and have one available as a fallback, or would that be a waste of time? I'm trying to schedule this out a bit, so I'd love to hear real-world experiences from anyone who's done this.
EDIT: My original example in this answer initialized the webview with a frame of CGRectZero. This worked up to iOS 3.2. Starting with iOS 4 the webview must have a nonzero frame or the video won't play. I've edited my example below to reflect this change.
The accepted answer here isn't accurate. You can in fact use UIWebView to stream videos, and in some ways it's better than MPMoviePlayerController. If you tell UIWebView to request a video file (e.g. an mp4) via loadRequest:, it will open a new window and stream the video within your app. Unlike MPMoviePlayerController, the video window created by UIWebView can be rotated to landscape or portrait orientation. When the video ends, the user can close this window and return to your app.
EDIT 2: Since you can now implement a video player that rotates using MPMoviePlayerViewController, I can no longer think of a reason to use UIWebView for videos using the technique described in this answer.
Hint: Since UIWebView creates its own window to play the video, you don't even need to add the UIWebView to your view hierarchy. You can just create the UIWebView object and call loadRequest: to play the video without ever passing the object to addSubview:.
self.webView = [[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
NSURL *url = [NSURL URLWithString:#"http://www.jonathancoulton.com/music/thingaweek/CodeMonkey.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
UIWebView cannot actually play videos. Navigating to a Youtube page with a UIWebview will simply launch the iPhone's Youtube App. Doing this a certain way will return control to your app after the video is played. See here: http://iphoneincubator.com/blog/tag/uiwebview
I would recommend using MPMoviePlayer Controller, as long as you are only doing simple streaming. Here's some sample code to get you started:
NSString *url = #"http://www.example.com/path/to/movie.mp4";
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:url]];
[moviePlayer play];