AVPlayer won't play a different format - ios

I am implementing a player with AVPlayer, which will play videos from a URL. This URL can be for "fixed-length" videos (that have a predetermined duration) and live videos (whose duration is unknown/not defined).
The fixed-length URLs I am using link to .mp4 files (using H264 codec), while the live videos have a .m3u8 extensions (but also H264).
I can play a number of .m3u8 URLs, but when I try to play the .mp4, it won't load.
However, if I start the app and play a .mp4 URL first, I can play other .mp4 URLs, but I can't switch to the .m3u8.
Both formats play fine, but there some issue when I switch from one to another.
These are the methods I use to instantiate and "clean" the player:
- (void)prepareAndPlayAutomatically:(BOOL)playAutomatically {
currentItem = [AVPlayerItem playerItemWithURL:videoURL];
[[self player] replaceCurrentItemWithPlayerItem:currentItem];
[self.layer addSublayer:[self playerLayer]];
[[self player] addObserver:self forKeyPath:#"rate" options:0 context:nil];
[[self player] seekToTime:kCMTimeZero];
if (playAutomatically) {
[[self player] play];
}
}
- (void)clean {
[[self player] pause];
[[self player] seekToTime:kCMTimeZero];
[[self player] replaceCurrentItemWithPlayerItem:nil];
[[self player] removeObserver:self forKeyPath:#"rate"];
[self removeFromSuperview];
}
[self player] will return a singleton AVPlayer instance;
[self playerLayer] will also return a singleton AVPlayerLayer instance;
- (AVPlayer *)player {
static dispatch_once_t once;
static AVPlayer *player;
dispatch_once(&once, ^{
player = [AVPlayer playerWithPlayerItem:currentItem];
});
return player;
}
- (AVPlayerLayer *)playerLayer {
static dispatch_once_t once;
static AVPlayerLayer *playerLayer;
dispatch_once(&once, ^{
playerLayer = [AVPlayerLayer playerLayerWithPlayer:[self player]];
});
return playerLayer;
}
I've narrowed down the problem to these singletons. If I remove the singleton pattern, no problem will occur, and I am able to play both kinds of videos.
Is there anything else I am supposed to do when I "clean" the player?

It seems that AVPlayer class is not intended for reuse for items with different video compositions.
From the docs on [AVPlayer replaceCurrentItemWithPlayerItem:] method:
Special Considerations
The new item must have the same compositor as the item it replaces, or
have no compositor.

I too ran into same problem recently. I solved it by maintaining 2 instances of the AVQueuePlayer - One to play Mp4 and other for M3u8.
And switched between these players by hiding one player view and showing the other based on the video format (Mp4/M3u8).

Related

iOS AVPlayer Does Not Play the Next Song in a Playlist After App is Backgrounded Then Screen is Locked

I am working on this iOS app that plays music from URLs. Currently it has no issues playing through a playlist while the app is foregrounded, backgrounded, or even when the screen is locked ONLY IF the app was in the foreground prior to locking.
If the app is backgrounded THEN has the screen locked, the next song will load, but it will not play.
My question is: How can I get the AVPlayer to play the next song in a list while the app is in the background and the screen is locked?
Below is the code I am using to load and play the songs...
- (void)loadNextSong {
if (self.playlistIndex < self.playlist.count-1) { //check to see if the next track is in bounds of the playlist array
self.playlistIndex++;
[self.player pause];
[self serviceDidFetchSong:self.playlist[self.playlistIndex]];
[self.player play];
} else { //play the first song if we reach the end of the playlist
self.playlistIndex = 0;
[self.player pause];
[self serviceDidFetchSong:self.playlist[self.playlistIndex]];
[self.player play];
}
}
- (void)serviceDidFetchSong:(Song *)song {
if (song.audioFileURL != nil) {
if (![self.song.audioFileURL isEqual:song.audioFileURL]) {
if (self.timeIntervalObserver) {
[self.player removeTimeObserver:self.timeIntervalObserver];
}
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:song.audioFileURL options:0];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
//this is just for a visual of the song progress
__block __weak SongPlayerViewController *blockSelf = self;
self.timeIntervalObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds((1.0f/5.0f), NSEC_PER_SEC)
queue:NULL
usingBlock:^(CMTime time){
[blockSelf updateProgressBar];
}];
}
self.song = song;
}
}
After trying to debug with some console logs and break points, I have verified that [self.player play] is definitely being called and that the correct URL is loaded each time.
My info.plist file has audio and fetch enabled for the required background modes.
The next song just doesn't seem to play at all when the app has been backgrounded and the screen is locked.
Any insight or suggestions would be much appreciated. Thank you.
EDIT
This seems to be an intermittent issue which appears much less frequently since the release of iOS 9.3 which leads me to believe this may have been related to the OS...?
It works when you play it twice.
player?.play()
player?.play()

What is the better way to implement video files playback in application background?

I do need to implement iOS application with video files played back in background.
E.g. on first viewController one video should be played back in background instead of some photo or color background.
on second viewController another video should be played back in background instead of some photo or color background.
and so on.
What is the best way to implement this?
* is it better to import those video files in project?
* or is it better to store them in some external place and playback via network?
From the AppStore approval point of view and from the Apple Guidlines point of view - is this case with video correct? Or it's better to avoid video usage in mobile applications?
Thank you in advance.
Found solution with local video files playback via native AVPlayer from AVFoundation
1.Import AVFoundation:
#import <AVFoundation/AVFoundation.h>
2.Use property for player:
#property (nonatomic) AVPlayer *avPlayer;
3.Add video file into "Video" folder and added "Video" into project
4.Initialize the player
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"shutterstock_v885172.mp4" ofType:nil inDirectory:#"Video"];
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];
5.Subscribe for event - video did play to the end
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
6.Resume video playback to the very start in related method
- (void)itemDidFinishPlaying:(NSNotification *)notification {
AVPlayerItem *player = [notification object];
[player seekToTime:kCMTimeZero];
}

Play next video on button click in AVPlayer

I have 2 videos. I am using AVPlayer to play videos. The first one is loaded when App starts, and when I click on the next video, it should start playing that video from the start. How can it be done?
I am using the following code in attempt to switch to the next video
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithURL:url];
[self pause];
[self.moviePlayer replaceCurrentItemWithPlayerItem:playerItem];
[self.moviePlayer seekToTime:kCMTimeZero];
[self play];

UI is Freezing, when streaming the videos from an internet using AVPlayer

I am implementing Media Player in iOS Platform. I have a problem with UI Freezing, when streaming the videos from the internet using AVPlayer. Note: I'm not using AVAudioPlayer, AVQueuePlayer. Here following code for playing the media: UI Freeze is occurring only start Streaming.
if(_player.rate!=0.0)
{
[_player pause];
[ad.player.playerLayer removeFromSuperlayer];
[_player replaceCurrentItemWithPlayerItem:[ AVPlayerItem playerItemWithURL:_tempURL]];
}
else
{
_player = [_player initWithURL:mediaURL];
ad.player.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
ad.player.playerLayer.frame=ad.player.bounds;
ad.player.playerLayer.frame=CGRectMake(0, 0, 320, 150);
[ad.player.playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
ad.player.playerLayer.needsDisplayOnBoundsChange = YES;
[ad.player.layer addSublayer:ad.player.playerLayer];
[_player play];
}
I referred the following Link:
AVPlayer "freezes" the app at the start of buffering an audio stream
But that link suggested for AVQueuePlayer. But my Requirement is to do in AVPlayer
When you start playing the video it hasn't downloaded any data yet, AVPlayer class has a method called prerollAtRate:completionHandler which loads data starting at the item’s current playback time, which then calls a completionHandler whens its finishes the load attempt.
__weak typeof(self) weakSelf = self;
[self prerollAtRate:0.0 completionHandler:^(BOOL finished) {
NSLog(#"ready: %d", finished);
// if ready call the play method
if (finished) {
dispatch_async(dispatch_get_main_queue(), ^{
// call UI on main thread
[weakSelf.player play];
});
}
}];

No AVPlayer Delegate? How to track when song finished playing? Objective C iPhone development

I've looked around but I can't find a delegate protocol for the AVPlayer class. What gives?
I'm using its subclass, AVQueuePlayer, to play an array of AVPlayerItems, each loaded from a URL. Is there any way I can call a method when a song finishes playing? Notably at the end of the queue?
And if that's not possible, is there any way I could call a method when the song STARTS playing, after buffering? I'm trying to get a loading icon in there but it turns the icon off before the music actually begins, even though it's after the [audioPlayer play] action.
Yes, the AVPlayer class does not have a delegate protocol like the AVAudioPlayer. You need to subscribe to notifications on an AVPlayerItem. You can create an AVPlayerItem using the same URL that you would otherwise pass to -initWithURL: on AVPlayer.
-(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
AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];
// Begin playback
[player play]
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
}
Yes. Add a KVO observer to the player's status or rate:
- (IBAction)go {
self.player = .....
self.player.actionAtItemEnd = AVPlayerActionStop;
[self.player addObserver:self forKeyPath:#"rate" options:0 context:0];
}
- (void)stopped {
...
[self.player removeObserver:self]; //assumes we are the only observer
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == 0) {
if(player.rate==0.0) //stopped
[self stopped];
}
else
[super observeVal...];
}
So basically, that's it.
Disclaimer: I wrote that in here so I didn't check if the code was good. ALSO I never used AVPlayer before but it should be about right.
Swift 3 - I add an observer to AVPlayerItem every time I add a video to the player:
func playVideo(url: URL) {
let playerItem = AVPlayerItem(asset: AVURLAsset(url: someVideoUrl))
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidPlayToEndTime), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
self.player.replaceCurrentItem(with: playerItem)
self.player.play()
}
func playerItemDidPlayToEndTime() {
// load next video or something
}
There is a lot of information in the Apple docs AVFoundation Programming Guide (look for the monitoring playback section). It appears to be mainly through KVO so you may wish to brush up on that if you are not too familiar (there is a guide for that to Key Value Observing ProgrammingGuide.
I'm using this one, and it works:
_player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:_playingAudio.url]];
CMTime endTime = CMTimeMakeWithSeconds(_playingAudio.duration, 1);
timeObserver = [_player addBoundaryTimeObserverForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:endTime]] queue:NULL usingBlock:^(void) {
[_player removeTimeObserver:timeObserver];
timeObserver = nil;
//TODO play next sound
}];
[self play];
where _playingAudio is my custom class with some properties and timeObserver is id ivar.

Resources