I'm using an AVPlayer object to play a remote radio stream in my iOS app. The stream works fine and plays in the background.
Doing some connection tests I've come across some issues. Once the player connection is lost the player stops (as you'd expect) however i cannot make the player begin again when the connect is back.
I've crudely setup a timer to just hit [player play] every second to force it to start but no luck. My best guess is based on the lack of data being requested it's just died.
I do have an observer setup to monitor when the player is ready to start or if there is an error but seemingly this isn't being called at all after the initial time.
My question is how to i force the AVPlayer to begin getting the stream again when it's available.
I've subclassed audio player to BCRadio
-(BCRadio*)initWithRadioOneAndAutoPlay:(BOOL)autoPlay whenReady:(void(^)(void))ready whenFailed:(void(^)(void))failed whenBecameActive:(void(^)(void))active {
// Current item
self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:#"http://69.64.92.79:8276"]];
// Super it
self = [super initWithPlayerItem:self.playerItem];
// Observe for it become ready
[self addObserver:self forKeyPath:#"status" options:0 context:nil];
// If app comes from background and if app goes inactive
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appBecameActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
// Set blocks
self.playerReady = ready;
self.playerFailed = failed;
self.playerActive = active;
self.willAutoPlay = autoPlay;
// Register for remote notifications
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Play is pressed
[[NSNotificationCenter defaultCenter] addObserverForName:#"TogglePlay" object:nil queue:NULL usingBlock:^(NSNotification *notification){
[self doPlay:YES];
}];
// Pause is pressed
[[NSNotificationCenter defaultCenter] addObserverForName:#"TogglePause" object:nil queue:NULL usingBlock:^(NSNotification *notification){
[self doPlay:NO];
}];
// Check if playing
NSTimer *timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer){
[self statusMonitor];
} repeats:YES];
[timer fire];
// Monitor reachability and pause when needed
Reachability* reach = [Reachability reachabilityWithHostname:#"69.64.92.79"];
reach.reachableBlock = ^(Reachability*reach){
if(self.willAutoPlayOnResume && !self.playing){
self.willAutoPlayOnResume = NO;
[self doPlay:YES];
}
};
reach.unreachableBlock = ^(Reachability*reach){
if(self.playing){
self.willAutoPlayOnResume = YES;
[self doPlay:NO];
}
};
[reach startNotifier];
return self;
}
Related
I am creating video with AVAssetExportSession and playing video when it finishes.
But Visual Part not showing instantly but only audio is playing instantly.
Visual part comes after some delay about of 20 - 30 seconds. Here is my code to play video
-(void)playUrl:(NSURL *)vUrl{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]];
_avAsset=nil;
_avPlayerItem=nil;
_avPlayer =nil;
[_avPlayerLayer removeFromSuperlayer];
_avPlayerLayer=nil;
_avAsset=[AVAsset assetWithURL:vUrl];
_avPlayerItem =[[AVPlayerItem alloc]initWithAsset:_avAsset];
_avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem];
[_avPlayer replaceCurrentItemWithPlayerItem:_avPlayerItem];
_avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:_avPlayer];
[_avPlayerLayer setFrame:CGRectMake(0, 0, viewAVPlayer.frame.size.width, viewAVPlayer.frame.size.height)];
[viewAVPlayer.layer addSublayer:_avPlayerLayer];
[_avPlayer seekToTime:kCMTimeZero];
[_avPlayer play];
_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(repeatPlayer:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_avPlayer currentItem]];
}
please let me know if anybody know its answer. this code is working perfectly in iOS 9 but not iOS 10. Thanks in advance.
Try to set automaticallyWaitsToMinimizeStalling property of AVPlayer to NO in order to start playback immediately.
_avPlayer = [[AVPlayer alloc]init]; //WithPlayerItem:_avPlayerItem];
_avPlayer.automaticallyWaitsToMinimizeStalling = NO;
But if sufficient content is not available for playing then player might stall.
Apple documentation: https://developer.apple.com/reference/avfoundation/avplayer/1643482-automaticallywaitstominimizestal.
Hope this helps.
I do next:
First
I add watcher
- (void)attachWatcherBlock {
[self removeWatcherBlock];
if (self.videoPlayer) {
__weak typeof(self) wSelf = self;
self.timeObserver = [self.videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
if (wSelf.playerBlock && wSelf.videoPlayer) {
CGFloat playTime = CMTimeGetSeconds(wSelf.videoPlayer.currentTime);
CGFloat duration = CMTimeGetSeconds(wSelf.videoPlayer.currentItem.duration);
if (playTime > 0.0f) {
[wSelf replaceCoverToVideo];
}
wSelf.playerBlock(wSelf, playTime, duration);
}
}];
}
[self.videoPlayer play];
}
And then if in block playTime equals duration call replay
- (void)replay {
__weak typeof(self) wSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(wSelf) self = wSelf;
if (self.videoPlayer) {
[self.videoPlayer seekToTime:kCMTimeZero];
}
});
}
All this in my UIView subclass called NDVideoPlayerView
Im facing the same problem and my solution is take old code into main thread:
-(void)ExporterManager:(DoCoExporterManager *)manager DidSuccessComplementWithOutputUrl:(NSURL *)outputUrl{
//...
dispatch_async(dispatch_get_main_queue(), ^{
[_playView setContentUrl:outputUrl.path];
});
//...
}
Im using exportAsynchronouslyWithCompletionHandler to process my video.someone thinks that AVVideoCompositionCoreAnimationTool is the cause of the issuehttps://forums.developer.apple.com/thread/62521.
I'm not sure,but i do use it.
Just try it!
Hope this helps!
The AVVideoCompositionCoreAnimationTool when used in AVAssetExportSession interferes with AVPlayer in iOS 10.0 - 10.1.1. iOS 10.2 fixes this bug and your code should work normally.
Hi everyone I am working on an application in which I have a url of video and I have to play a video from that url. I have done this job from this code
- (IBAction)btnPlayVideo:(id)sender
{
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
[self.view addSubview:mPlayerVC.view];
//play movie
MPMoviePlayerController *player = [mPlayerVC moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
player.view.frame = self.view.frame;
[player setFullscreen:YES animated:YES];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
//============Other Methods====================
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
break;
default:
break;
}
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
}
My problem is that when this code run video player open and start loading and it takes too much time to run a video. Can anybody guide me how to run video in fast way from internet?
There's nothing in the code that suggests that lag is anything other than the time it takes to make the request and sufficiently buffer. The most common technique to improve UE is to start loading as early as possible, even before the user requests playback.
If this is possible, the code should be reorganized as follows:
// hang on to the movie player
#property(nonatomic,retain) MPMoviePlayerController *mp;
// call this as soon as its possible to know the user might want to see the video
- (void)primeVideo {
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
MPMoviePlayerController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
// do this also in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
mp.shouldAutoplay = NO;
[mp prepareToPlay];
self.mp = mp;
}
That does as much prep as possible without changing the UI. The rest of your code from the button action is left, tweaked a little...
- (IBAction)btnPlayVideo:(id)sender {
// if there's any way that the user can request playback before
// you've called primeVideo, check for that here. But hopefully you
// can call primeVideo before user even sees the play button
if (!self.mp) [self primeVideo];
self.mp.view.frame = self.view.frame;
[self.mp setFullscreen:YES animated:YES];
[self.view addSubview:self.mp.view];
MPMovieLoadState state = [self.mp loadState];
if (state & MPMovieLoadStatePlaythroughOK) [self.mp play];
else self.mp.shouldAutoplay = YES;
}
I have a UIView that responds to a single tap gesture by playing an audio file. It does this by using the AVPlayer class. It all works well with one exception. If a user is currently listening to an audio file, and they tap the same UIView again, I want the audio file to return to the beginning and start playing again from the start. However, when this occurs, I get the following console output:
An instance 0x10ad89dd0 of class AVPlayer was deallocated while key
value observers were still registered with it. Observation info was
leaked, and may even become mistakenly attached to some other object.
The first three lines in my play method are my attempt to deal with this, but they don't solve anything. The audio does restart, but the controls (time played, sliders, etc) all go crazy. I see a couple of other posts on this, I'm still stuck. Can anyone see what I need to do to clear this problem?
- (void) playAudio : (UITapGestureRecognizer *)recognizer
{
// remove any existing observers to prevent memory leaks
[self.audioPlayer pause];
self.audioPlayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.audioPlayer];
unsigned long buttonPressed = [self.buttonsArray indexOfObject:recognizer.view];
Sessions *session = self.sessionsList[buttonPressed];
self.mediaFile = session.media_file;
self.totalSecondsToPlay = [session.play_seconds integerValue];
[self resetAVControls];
NSString *urlString = [NSString stringWithFormat:#"%#%#", AUDIO_URL, self.mediaFile];
AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
self.audioPlayer = player;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.audioPlayer currentItem]];
[self.audioPlayer addObserver:self forKeyPath:#"status" options:0 context:nil];
self.isPlaying = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(updateProgress)
userInfo:nil
repeats:YES];
}
You've registered KeyValueObservers for self.audioPlayer, but you haven't removed them when you are setting the value to nil. So before doing self.audioPlayer = nil; unsubscribe to KVO by using [self.audioPlayer removeObserver:self forKeyPath:#"status" context:nil]
Check this if you want to know more about KVO
Each time when your set your playerview to nil, before call removeObserver.
Example
[self removeObserverForStatusPlay];
playerView = nil;
And before Init
[self removeObserverForStatusPlay];
playerView = [[AVPlayer alloc] initWithURL:url];
When removeObserverForStatusPlay method is
- (void)removeObserverForStatusPlay
{
#try {
[playerView removeObserver:self forKeyPath:#"status"];
} #catch(id anException) {
NSLog(#"excepcion remove observer == %#. Remove previously or never added observer.",anException);
//do nothing, obviously it wasn't attached because an exception was thrown
}
}
Im using AVQueuePlayer to loop through an array of AVPlayerItems.
The way I'm looping it, I listen to the AVPlayerItemDidPlayToEndTimeNotification and every time its called, I add the current object to the end of the queue.
heres the code:
viewWillAppear
{
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_queuePlayer currentItem]];
[_queuePlayer play];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
AVPlayerItem *fakeNew = [[AVPlayerItem alloc] initWithAsset:p.asset];
if (_queuePlayer.items.count == 1)
{
[p seekToTime:kCMTimeZero];
[_queuePlayer play];
}
else
{
[_queuePlayer insertItem:fakeNew afterItem:[_queuePlayer.items lastObject]];
}
NSLog(#"array of items to play:%lu", (unsigned long)_queuePlayer.items.count);
}
The problem is, that the method is called only for the first video that plays, after that, the method stops getting called, so if for example i have 2 movies in the array, it would play them both+the first one again, any idea why is this happening?
More Info:
also tried to make a new player every time and set it to layer. failed to send the notification more than once just the same
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[self.playList removeObjectAtIndex:0];
[self.playList addObject:p];
AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
_player = newPlayer;
self.AVPlayerLayerView.layer.player = self.player;
[_player play];
}
After a lot of messing around, apparently for whatever reason, the view unregistered as observer every time, I just removed and added observer after every notification:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
AVPlayerItem *fakeNewItem = [[AVPlayerItem alloc] initWithAsset:p.asset];
[self.playList removeObjectAtIndex:0];
[self.playList addObject:fakeNewItem];
AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
_player = newPlayer;
self.AVPlayerLayerView.layer.player = self.player;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
[_player play];
}
For a clean approach to resolve this issue. I approached with the next piece of code instead
The first is you have to add the code necessary to receive a feedback from the AVPlayer when the reproduction time changes.
- (void)addPeriodicTimeObserverForReproductionChanges {
#weakify(self);
[self.player
addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(kBQDefaultTimeIntervalReproductionChanges, NSEC_PER_SEC)
queue:self.eventsQueue
usingBlock:^(CMTime time) {
#strongify(self);
[self dispatchBlockOnMainQueue:^{
if ([self.delegate respondsToSelector:#selector(playerItemController:didChangeReproductionTime:)])
[self.delegate playerItemController:self
didChangeReproductionTime:time];
[self checkForItemPlayToEnd];
}];
}];
}
- (void)checkForItemPlayToEnd
{
CMTime durationScaled = CMTimeConvertScale(self.duration,self.player.currentTime.timescale, kCMTimeRoundingMethod_Default);
if (CMTIME_COMPARE_INLINE(durationScaled, ==, self.player.currentTime)) {
[self playerDidFinishReproducingItem];
}
}
In MonoTouch, we ran into this problem with the Movie Player sample in that it would only play the video once, but would not play it a second time.
I am asking this question to post an answer, since it has been hitting various folks.
MPMoviePlayerController is a singleton underneath the hood. If you have not properly release'd (ObjC) or Dispose()'d (MonoTouch) and you create a second instance, it will either not play, or play audio only.
Additionally if you subscribe to MPMoviePlayerScalingModeDidChangeNotification or MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerContentPreloadDidFinishNotification, be warned that the posted NSNotification takes a reference to the MPMoviePlayerController as well, so if you keep it around, you will have a reference the player.
Although Mono's Garbage Collector will eventually kick-in, this is a case where deterministic termination is wanted (you want the reference gone now, not gone when the GC decides to perform a collection).
This is why you want to call the Dispose () method on the controller, and the Dispose() method on the notification.
For example:
// Deterministic termination, do not wait for the GC
if (moviePlayer != null){
moviePlayer.Dispose ()
moviePlayer = null;
}
If you were listening to notifications, call Dispose in your notification handler at the end, to release the reference that it keeps to your MPMoviePlayerController for example:
var center = NSNotificationCenter.DefaultCenter;
center.AddObserver (
"MPMoviePlayerPlaybackDidFinishNotification"),
(notify) => { Console.WriteLine ("Done!"); notify.Dispose (); });
Can't see your code Nir and I don't have edit privileges so here it is again:
The secret lays in the endPlay with setting the: moviePlayer.initialPlaybackTime = -1; before releasing it. Try it out : :)
-(void)playMovie:(NSString *)urlString{ movieURL = [NSURL URLWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.initialPlaybackTime = 0;
//Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(endPlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieControlMode = MPMovieControlModeDefault;
moviePlayer.backgroundColor = [UIColor blackColor];
[moviePlayer play];
}
-(void)endPlay: (NSNotification*)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.initialPlaybackTime = -1;
[moviePlayer stop];
[moviePlayer release];
}
The secret lays in the endPlay with setting the: moviePlayer.initialPlaybackTime = -1;
before releasing it.
Try it out :
:)
-(void)playMovie:(NSString *)urlString{
movieURL = [NSURL URLWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.initialPlaybackTime = 0;
//Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(endPlay:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieControlMode = MPMovieControlModeDefault;
moviePlayer.backgroundColor = [UIColor blackColor];
[moviePlayer play];
}
-(void)endPlay: (NSNotification*)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.initialPlaybackTime = -1;
[moviePlayer stop];
[moviePlayer release];
}