如何使用 AVFoundation 加载多个本地文件

发布于 2024-10-18 08:18:29 字数 1685 浏览 1 评论 0原文

我希望使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-10-25 08:18:29

哎呀,我自己想出来了,看看我对这个问题的回答:MPMoviePlayerController切换电影导致白色闪光

oops i figured it out myself, check out my answer to this question: MPMoviePlayerController switching movies causes white flash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文