How can i play a video on device from server? - ios

I am trying to play a video that is on my server. its short video. Nearly 2 minutes. i must download it and play whenever download finished. How can i do that. i havent opinion? do you know any article or code ? Thanks.

NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
[moviePlayer play];`enter code here`

Related

how to play the video in objective-c

I need to play the video in Xcode using objective-c. I have dragged an mp4 file in Xcode. And used the code:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"videoname" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];
But showing the error as nil in filepath.
While adding item by drag and drop You should take Care following things
1) While drag and drop to XCode you must check check box copy item if needed
2) Check Target on right panel should be checked with your current target
try as
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"videoname" ofType:#"mp4"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
Drag and drop video file into your project and must check box
copy item if needed
Also add your video file in build phases copy bundle Resources

How to add video to iOS app intro screen like Uber

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;
}

I am trying to play a video from URL which is playing in VLC media player but not playing in native player i.e MPMoviePlayer

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!

MPMoviePlayerViewController with dropbox URL error: _itemFailedToPlayToEnd:

I get the above error with the following code:
NSURL *url = [NSURL fileURLWithPath:#"https://www.dropbox.com/s/crzu6yrwt35tgej/flexao.mp4?dl=1" isDirectory:NO];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
When I copy the file to my bundle and load the url from there it works.
I guess it has something to do with my dropbox link, but I also don't know how to generate a different link or filename.
Modify your code as below
//You're using the URL https://www.dropbox.com/s/crzu6yrwt35tgej/flexao.mp4, but that's not a link to a video... it's a link to a page with a video on it.
To convert it to a direct link to the video, change www.dropbox.com to dl.dropboxuser.com, like this: https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4
NSURL *url = [NSURL URLWithString:#"https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4"];
self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:self.player];
Hope it fixes the issue..!

How to change the speed of video playback

I'm wondering if it's possible to change the video playback speed in an iphone application. we want users to yell in the microphone to speed up the playback and get to the end.
You have to use setCurrentPlaybackRate:
There is a rate property for the AVPlayer.
If you take the example from Apple called "avPlayerDemo" in the resource section, you then simply have to set the mplayer.rate. It worked for me, I created a new slider in the xib files, implemented that slider in the AVPlayerDemoPlaybackViewController and simply set mPlayer.rate to the slider value.
What about the MPMoviePlayerController?
setCurrentPlaybackRate
Here's some code which does not work at that spot
-(IBAction)abspielen:(id)sender
{
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:titleOfButton ofType:#"mov"];
NSURL *movieURL = [ NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *themovie = [[MPMoviePlayerController alloc]initWithContentURL: movieURL];
[themovie play];
[themovie setCurrentPlaybackRate:2.f];
[themovie release];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer release];
}

Resources