I want to launch Media Player upon clicking on a video link within UIWebView. The content of the UIWebView is an HTML5 page on my server, so whether the solution is to change the code in XCode or HTML5 I will be able to handle it.
All the videos in question are of 'mp4' extension - Just in case this addition helps.
Please note this is for an iPad-only App.
This code could be modified to launch a video based on a button click from within the app.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"mp4"];
//LOCAL FILE
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
//REMOTE FILE
NSURL *fileURL = [NSURL URLWithString:#"http://someurlsomewhere.com/movie.mp4"];
player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
player.frame = CGRectMake(0, 0, 1024, 768);
[self.view addSubview:player.view];
player.fullscreen = NO;
[player play];
Here is a link that covers detecting taps/clicks from a webview which may be what you need in order to trigger the video.
how to intercept Button click inside UIWebview on IOS?
Related
I want to embed Facebook videos in my app. I am getting video links from a webservice.
For example:
"video_url": "https://www.facebook.com/urdutimes.ca/videos/520665921438799/"
Is it possible to embed this video in native iOS app using Objective-C?
Thanks in advance
Try this one:
NSURL *url = [NSURL URLWithString:#"https://www.facebook.com/urdutimes.ca/videos/520665921438799/"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mp];
Looking to add a video to my apps intro screen like the Uber app. I am sure there are others.
I found this which uses a gif and UIWebView, but not sure if this is the best solution. I definitely don't want to use images and stitch them together (would rather not have video if that is the preferred method).
https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2
This is what I use in my app Letsplay, The answer from Aslam will work but uses MPmoviePlayerController which is depreciated as of IOS 9.0.
Also I set the video gravity so that your video will fill the entire frame which means no black borders.
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"icebergs" ofType:#"mp4"];
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath: videoPath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:videoURL options:nil]];
AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer* videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
[videoPlayerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view.layer addSublayer:videoPlayerLayer];
[videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[videoPlayer play];
If you don't want sound then remember to mute the player
[videoPlayer setMuted:YES];
I haven't tried the code, but I it should work. It generally takes the path of the video file and adds it to the view controller. You may have to set the coordinates and size. Though I have used here video, GIF is more preferred.
- (void)viewDidAppear:(BOOL)animated
{
[self loadVideoInBackgroundOfView];
}
-(void)loadVideoInBackgroundOfView{
MPMoviePlayerController* videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getTheVideoPath]];
[videoPlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0];
[videoPlayer prepareToPlay];
[videoPlayer play];
}
-(NSURL*)getTheVideoPath{
NSString *path = [[NSBundle mainBundle] pathForResource:#"VideoFileName" ofType:#"mp4"];
NSURL *URL = [NSURL fileURLWithPath:path];
return URL;
}
It is showing like this in browser,when I click on play it doesn't do anything.
But the same URL is working fine in Android native player.
Here is my code and URL of that video stored in server, format of video is MP4.
I tried with some other URLs like
NSURL *url=[[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
NSURL *urll = [[NSURL alloc]initWithString:#"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
These are working fine but below one is not working
NSURL *url = [[NSURL alloc]initWithString:#"http://****IP****.com:8888/alias_1440247177838"];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 20, self.videoPlayView.frame.size.width, self.videoPlayView.frame.size.height * (1.0f/3.0f)-20);
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
[self.videoPlayView addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
Unfortunately MPMoviePlayer can only play video from a direct link. (similar to the ones that you mentioned in your question.
You can either:
use a direct link
Or switch to UIWebView instead of MPMoviePlayer
You can use a UIWebView in following way:
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
webView.allowsInlineMediaPlayback = YES;
NSURL* url = [NSURL URLWithString:#"http://****IP****.com:8888/alias_1440247177838"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Hope this helps!
i want to play video in ios one after another..I have two video files .m3u8 format. if video one completed then video two start automatically.. after video two complete then again video one should played automatically. and again video two should start automatically. how can i do this thing….I am using Mediaplayer framework. plz give me any idea…..Thanks in advance.
-(void)viewDidLoad
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"buyTutorial" ofType:#“m3u8”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self setController:moviePlayerController];
}
You should be able to use this notification:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/data/MPMoviePlayerPlaybackDidFinishNotification
And then look for this constant PlaybackEnded:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/tdef/MPMovieFinishReason
Then just start the next movie in your playback queue.
Here's some examples on how to register to the notification:
MPMoviePlayerPlaybackDidFinishNotification being called again in iPhone 4.3 simulator when setting contentURL
I tried to build a radio in my app using webView. But on loading the link my app crashes every time.
- (IBAction)playMusic:(id)sender {
NSString *stream = #"http://www.bbc.co.uk/radio/listen/live/r2.pls"; //just an example link
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[Webview loadRequest:urlrequest];
}
...but with a page link (like www.google.com) everything works fine.
Maybe there is also a better solution than using webView to play radio.
Thanks to all of you in advance
UIWebView is indeed not meant to play audio files. I would recommend you to use MPMoviePlayerController (part of the MediaPlayer framework) for what you're trying to achieve. I wrote some sample code to below to show how you how to get started.
If you need more information, I suggest you to take a look at the HTTP Live Streaming Resources or MPMoviePlayerController Class Reference
NSURL *URL = [NSURL URLWithString:#"http://www.bbc.co.uk/radio/listen/live/r2.pls"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:URL];
[moviePlayerController prepareToPlay];
[[moviePlayerController view] setFrame:CGRectMake(0.f, 0.f, [[self view] frame].size.width, 24.f)];
[[self view] addSubview:[moviePlayerController view]];
[self setMoviePlayerController:moviePlayerController];