如何使用 AVFoundation 加载多个本地文件
我希望使用 AVFoundation 加载和播放电影文件。我遇到的第一个问题是尝试加载多个本地文件。我找到的代码只允许我加载一个。如果我调用下面的方法,它将退出并出现异常:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“无法删除关键路径“currentItem.nonForcedSubtitleDisplayEnabled”的观察者,因为它未注册为观察者。”
感谢您的任何建议
- (IBAction)loadAssetFromFile:(NSString*)fileName withExtension:(NSString*)extension {
NSURL *fileURL = [[NSBundle mainBundle]
URLForResource:fileName withExtension:extension];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
^{
// Completion handler block.
NSError *error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
[playerView setPlayer:player];
}
else {
// Deal with the error appropriately.
NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
}];
}
I wish to use the AVFoundation to load and play movie files. The first problem I have is attempting to load multiple local files. The code I found only allows me to load one. If I call the method below it exits with the exception:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer for the key path "currentItem.nonForcedSubtitleDisplayEnabled" from because it is not registered as an observer.'
Thanks for any advice
- (IBAction)loadAssetFromFile:(NSString*)fileName withExtension:(NSString*)extension {
NSURL *fileURL = [[NSBundle mainBundle]
URLForResource:fileName withExtension:extension];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
^{
// Completion handler block.
NSError *error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
[playerView setPlayer:player];
}
else {
// Deal with the error appropriately.
NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
}];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,我自己想出来了,看看我对这个问题的回答:MPMoviePlayerController切换电影导致白色闪光
oops i figured it out myself, check out my answer to this question: MPMoviePlayerController switching movies causes white flash