Задать вопрос
@foma24
QA Engineer

Как решить ошибку «unrecognized selector sent to instance»?

Приложение запускается, начинает играть видео и крашится:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:   
'-[VideoW.MenuScene playerItemDidReachEnd:]: unrecognized selector sent to instance 0x7f92ee407090'  
*** First throw call stack:  
(  
  0   CoreFoundation                      0x0000000102f9bc7b __exceptionPreprocess + 331  
  1   libobjc.A.dylib                     0x000000010211dac5 objc_exception_throw + 48  
  2   CoreFoundation                      0x0000000102fba034 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
  3   CoreFoundation                      0x0000000102fa09c3 ___forwarding___ + 1443  
  4   CoreFoundation                      0x0000000102fa27b8 _CF_forwarding_prep_0 + 120  
  5   CoreFoundation                      0x0000000102eddb9c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12  
  6   CoreFoundation                      0x0000000102edd01f _CFXRegistrationPost + 447  
  7   CoreFoundation                      0x0000000102edcd63 ___CFXNotificationPost_block_invoke + 227  
  8   CoreFoundation                      0x0000000102fc0b62 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1858  
  9   CoreFoundation                      0x0000000102edc6b1 _CFXNotificationPost + 977  
  10  AVFoundation                        0x0000000102b05055 __avplayeritem_fpItemNotificationCallback_block_invoke + 5586  
  11  libdispatch.dylib                   0x0000000106a82d7f _dispatch_call_block_and_release + 12  
  12  libdispatch.dylib                   0x0000000106a83db5 _dispatch_client_callout + 8  
  13  libdispatch.dylib                   0x0000000106a91080 _dispatch_main_queue_callback_4CF + 1540  
  14  CoreFoundation                      0x0000000102f02e59 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9  
  15  CoreFoundation                      0x0000000102efd506 __CFRunLoopRun + 2310  
  16  CoreFoundation                      0x0000000102efc8b2 CFRunLoopRunSpecific + 626  
  17  GraphicsServices                    0x00000001091fa2fe GSEventRunModal + 65  
  18  UIKitCore                           0x000000010ef695f5 UIApplicationMain + 140  
  19  VideoWallpaper                      0x000000010179f9cb main + 75  
  20  libdyld.dylib                       0x0000000106af8771 start + 1  
)  
libc++abi.dylib: terminating with uncaught exception of type NSException


Код
func playVideo(filename: String) {  
        let path = Bundle.main.path(forResource: filename, ofType:"mp4")!  
        player = AVPlayer(url: NSURL(fileURLWithPath: path) as URL)  
         
        playerLayer = AVPlayerLayer(player: player)  
        playerLayer.frame = CGRect(x: 880, y: 270, width: 960, height: 540)  
        scene!.view!.layer.addSublayer(playerLayer)  
         
        player.actionAtItemEnd = .none  
        player.play()  
         
        DispatchQueue.main.async {  
            NotificationCenter.default.addObserver(self,  
                                                   selector: Selector(("playerItemDidReachEnd:")),  
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime ,  
                object: self.player.currentItem)  
        }  
    }  

func playerItemDidReachEnd(notification: NSNotification) {  
        print("repeating...")  
         
        let seconds : Int64 = 0  
        let preferredTimeScale : Int32 = 1  
        let seekTime : CMTime = CMTimeMake(value: seconds, timescale: preferredTimeScale)  
         
        player.seek(to: seekTime)  
        player.play()  
    }
  • Вопрос задан
  • 3953 просмотра
Подписаться 1 Простой Комментировать
Решения вопроса 2
doublench21
@doublench21 Куратор тега Swift
5d10efdae0912561988174.png

Код
func playVideo(filename: String) {
    let path = Bundle.main.path(forResource: filename, ofType:"mp4")!
    player = AVPlayer(url: NSURL(fileURLWithPath: path) as URL)

    playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = CGRect(x: 880, y: 270, width: 960, height: 540)
    scene!.view!.layer.addSublayer(playerLayer)

    player.actionAtItemEnd = .none
    player.play()



    DispatchQueue.main.async {
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(playerItemDidReachEnd),
                                             name: NSNotification.Name.AVPlayerItemDidPlayToEndTime ,
                                             object: self.player.currentItem)
    }
  }

  @objc func playerItemDidReachEnd(notification: NSNotification) {
    print("repeating...")

    let seconds : Int64 = 0
    let preferredTimeScale : Int32 = 1
    let seekTime : CMTime = CMTimeMake(value: seconds, timescale: preferredTimeScale)

    player.seek(to: seekTime)
    player.play()
  }
Ответ написан
briahas
@briahas
ObjC, Swift, Python
У себя делал через блок (чтобы не возится с селекторами):
let nfCenter = NotificationCenter.default
let kPlayToEnd = NSNotification.Name.AVPlayerItemDidPlayToEndTime

endOfPlayObserver = nfCenter.addObserver(forName: kPlayToEnd, 
                                         object: playerItem,
                                         queue: OperationQueue.main) { [unowned self] notif in
            self.delegate?.didReachVideoEnd()
        }


Кстати, крашится то когда? Когда конца достигает?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы