I’m using AVPlayer
to implement a custom video player in an iOS application. To play video from the network I allocate a player:
[[AVPlayer alloc] initWithURL:_URL];
Create an asset:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.URL
options:@{AVURLAssetPreferPreciseDurationAndTimingKey : @(YES)}];
load the playable
key asynchronously:
NSArray *keys = @[@"playable"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
for (NSString *key in keys) {
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:key error:&error];
if (status == AVKeyValueStatusFailed) {
NSLog(@"Failed to load asset key %@ error %@", key, [error localizedDescription]);
[self fail];
return;
}
}
if (asset.isPlayable) {
[self.player replaceCurrentItemWithPlayerItem:self.playerItem];
}
else {
[self fail];
}
});
}];
The problem is that when the device has no internet connection or high packet loss, the completionHandler
is never called (even after waiting for minutes) and so I have no idea when to show a message to the user that loading the video failed.
My questions are:
loadValuesAsynchronouslyForKeys:
when Reachability
believes that the network isn't reachable.AVPlayer
and network failures?An object that models the timing and presentation state of an asset during playback.
AVPlayer does not have a method named stop . You can pause or set rate to 0.0. Show activity on this post. I usually seekToTime 0.0, then pause.
Overview. A player is a controller object that manages the playback and timing of a media asset. Use an instance of AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.
You should be observing (KVO) features of the AVPlayerItem that reveal how the buffer is doing, such as playbackBufferEmpty
etc.
I've integrated AVAsset before, but haven't run into this specific issue. Nonetheless, here's what I would do:
statusOfValueForKey:error:
.AVKeyValueStatusLoaded
or error is not nil, call [asset cancelLoading]
and display an error message to the user or whatever cleanup you might need to do.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With