I'm using this below code. And this code is working perfectly, once application goes in background. If app not goes in background and we try to play and pause currently playing song, then play and pause image is not change. Ad every time breakpoint goes in this case UIEventSubtypeRemoteControlPause: case.
albumArt= [[MPMediaItemArtwork alloc] initWithImage: img.image];
NSDictionary *playingNowInfo = #{MPMediaItemPropertyTitle: (self.currentItem.songname==nil) ? (#"") : (self.currentItem.songname),
MPMediaItemPropertyPlaybackDuration: #(self.currentItem.duration),
MPMediaItemPropertyArtist:[MusicSetting getArtistName],
MPMediaItemPropertyAlbumTitle:(self.currentItem.albumname==nil) ? (#"") : (self.currentItem.albumname),
MPNowPlayingInfoPropertyPlaybackRate: #(self.player.rate),
MPNowPlayingInfoPropertyElapsedPlaybackTime: #(CMTimeGetSeconds(self.player.currentItem.currentTime)),
MPMediaItemPropertyArtwork:albumArt,
};
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = playingNowInfo;
#pragma mark - Remote Control
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
// [self.manager remoteControlReceivedWithEvent:receivedEvent];
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self Onclick_Play_Pause:self];
break;
case UIEventSubtypeRemoteControlPause:
[self Onclick_Play_Pause:self];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
//if ([self.manager.player isPlaying])
if(self.manager.player.rate != 0)
{
[self.manager.player pause];
}
else {
[self.manager.player play];
}
break;
case UIEventSubtypeRemoteControlNextTrack:
[self Onclick_next:self];
NSLog(#"Next song play");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self Onclick_prev:self];
NSLog(#"Prev song play");
break;
default:
break;
}
}
}
- (IBAction)Onclick_Play_Pause:(id)sender {
(AppObj).playerview_height=playviewHeight;
if(self.manager.player.rate != 0)
{
[MusicSetting set_SongStatus:#"Pause"];
[self.Play_PauseBtn setImage:[UIImage imageNamed:#"pauseImg"] forState:UIControlStateNormal];
[self.manager pause];
}
else {
[MusicSetting set_SongStatus:#"Play"];
[MusicSetting set_isMusicPlay:#"Playing"];
[self.Play_PauseBtn setImage:[UIImage imageNamed:#"playImg"] forState:UIControlStateNormal];
[self.manager play];
}
}
Thanks!
- (IBAction)Onclick_Play_Pause:(id)sender {
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];
(AppObj).playerview_height=playviewHeight;
if(self.manager.player.rate != 0)
{
[MusicSetting set_SongStatus:#"Pause"];
[self.Play_PauseBtn setImage:[UIImage imageNamed:#"pauseImg"] forState:UIControlStateNormal];
[self.manager pause];
//set playback rate
[playingInfo setObject:[NSNumber numberWithFloat:0] forKey:MPNowPlayingInfoPropertyPlaybackRate];
}
else
{
[MusicSetting set_SongStatus:#"Play"];
[MusicSetting set_isMusicPlay:#"Playing"];
[self.Play_PauseBtn setImage:[UIImage imageNamed:#"playImg"] forState:UIControlStateNormal];
[self.manager play];
//set playback rate
[playingInfo setObject:[NSNumber numberWithFloat:1] forKey:MPNowPlayingInfoPropertyPlaybackRate];
}
center.nowPlayingInfo = playingInfo;
}
Hi Please check have added the following in info.plist
You have following code
case UIEventSubtypeRemoteControlPlay:
[self Onclick_Play_Pause:self];
break;
case UIEventSubtypeRemoteControlPause:
[self Onclick_Play_Pause:self];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
//if ([self.manager.player isPlaying])
if(self.manager.player.rate != 0)
{
[self.manager.player pause];
}
else {
[self.manager.player play];
}
break;
In the first two cases you called your method but not in the third one can you share the definition of your method?
Related
If the app is playing the audio and phone screen is locked then control screen is shown as below. I am not able to take any action on avplayer
In my appdelegate I implemented:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];
[[rcc skipForwardCommand] setEnabled:NO];
[[rcc skipBackwardCommand] setEnabled:NO];
[[rcc nextTrackCommand] setEnabled:NO];
[[rcc previousTrackCommand] setEnabled:NO];
[[rcc skipForwardCommand] setEnabled:NO];
[[rcc skipBackwardCommand] setEnabled:NO];
rcc.playCommand.enabled = YES;
rcc.pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:#selector(play)];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:#selector(pause)];
}
- (void) play {
[[MyVideoController instance] play];
}
- (void) pause {
[[MyVideoController instance] pause];
}
class MyVideoController consists of:
- (void) pause {
[self.avPlayer pause];
}
- (void) play {
[self.avPlayer play];
}
Even though these methods are triggered (added breakpoints to check), no action on avplayer is taken. No matter what, avplayer doesn't pause.
Is there any way to pause the avplayer?
EDIT 1:
Adding the complete code
In my AppDelegate:
- (void) remoteControlReceivedWithEvent: (UIEvent *) event {
[[ZVideoPlayerController instance] eventReceived:event];
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause: {
break;
}
case UIEventSubtypeRemoteControlPlay: {
[[ZVideoPlayerController instance] play];
break;
}
case UIEventSubtypeRemoteControlPause: {
[[ZVideoPlayerController instance] pause];
break;
}
default:
break;
}
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}
I AM RECEIVING EVENTS BUT THE AUDIO DOESN'T PAUSE UPON CALLING PAUSE METHOD ON AVPLAYER.
EDIT 2:
instance declaration in PlayerController class
+ (instancetype)instance {
static id instance = nil;
if (instance == nil)
{
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^(void) {
NSAssert(instance == nil, #"Singleton instance is already allocated.");
instance = [[super allocWithZone:NULL] init];
});
}
return instance;
}
initialising AVPlayer
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *activationError = nil;
BOOL success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:#"Audio_Thumbnail_Play"]];
[songInfo setObject:title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:#"100" forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
self.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
I found a solution to the problem. As I was getting nil value of avPlayer, I used my PageViewController class to get the instance of PlayerController. Then I used the instance of this playerController to play and pause my avplayer because this instance holds the reference to avPlayer.
- (PlayerController *)getVideoController {
NSArray *controllers = [UtiliyClass getNavigationController].viewControllers;
PageViewController *pageController = nil;
for (UIViewController *cont in controllers) {
if ([cont isKindOfClass:[PageViewController class]]) {
pageController = (PageViewController *)cont;
break;
}
}
if (pageController == nil) {
return nil;
}
NSArray *objectsController =pageController.pageController.viewControllers;
PlayerController *videoPlayerController = nil;
for (UIViewController *item in objectsController) {
if ([item isKindOfClass:[PlayerController class]]) {
videoPlayerController = (PlayerController *)item;
break;
}
}
return videoPlayerController;
}
- (void) pause {
PlayerController *controller = [self getVideoController];
[controller.avPlayer pause];
}
- (void) play {
PlayerController *controller = [self getVideoController];
[controller.avPlayer play];
}
You need to register for remote notification to update player state when application is locked.For that follow following:
Add this in your AppDelegate , Ideally in applicationDidEnterBackground:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
And this in applicationDidBecomeActive:
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
Recieve remote notifcations by adding this in AppDelagate. This will listen all actions when phone is locked.
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl){
// Call method of your player where you want to make change (Pause , Paly),
// I am calling a shared view for example, Its up to your logic how you want to deal it
[[AudioPlayerView sharedPlayerView] remoteControlReceivedWithEvent:event];
}
}
And in that get your desired event and update state accordingly
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl){
switch (event.subtype){
case UIEventSubtypeRemoteControlPlay:
[[MyVideoController instance] play];
break;
case UIEventSubtypeRemoteControlPause:
[[MyVideoController instance] pause];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
// Check if state is playing , call pause else call play
break;
}
default:
break;
}
}
}
In iOS 7.1 and later, use the shared MPRemoteCommandCenter object to register for remote control events. You do not need to call this method when using the shared command center object.
This method starts the delivery of remote control events using the responder chain. Remote-control events originate as commands issued by headsets and external accessories that are intended to control multimedia presented by an app. To stop the reception of remote-control events, you must call endReceivingRemoteControlEvents().
Add this following code for in didfinishlunching for init audio season and get remote control event :
// Initialize the AVAudioSession here.
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
// Handle the error here.
NSLog(#"Audio Session error %#, %#", myErr, [myErr userInfo]);
}
else{
// Since there were no errors initializing the session, we'll allow begin receiving remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
for reciving commadn use this code :
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
case UIEventSubtypeRemoteControlPlay:
[[MyVideoController instance] play];
break;
case UIEventSubtypeRemoteControlPause:
[[MyVideoController instance] pause];
break;
default:
break;
}
}
}
I want to display playback(seekbar, next button & prev button) and Song name , Artist name ,Image etc. on lock screen if song is playing and user locked his iPhone. But I got one issue. Issue is the MPNowPlayingInfoCenter info value display once for 1-2 sec after that all value will disappeared automatically and after that it is display default value.
This below screenshot1 is perfect but after 1-2 this all value disappeared see the screenshot2 all values are disappeared
-(void)SetLockScreenView :(NSString *)SongName ArtistName:(NSString *)ArtistName AlbumTitle:(NSString *)AlbumTitle DisplayImg:(NSString *)displayImg
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Class playingInfoCenter = NSClassFromString(#"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
if(displayImg.length>0 && displayImg!=nil)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:displayImg]];
dispatch_async(dispatch_get_main_queue(), ^{
if(data!=nil)
{
UIImage *image = [UIImage imageWithData:data];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: image];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
}
else
{
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:#"artistDefaultImg"]];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
}
[songInfo setObject:SongName forKey:MPMediaItemPropertyTitle];
[songInfo setObject:ArtistName forKey:MPMediaItemPropertyArtist];
[songInfo setObject:AlbumTitle forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:[NSNumber numberWithFloat:self.manager.currentItem.duration] forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:[NSNumber numberWithInt:0] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
NSLog(#"songInfo :%#",songInfo);
});
});
}
}
}
#pragma mark - Remote Control
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.manager.player play];
break;
case UIEventSubtypeRemoteControlPause:
[self.manager.player pause];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
if ([self.manager.player isPlaying]) {
[self.manager.player pause];
}
else {
[self.manager.player play];
}
break;
case UIEventSubtypeRemoteControlNextTrack:
[self Onclick_next:self];
NSLog(#"Next song play");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self Onclick_prev:self];
NSLog(#"Prev song play");
break;
default:
break;
}
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
I created a sample video app using AVPlayerViewController. It is working fine. But the problems are after finishing the video the AVPlayerViewController view is not removing. Other problem is set the constraints for AVPlayerViewController for portrait default size in landscape mode full screen. I am new for constraints. Please download the project and run the project logger shows the constraint problems.
Source code project : http://www.filedropper.com/avplayerdemos
I see two problem with your code:
asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^{
dispatch_async(
dispatch_get_main_queue(), ^{
if (!asset.playable) {
return;
} else {
[self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
}
if (videoPlayerItem) {
[videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object: videoPlayerItem];
}
});
}];
this code above you add observer and remove after. so should change two:
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^{
dispatch_async(
dispatch_get_main_queue(), ^{
if (videoPlayerItem) {
[videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object: videoPlayerItem];
}
if (!asset.playable) {
return;
} else {
[self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
}
});
}];
Problem 2:
- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys {
for (NSString *thisKey in requestedKeys) {
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
switch (keyStatus) {
case AVKeyValueStatusUnknown:
NSLog(#"%# AVKeyValueStatusUnknown", thisKey);
break;
case AVKeyValueStatusFailed:
NSLog(#"Error! PlayAsset failed.\nAVKey : %#.\nError: %#", thisKey, error);
return;
break;
case AVKeyValueStatusLoading:
NSLog(#"%# AVKeyValueStatusLoading", thisKey);
break;
case AVKeyValueStatusCancelled:
NSLog(#"%# AVKeyValueStatusCancelled", thisKey);
break;
case AVKeyValueStatusLoaded: {
videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
[videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];
videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];
/**
* Creating the videoAdplayer through passing the avplayer object
*/
[self createVideoPlayer: videoPlayer];
if ([thisKey isEqualToString: #"duration"]) {
} else if ([thisKey isEqualToString: #"tracks"]) {
NSLog(#"\n\n asset.tracks : %# \n\n", asset.tracks);
} else if ([thisKey isEqualToString: #"metadata"]) {
NSLog(#"\n\n assetMetadata : %# \n\n", asset.metadata);
}
}
break;
default:
break;
}
}
if (!asset.playable) {
return;
}
}
In this loop just check eveything load and return if have failed. In this case have 2 key, you code like this will add two childviewcontroller and it will play two item player. So change code to it:
- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys {
for (NSString *thisKey in requestedKeys) {
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
switch (keyStatus) {
case AVKeyValueStatusUnknown:
NSLog(#"%# AVKeyValueStatusUnknown", thisKey);
break;
case AVKeyValueStatusFailed:
NSLog(#"Error! PlayAsset failed.\nAVKey : %#.\nError: %#", thisKey, error);
return;
break;
case AVKeyValueStatusLoading:
NSLog(#"%# AVKeyValueStatusLoading", thisKey);
break;
case AVKeyValueStatusCancelled:
NSLog(#"%# AVKeyValueStatusCancelled", thisKey);
break;
case AVKeyValueStatusLoaded: {
}
break;
default:
break;
}
}
videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
[videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];
videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];
/**
* Creating the videoAdplayer through passing the avplayer object
*/
[self createVideoPlayer: videoPlayer];
if (!asset.playable) {
return;
}
}
I pretty sure that with your demo. Change like this, it will work ok.
I'm trying to use Key Value Observation to detect a change in the torchMode variable for an AVCaptureDevice. I'm having an issue where the switch statement doesn't recognize the value (which is an enum). Any idea whats going on? I know that the program gets to the line with the switch, then immediately skips all the cases. Here's the relevant code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == CapturingStillImageContext)
{
BOOL isCapturingStillImage = [change[NSKeyValueChangeNewKey] boolValue];
if (isCapturingStillImage)
{
[self runStillImageCaptureAnimation];
}
}
else if (context == RecordingContext)
{
BOOL isRecording = [change[NSKeyValueChangeNewKey] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (isRecording)
{
[[self record] setTitle:NSLocalizedString(#"-", #"Recording button stop title") forState:UIControlStateNormal];
[[self record] setEnabled:YES];
}
else
{
[[self record] setTitle:NSLocalizedString(#"+", #"Recording button record title") forState:UIControlStateNormal];
[[self record] setEnabled:YES];
}
});
}
else if (context == SessionRunningAndDeviceAuthorizedContext)
{
BOOL isRunning = [change[NSKeyValueChangeNewKey] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (isRunning)
[[self record] setEnabled:YES];
else
[[self record] setEnabled:NO];
});
}
else if (context == TorchContext) {
id torchValue = [change objectForKey: NSKeyValueChangeNewKey];
NSString* title;
switch((AVCaptureTorchMode)torchValue) {
case AVCaptureTorchModeAuto: title = #"Auto"; break;
case AVCaptureTorchModeOff: title = #"Off"; break;
case AVCaptureTorchModeOn: title = #"On"; break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.torch setTitle:title forState:UIControlStateNormal];
//NSLog(self.torch);
});
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Code for adding observer for key value:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:NO];
dispatch_async([self sessionQueue], ^{
[self addObserver:self forKeyPath:#"sessionRunningAndDeviceAuthorized" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:SessionRunningAndDeviceAuthorizedContext];
[self addObserver:self forKeyPath:#"stillImageOutput.capturingStillImage" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:CapturingStillImageContext];
[self addObserver:self forKeyPath:#"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext];
[self addObserver:self forKeyPath:#"videoDeviceInput.device.torchMode" options:(NSKeyValueObservingOptionNew) context:TorchContext];
...
}
I solved the issue. I'm not sure if this is the most elegant solution, but I just casted the value to an NSNumber and got the integer value, then put it in the switch. Here's the code:
int torchValue = ((NSNumber*)[change objectForKey: NSKeyValueChangeNewKey]).integerValue;
NSString* title;
switch(torchValue) {
case AVCaptureTorchModeAuto: title = #"Auto"; break;
case AVCaptureTorchModeOff: title = #"Off"; break;
case AVCaptureTorchModeOn: title = #"On"; break;
}
I am using this code to start playing local video chunks referenced to form a play list.
The very same code works on one project, but not on another.
On the project I am working on right now, I can see how the first chunk gets loaded, and the first frame also show up. But the AVPlayer never start playing because it never gets the AVPlayerStatusReadyToPlay notification:
- (void)loadAssetAsync
{
NSLog(#"loadAssetAsync for URL: %#", videoURL);
/**
* Create an asset for inspection of a resource referenced by a given URL.
* Load the values for the asset keys "tracks", "playable".
*/
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
NSArray *requestedKeys = [NSArray arrayWithObjects:kTracksKey, kPlayableKey, nil];
// Tells the asset to load the values of any of the specified keys that are not already loaded.
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
^{
dispatch_async( dispatch_get_main_queue(),
^{
// IMPORTANT: Must dispatch to main queue in order to operate on the AVPlayer and AVPlayerItem.
[self prepareToPlayAsset:asset withKeys:requestedKeys];
});
}];
}
/**
* Invoked at the completion of the loading of the values for all keys on the asset that required.
*/
- (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys
{
//assert([NSThread isMainThread]);
// Make sure that the value of each key has loaded successfully.
for (NSString *thisKey in requestedKeys)
{
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
if (keyStatus == AVKeyValueStatusFailed)
{
BVLogWarn(#"%#: %#", THIS_FILE, error.localizedDescription);
[self handleErrorForProxy:error];
[self assetFailedToPrepareForPlayback];
return;
}
}
if (!asset.playable)
{
BVLogWarn(#"%#: Item cannot be played", THIS_FILE);
[self handleErrorForProxy:nil];
[self assetFailedToPrepareForPlayback];
return;
}
// Create a new instance of AVPlayerItem from the now successfully loaded AVAsset.
playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
// Observe the player item "status" key to determine when it is ready to play.
[playerItem addObserver:self
forKeyPath:kStatusKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:BVPlayerItemStatusObserverContext];
[playerItem addObserver:self
forKeyPath:kBufferEmpty
options:NSKeyValueObservingOptionNew
context:BVPLayerBufferEmptyObserverContext];
[playerItem addObserver:self
forKeyPath:kLikelyToKeepUp
options:NSKeyValueObservingOptionNew
context:BVPlayerLikelyToKeepUpObserverContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
// Get a new AVPlayer initialized to play the specified player item.
player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
// Do nothing if the item has finished playing
[player setActionAtItemEnd:AVPlayerActionAtItemEndNone];
/* Observe the AVPlayer "currentItem" property to find out when any
AVPlayer replaceCurrentItemWithPlayerItem: replacement will/did
occur.*/
[player addObserver:self
forKeyPath:kCurrentItemKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:BVCurrentItemObserverContext];
// Observe the AVPlayer "rate" property to update the scrubber control.
[player addObserver:self
forKeyPath:kRateKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:BVRateObserverContext];
[player replaceCurrentItemWithPlayerItem:playerItem];
}
- (void)observeValueForKeyPath:(NSString*) keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
// AVPlayerItem "status" property value observer.
if (context == BVPlayerItemStatusObserverContext)
{
AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
switch (status)
{
case AVPlayerStatusUnknown:
{
[self removeTimeObserver];
[self syncTimeScrubber];
[timeControl setEnabled:NO];
[playButton setEnabled:NO];
[fullscreenButton setEnabled:NO];
[loadingIndicator startAnimating];
}
break;
case AVPlayerStatusReadyToPlay:
{
if (firstPlayback|becomeActive)
{
[timeControl setEnabled:YES];
[playButton setEnabled:YES];
[fullscreenButton setEnabled:YES];
[upperControls setHidden:NO];
[lowerControls setHidden:NO];
[loadingIndicator stopAnimating];
if (firstPlayback) {
[playbackView setNeedsDisplay];
}
if (self.shouldAutoplay)
[player play];
if (firstPlayback) {
timeRemaining.text = [NSString stringWithFormat:#"-%#", timeStringForSeconds(CMTimeGetSeconds(playerItem.duration) )];
}
firstPlayback = NO;
controlsHidden = NO;
if (!isSeeking)
[self startHideControlsTimer];
}
if (becomeActive) {
dispatch_async(dispatch_get_main_queue(), ^{
[player seekToTime:CMTimeMakeWithSeconds(lastTimeStop, NSEC_PER_SEC)
toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero
completionHandler:^(BOOL finished) {
if (finished && rateToRestoreAfterScrubbing)
{
[player setRate:rateToRestoreAfterScrubbing];
rateToRestoreAfterScrubbing = 0.f;
}
[self addTimeObserver];
[playbackView setPlayer:player];
becomeActive = NO;
}];
});
}else{
[self addTimeObserver];
}
}
break;
case AVPlayerStatusFailed:
{
AVPlayerItem *thePlayerItem = (AVPlayerItem *)object;
BVLogWarn(#"%#: %#", THIS_FILE, thePlayerItem.error.localizedDescription);
[self handleErrorForProxy:thePlayerItem.error];
[self assetFailedToPrepareForPlayback];
}
break;
}
}
// AVPlayer "rate" property value observer.
else if (context == BVRateObserverContext)
{
[self updatePlayPauseButton];
}
// AVPlayer "currentItem" buffer is empty observer
else if (context == BVPLayerBufferEmptyObserverContext)
{
[loadingIndicator startAnimating];
}
// AVPlayer "currentItem" is likely to keep up observer
else if (context == BVPlayerLikelyToKeepUpObserverContext)
{
[loadingIndicator stopAnimating];
}
// AVPlayer "currentItem" property observer.
else if (context == BVCurrentItemObserverContext)
{
AVPlayerItem *newPlayerItem = [change objectForKey:NSKeyValueChangeNewKey];
// New player item null?
if (newPlayerItem == (id)[NSNull null])
{
[playButton setEnabled:NO];
[timeControl setEnabled:NO];
} else // Replacement of player currentItem has occurred
{
if (!becomeActive) {
[playbackView setPlayer:player];
}else{
}
[playbackView setVideoFillMode:[self scalingMode]];
[self updatePlayPauseButton];
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
return;
}
Did you try to put some log before your if ? Maybe the notification is working but it's stucked because your if?
case AVPlayerStatusReadyToPlay:
{
NSLog(#"NOTIFICATION TEST PASSED");
if (firstPlayback|becomeActive) {}
}