D
D
Denis2019-06-24 18:04:18
Swift
Denis, 2019-06-24 18:04:18

How to solve "unrecognized selector sent to instance" error?

The application starts, starts playing video and crashes:

*** 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

The code
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()  
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2019-06-24
@foma24

5d10efdae0912561988174.png

The code
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()
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question