Error about using AVQueuePlayer - ios

I init a AVQueuePlayer like :
AVPlayerItem *item1 = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:#"http://XXX.mp4"]];
AVPlayerItem *item2 = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:#"http://YYY.mp4"]];
AVQueuePlayer player = [[AVQueuePlayer alloc]initWithItems:#[item1,item2]];
and I want to show it by AVPlayerViewController so I init a AVPlayerViewController like :
AVPlayerViewController *playerController = [[AVPlayerViewController alloc] init];
playerController.player = player;
...
...
[self.view addSubview:_playerController.view];
[self addChildViewController:_playerController];
now I want to play it:
[player play];
Question:
It always crashes at main when the first AVPlayerItem end play.
Did I do something wrong or wrong way to show it?
(AVPlayer is OK by this way)

Related

AVQueuePlayer doesn’t play wav file smoothly from remote URL. It is choppy and stops in the middle

Here is code for initialise AVQueuePlayer.
AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] init];
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
if ([queuePlayer respondsToSelector:#selector(automaticallyWaitsToMinimizeStalling)])
queuePlayer.automaticallyWaitsToMinimizeStalling = NO;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
if (playerItem)
[queuePlayer insertItem:playerItem afterItem:nil];
if ([queuePlayer respondsToSelector:#selector(playImmediatelyAtRate:)])
[queuePlayer playImmediatelyAtRate:1.0];
else [queuePlayer play];

iOS stream playback with AVPlayer don't work

I play video streams with AVPlayer.
Some of streams plays only if click button "Play" for 2-5 times. (sorry, I can't provide link, and don't sure what especial in this streams, but may be problem only with full hd streams)
I do like that:
fields:
AVPlayerItem *playerItem;
AVPlayer *player;
AVPlayerViewController *playerViewController;
in viewDidLoad:
player = [[AVPlayer alloc] init];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = player;
when click "Play":
NSURL *videoURL = [NSURL URLWithString:strUrl];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addObserver:self
forKeyPath:#"status"
options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];
[player replaceCurrentItemWithPlayerItem:playerItem];
and observing:
NSLog(#"status changed");
if (((AVPlayerItem *)object).status == AVPlayerStatusReadyToPlay)
{
NSLog(#"ready to play");
[playerItem removeObserver:self forKeyPath:#"status"];
[self presentViewController:playerViewController animated:YES completion:nil];
[player play];
}
else if (((AVPlayerItem *)object).status == AVPlayerStatusFailed)
{
NSLog(#"failed to ready");
}
else if (((AVPlayerItem *)object).status == AVPlayerStatusUnknown)
{
NSLog(#"unknown");
}
First always "unknown" but in problem cases no messages after (but most streams play normally and I see "ready to play" message)
I've tried to observe player.status but when it ReadyToPlay and I start play I see only first frame and endless message "Loading" in top of screen.

AVPlayer not showing FullScreen

I am working on playing a recorded video recorded by AVCapture.I am saving the video URL in string named outputFileURL. I tried playing back the video using AVPlayerLayer concept. The code I used is
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
But the video I am getting is not full screen.
Can anyone can help me to solve?
I added the following code and I am able to get the full screen.
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
Hope this may help.
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.delegate = self;
playerViewController.player = player;
[playerViewController.player play];
[self presentViewController:playerViewController animated:YES completion:nil];
Try This
Ok than you have to use MPMoviePlayerController
NSString *path = [[NSBundle mainBundle] pathForResource:#"Video_Intro" ofType:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
moviePlayer.view.frame = ivVideoThumbnail.frame;
//moviePlayer.view.top += 20; // Add to fix 20 pixel diff of moviePlayer view
// Register this class as an observer instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[svIntro addSubview:moviePlayer.view];
moviePlayer.fullscreen = true;
[moviePlayer play];
Also implement Observer method
-(void)movieFinishedCallback:(id)mPlayer
{
[moviePlayer.view removeFromSuperview];
}

How to hide AVPlayerViewController control bar?

I've implemented iAd's Preroll Video Ad and I want to guarantee that my user will watch the entire advertisement. How do I hide the AVPlayerViewController's control bar so the user can not tap "Done" and get out of the video before it finishes?
self.canDisplayBannerAds = YES;
[AVPlayerViewController preparePrerollAds];
player = [[AVPlayerViewController alloc] init];
player.showsPlaybackControls = NO;
player.delegate = self;
Simple code
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.showsPlaybackControls = FALSE;
You can use this code to do the same. You need to call play when you present the controller.
playerItem = [[AVPlayerItem alloc] initWithURL:url];
if(playerItem) {
player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = _player;
[playerViewController setShowsPlaybackControls:NO];
[parentViewController presentViewController:playerViewController animated:YES completion:^{
[playerViewController.player play];
}];

Creating an AVPlayer for video

I am creating an AVPlayer to display a video. I have some code to create the Player but nothing displays. I'm not sure if the URL is not working or if the player it's self is not working.
Here is the code I am using to create the player:
self.playing = NO;
AVPlayer *player = [[AVPlayer alloc] init];
player = [player initWithURL:self.videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[[[self view] layer] insertSublayer:playerLayer atIndex:3];
[playerLayer setFrame:self.view.bounds];
[player play];
Here is the code to save the video:
self.videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"%#", self.videoURL);
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.videoURL = self.videoURL;

Resources