我是从AVPlayerItem像这样观察键属性的键值:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if context == &PlayerItemObserverContext {
        [... more code...]
        } else if keyPath == #keyPath(AVPlayerItem.tracks) {
            if let playerItem = self.playerItem {
                for track in playerItem.tracks where track.assetTrack.mediaType == AVMediaType.video {
                    // Do something with the track
                    break
                }
            }
        } [... more code...]

但是当跳过快进时,我的应用程序在这里崩溃
for track in playerItem.tracks where track.assetTrack.mediaType == AVMediaType.video {

XCode 9调试器说Thread 1: EXC_BAD_ACCESS (code=1, address=0x0
我可以清楚地看到track.assetTrack为零!
(lldb) po track.assetTrack
 <uninitialized>
(lldb) po track
<AVPlayerItemTrack: 0x1c80083b0, assetTrack = (null)>

这不应该发生,因为assetTrack上的AVPlayerItemTrack的类型是var assetTrack: AVAssetTrack { get }。但是显然这是不正确的。

(所有视频处理均在主线程上完成)

最佳答案

这里的神话是,在对Swift(在本例中为4)进行编程时,您处于安全的环境中。我很难学的是该语言可能很安全,但运行时却不是。

(希望是临时的)修复是要执行以下操作:

for track in playerItem.tracks where track.assetTrack != nil && track.assetTrack.mediaType == AVMediaType.video {

甚至编译器也认为这很愚蠢,因为XCode抱怨:
Comparing non-optional value of type 'AVAssetTrack' to nil always returns true

但这确实可以解决问题,并且该应用程序也不再崩溃!

该代码在上有效,而在iOS10和早期版本上没有修复,但iOS11在此方面似乎有严重的退步。

关于ios - iOS11在AVPlayerItemTrack.assetTrack.mediaType上崩溃(线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46472615/

10-13 09:38