AVPlayerItem and KVO - am i missing something? - ios

so in the init of a simple Controller i have this code:
self.playerItem = [AVPlayerItem playerItemWithURL:url];
[self.playerItem addObserver:self forKeyPath:#"status" options:0 context:nil];
which should try and load media from the url, right? i'm implemeting
observeValueForKeyPath:ofObject:change:context:
however, this method is never called. Clueless?

playerItem starts working after being assigned to a AVPlayer object (duh)
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

Related

AVPlayer does not use prelload

I have a problem when I try to play some remoute audio file.
For this I use AvPlayer.
How you know for play something at first we start preload:
init AVURLAsset
set observer
init avplayer
example:
if (trackUrl) {
self.asset = [AVURLAsset assetWithURL:trackUrl];
NSArray *assetKeys = #[#"playable", #"hasProtectedContent"];
self.playerItem = [AVPlayerItem playerItemWithAsset:self.asset
automaticallyLoadedAssetKeys:assetKeys];
NSKeyValueObservingOptions options =
NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew;
[self.playerItem addObserver:self
forKeyPath:#"status"
options:options
context:playerItemContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(trackDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
[self removePeriodicTimeObserver];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
return;
}
So, for audio file which does not have this field at response header "content-disposition:" everything works fine. Preload process is really fast.
example: http://podcast.cbc.ca/mp3/podcasts/asithappens_20160907_50906.mp3
But for audio file which has this field at response header "content-disposition:". Preload process is really long.
example: https://ondemand.npr.org/anon.npr-mp3/npr/fa/2017/01/20170124_fa_01.mp3?orgId=427869011&topicId=1033&d=2209&p=13&story=511387528&t=progseg&e=511402985&seg=1&siteplayer=true&dl=1
It looks like AvPlayer tries to load all file and only after that play this.
Question:
Do you know how I can omit this long preload?
How can I miss this field "content-disposition:" at response?

ios AVPlayer failed to stream remote video

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

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