iOS swift remoteControlReceivedWithEvent

时间:2015-09-02 20:39:58

标签: ios swift

这在我的AppDelegate.swift中无效:

override func remoteControlReceivedWithEvent(event: UIEvent) {
    if event.type == UIEventType.RemoteControl {
        if event.subtype == UIEventSubtype.RemoteControlPlay {
            println("received remote play")
            ViewController.sharedInstance.play() 
        } else if event.subtype == UIEventSubtype.RemoteControlPause {
            println("received remote pause")
            ViewController.sharedInstance.pause2() 
        } else if event.subtype == UIEventSubtype.RemoteControlTogglePlayPause {
            println("received toggle")
            ViewController.sharedInstance.toggle()
        }
    }
}

从上面的代码中这部分不起作用:

ViewController.sharedInstance.pause2() 

虽然我可以看到'收到了远程暂停'正在印刷

我的ViewControl.swift:

class ViewController: UIViewController {

override func canBecomeFirstResponder() -> Bool {
    return true
}

static let sharedInstance = ViewController()

func pause2() {

        BackgroundAudio2.pause()

        BackgroundAudio3.pause()

        BackgroundAudio4.pause()

        BackgroundAudio5.pause()

        BackgroundAudio6.pause()

        BackgroundAudio7.pause()

        BackgroundAudio8.pause()

        BackgroundAudio9.pause()

    isPlaying = false
}

除了remoteControlReceivedWithEvent

中的事件之外,应用程序中的所有内容都能正常工作

1 个答案:

答案 0 :(得分:0)

看起来您的第一个响应者是ViewController,但您希望代码可以在AppDelegate中运行。我认为这应该是一种观点。也要成为第一响应者你应该做self.becomeFirstResponder()