Hi in my application I have avplayerViewController. I just launch the application and click on start play button - which will launch avplayerviewcontroller. There i just drag the seek bar and content is buffering before content start play i just close the playerViewcontroller. After few seconds content is start playing (audio is coming). It's very serious issue please guide me how to resolve this issue. As of now when ever i close the player i am doing below operations.
#try{
[self.player replaceCurrentItemWithPlayerItem:nil];
[self.player setRate:0.0];
[self.player seekToTime:CMTimeMake(0, 1)];
[self.player pause];
dispatch_async(dispatch_get_main_queue(), ^{
[self.contentOverlayView sendSubviewToBack:self->playerActivityIndicator];
[self->playerActivityIndicator stopAnimating];
});
// [[self.player currentItem] removeObserver:self forKeyPath:#"status"];
[[self.player currentItem] removeObserver:self forKeyPath:#"playbackBufferEmpty"];
[self.player removeTimeObserver:self->playerPeriodicTimeObserver];
}#catch(id anException){
//do nothing, obviously it wasn't attached because an exception was thrown
NSLog(#"No observer for a player in hide placeholder");
}
Related
Hi in my application i am launching AVPlayerViewController and while playing content i will fast forward the player after fast forward it will take sometime to start play before it start play again i will close the player.In this scenario i can able to hear audio even after close player.How to resolve this issue please let me know.
I am making player nil and setRate to zero when closing the player.
#try {
[self.player.currentItem removeObserver:self forKeyPath:#"status"];
[self.player.currentItem removeObserver:self forKeyPath:#"playbackBufferEmpty"];
[self.player replaceCurrentItemWithPlayerItem:nil];
[self.player setRate:0.0];
self.player=nil;
} #catch (NSException *exception)
{
NSLog(#"Execption is %#",exception);
}
Player audio should stop when ever we close player. Thanks in advance.
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];
});
}
}];
I have a video on a screen that must be played with an infinity loop.
So I wrote :
self.player = [AVPlayer playerWithPlayerItem:avItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
[_player play];
And I fallback method is :
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[_player.currentItem seekToTime:kCMTimeZero];
[_player play];
}
It works well but sometimes it seems that my fallback is not called. The video is freezing at the end and never played again. It's happening randomly ...
Do you have an idea ?
You can just set a boundary time observer.
- (id) setupBoundaryEndWith:(NSArray*)array
{
__weak MY_AVPlayer* weakself = self;
return [self addBoundaryTimeObserverForTimes:array queue:NULL usingBlock:^{
[weakself stopAVPlayerAndLoopOrTriggerNextTrack];
}];
}
[_player currentItem] is null, since you haven't started playing yet. I would suggest either to explicitly add the AVPlayerItem (ideally) or register to notifications after starting the playback (reverse the 2 last lines).
I have not tried the 2-nd solution, so it might not work, if it takes some time to start the playback. If that is the case, I suggest setting an NSTimer to trigger a second later and then register.
This question already has answers here:
Play YouTube videos in iPhone app without using UIWebView?
(5 answers)
Closed 9 years ago.
I'm making this iPhone app that allows people to watch youtube videos and vimeo videos. I was wondering if there's a custom player I can incorporate into my app that will play the video from the youtube link or vimeo link. This has to be iOS 7 compatible as well. Thanks!
Possibly you are looking for AVFoundation.framework. Just add it to your project and you can use its AVPlayer component to allow a total customization of your interface. It would be something like this:
-(void)viewDidLoad {
//prepare player
self.videoPlayer = [AVPlayer playerWithURL:<# NSURL of your youtube video #>];
self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
//prepare player layer
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
self.videoPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.videoPlayerLayer];
//add player item status notofication handler
[self addObserver:self forKeyPath:#"videoPlayer.currentItem.status" options:NSKeyValueObservingOptionNew context:NULL];
//notification handler when player item completes playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
//called when playback completes
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.videoPlayer seekToTime:kCMTimeZero]; //rewind at the end of play
//other tasks
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"videoPlayer.currentItem.status"]) {
//NSLog(#"player status changed");
if (self.videoPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay) {
//player is ready to play and you can enable your playback buttons here
}
}
}
And like normal VC you can add buttons or other items to invoke these methods for playback:
-(IBAction)play:(id)sender {
[self.videoPlayer play];
}
-(IBAction)pause:(id)sender {
[self.videoPlayer pause];
}
Make sure that you remove the observers previously added before you leave the VC or else those are gonna leak:
//remove observers
#try {
[self removeObserver:self forKeyPath:#"videoPlayer.currentItem.status" context:NULL];
}
#catch (NSException *exception) {}
#try {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}
#catch (NSException *exception) {}
A detailed explanation from Apple is available here. Hope you would find it helpful.
1) Youtube uses flash which does not work with native mediaplayer.
2) Native media player either needs a file or it can live stream url which use HTTPLiveStreaming (it does not support RTSP).
In simple words they work on different protocol and can't talk to each other.
However, you can use third party API's to download youtube video and then play but this requires your user to wait until the entire file is downloaded.
I would recommend you to use webview. here is a good article which shows how to do so.
YoutubeHacks is an opensource tool for the same.
Say we are downloading a large video - its 50Mb - but only 25Mb has downloaded so far - is it possible to start playing the video before its completely downloaded?
I am going to assume you are using MPMoviePlayerController for your video...
MPMoviePlayerController has a loadState property that you can monitor with this notification. Check to see if the video has been downloaded to a playable extent and then play.
//Begin observing the moviePlayer's load state.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
- (void)moviePlayerLoadStateChanged:(NSNotification *)notification{
//NSLog(#"State changed to: %d\n", moviePlayer.loadState);
if(moviePlayer.loadState == MPMovieLoadStatePlayable && [videoIndicator isAnimating]){
//if load state is ready to play
[videoIndicator stopAnimating];//Pause and hide the indicator
[self.contentView addSubview:[moviePlayer view]];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];//play the video
}
}
And this is very important: If you are trying to download a video more than 10 ten minutes long, you will need to use HTTP Live Streaming.