Best way to play video from Url in iOS - ios

I am playing a video from Url, It is playing but very slow. I am using my mobile data.
Her is the code I play the video:
self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:_selectedVideo.videoLink]];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];
It is really slow and takes long time to start. Is there any way to make it faster?
Thanks

You can try mpmovieplayer to play video.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame
[myView addSubview: player.view];
// ...
[player play];

Related

AVPlayer not showing Done button in objective c

I am trying to show live streaming in bottom left corner of my screen,Currently live video is playing smoothly with all controls(Play, pause ,volume,full screen).But close/done button is not there.so I am not able to close live streaming window at any time I want to close it.I am using AVPlayer to achive same .Below is my code.
I have tried MPPlayer also but had same problem with that.
I had taken one close button and tried playerlayer.removefromsuperlayer but this also not worked for me.
I just want one close button which will close live streaming from my app
Please suggest any another player if they support close functionality
NSURL *videourl = [NSURL URLWithString:#"my url"];
AVPlayer *player = [AVPlayer playerWithURL:videourl];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
playerViewController.view.frame = CGRectMake(0, 400, 300, 200);
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = CGRectMake(0, 400, 300, 200);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//externalPlaybackVideoGravity
playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
playerViewController.showsPlaybackControls = YES;
[self addChildViewController:playerViewController];
[self.view.layer addSublayer:playerLayer];
[self.view addSubview:playerViewController.view];
playerViewController.player.muted = TRUE;
[playerViewController.player play];

Objective-C How to play videos on MPMoviePlayerController or AVPlayer instantly, without delay?

I have tried playing 500 KB videos on AVPlayer and MPMoviePlayerController, and they both have a slightly annoying delay. AVPlayer and MPMoviePlayerController both display a black screen for 2-3 seconds, and then it starts the video. Thats really annoying, how come it takes so long for the video to start? How can I fix it?
//Movie Player Code
_moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:video];
_moviePlayerVC.moviePlayer.view.userInteractionEnabled = YES;
_moviePlayerVC.moviePlayer.view.multipleTouchEnabled = YES;
_moviePlayerVC.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayerVC.moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayerVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
_moviePlayerVC.moviePlayer.view.frame = self.frame;
[_moviePlayerVC.moviePlayer setShouldAutoplay:YES];
_moviePlayerVC.view.frame = self.frame;
[self addSubview:_moviePlayerVC.view];
//AVPlayer Code
AVAsset *asset = [AVAsset assetWithURL:video];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
_player = [[AVPlayer alloc] initWithPlayerItem:item];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
layer.frame = self.frame;
layer.backgroundColor = [UIColor clearColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
You could preload an AVPlayer and observe its status. Once it changes AVPlayerStatusReadyToPlay you should be able to start playback without any delay.

RuTube MPMoviePlayerController initWithContentURL:

I am trying to play a video from url and the only thing i am getting is a black screen and a loading wheel?
I have a XML/JSON from witch i need somehow to play the video,
I tried with all 3 links i have there and no response
NSURL *url = [[NSURL alloc] initWithString:#"http://rutube.ru/play/embed/7224398"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(self.view.frame.origin.x , self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES];
[moviePlayer setShouldAutoplay:YES];
Is there a chanse to start this video using MPMoviePlayerController ??
P.S: I managed to load the video in a WebView but its not what i need.
You have to extract direct link to .mp4 file somehow.
Check this article.

Load MPMoviePlayer Without playing video. Just preview

I am playing a m4v video in my app. It is playing fine but i just want to load the video without playing it. Just the preview.
I am using following code.
-(void)test
{
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"YOUR_PATH"]];
//[self.player prepareToPlay];
[self.player setControlStyle:MPMovieControlStyleDefault];
[self.player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.player.view];
}
But If i am uncommenting [self.player prepareToPlay];, It is starting to play.
If I comment [self.player prepareToPlay];, video is not loading.
try this
-(void)test
{
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"YOUR_PATH"]];
[self.player prepareToPlay];
[self.player play];
[self performSelector:#selector(pausePlayer) withObject:nil afterDelay:0.1];
[self.player setControlStyle:MPMovieControlStyleDefault];
[self.player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.player.view];
}
-(void)pausePlayer
{
if (self.player)
{
[self.player pause];
}
}

MPMoviePlayerController won't load an HTTP audio stream

I'm trying to use an MPMoviePlayerController on iOS to play back an audio stream over HTTP.
I've imported MediaPlayer.framework and used the code from MPMoviePlayerController's documentation, but I get no sound.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://shoutmedia.abc.net.au:10326"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
[player play];
I've also tried adding player.movieSourceType = MPMovieSourceTypeStreaming; but didn't work neither.
Any thoughts?

Resources