ios AVPlayer failed to stream remote video - ios

I have a AVPlayer that plays video from remote url.
Here is my code:
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:videoUrl];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = playerView.bounds;
[playerView.layer addSublayer:playerLayer];
[self.player play];
When I start to stream I have only less then a second video chunck and then downloading stops and nothing happens.
MPMoviePlayerController and browser plays this video as usual.
I also doubt, that it might be effect of cropping video (because videos without cropping works fine). Here is the guide I use to crod video http://www.one-dreamer.com/cropping-video-square-like-vine-instagram-xcode/
Also clean app with same setup can't play video.
Any ideas? thanks!

Use the AVPlayerItemPlaybackStalledNotification
[[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemPlaybackStalledNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[weakself performBlockOnMainQueueAfterDelay:1.5 block:^{
[weakself.player play];
}];
}];

Related

Vine’s Infinite Video Loops in iOS 9 (seamlessly video)

I need some help with Infinite Video Loops like a Vine did.
I tried a lot of approaches and all of them have a short delay.
One of most popular:
__weak typeof(self) weakSelf = self; // prevent memory cycle
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
object:nil // any object can send
queue:nil // the queue of the sending
usingBlock:^(NSNotification *note) {
// holding a pointer to avPlayer to reuse it
[weakSelf.avPlayer seekToTime:kCMTimeZero];
[weakSelf.avPlayer play];
}];
Is there any way to eliminate the delay and play local videos seamlessly?
I know that Apple add some updates in iOS 9, but I need that it work from 8+ ios
Sorry for Ojc-C sample, I use Swift
Try this
-(void)startPlaybackForItemWithURL:(NSURL*)url {
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
// Pass the AVPlayerItem to a new player
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
// Begin playback
[player play]
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
[self.avPlayer play];
// Will be called when AVPlayer finishes playing playerItem
}

AVPLayer show black screen when screen load

I am using AVPLayer to player video in UITableView. Video play properly but sometime initially when video played, sound is coming but screen in black. Video is visible after 5-6 second of video is played. I am using following code:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayer = avPlayer;
__weak CLBAVPlayer *weakSelf = self;
[self.avPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC)
queue:nil
usingBlock:^(CMTime time) {
[weakSelf progress];
}];
self.layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Please help me to figure out the issue.
I got the answer for this. Actually the issue was that when I was fetching layer and adding it as sublayer then it was not on main queue. Using dispatch_async with the main queue solved my problem.

AVPlayer iphone to play sound even views are changed

I am developing a music player app where the tracks or sounds are played from the url and I m using AVplayer to play the tracks from the url
Basically i want to keep on playing the tracks even user navigated or goes to new other or the application goes to background !! i really don't have a idea how to achieve the same..
these is the demo code I am using to get the track from the URL
NSURL *url = [NSURL URLWithString:#"<#Live stream URL#>];
// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
//(optional) [playerItem addObserver:self forKeyPath:#"status" options:0 context:&ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
i even wanted to play next track if the current played track is over.

MPMoviePlayerController doesn't play video

I'm using this code to try and play a video. However the video is never played, the call back is never made to say it's finished. I can't see anything that I'm missing or have where it shouldn't be. I've checked the path and that's fine. The video playback view does appear. It just doesn't play at that point.
Any suggestions?
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:#"IntroMovie" withExtension:#"mov"];
NSLog(#"%#", movieURL);
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController prepareToPlay];
[moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
The problem lies within the fact, that your moviePlayerController is being cleaned up due to ARC.
Try if it helps to introduce a strong property for the MPMoviePlayerController.

MPMoviePlayerController interrupts AirPlay iPod audio, but it works fine on the device

I'm using MPMoviePlayerController to play a video clip that loops. In the app delegate, I set my AVAudioSession's category to AVAudioSessionCategoryAmbient so that my video clip doesn't interrupt iPod audio.
This works great when playing iPod audio on the device, but when using AirPlay, the audio is briefly interrupted every time my looping video starts over, which is pretty frequent (and annoying).
In AppDelegate.m:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:#"AVAudioSessionCategoryAmbient" error:nil];
In my video view controller:
self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.videoPlayer.useApplicationAudioSession = YES;
self.videoPlayer.controlStyle = MPMovieControlStyleNone;
self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
I've scoured the net and can't seem to find answers. Any advice would be great. Thanks!
Problem solved! I just had to use AVPlayer instead of MPMoviePlayerController.
self.avPlayer = [AVPlayer playerWithURL:url];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.avPlayerLayer];
[self.avPlayer play];
And to make it loop:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
...
- (void)movieDidFinish:(NSNotification *)notification {
[self.avPlayer seekToTime:kCMTimeZero];
}

Resources