swift remoteControlReceivedWithEvent给出了致命错误:在解包一个Optional值

时间:2015-08-31 20:39:19

标签: ios xcode swift remote-control

我正在使用以下功能来播放/暂停音乐,我正在实现遥控器:

ViewController.swift中的

static let sharedInstance = ViewController()
@IBOutlet var PausePlay: UIButton!


var BackgroundAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!), error: nil)

func FuncPausePlay ()
{
if (BackgroundAudio.playing == true){
    BackgroundAudio.stop()
    PausePlay.setTitle("Play", forState: UIControlState.Normal)

} else {
    BackgroundAudio.play()
    PausePlay.setTitle("Pause", forState: UIControlState.Normal)
}
}

//在info.playlist中我添加了'必需的背景模式并添加到idem 0 ap播放音频播放然后在代码下方即使在iPhone被锁定时播放:-marcin

    PausePlay.setTitle("Play", forState: UIControlState.Normal)

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)

    // Do any additional setup after loading the view, typically from a nib.
    PausePlay.setTitle("Play", forState: UIControlState.Normal)
    if NSClassFromString("MPNowPlayingInfoCenter") != nil {
        let image:UIImage = UIImage(named: "logo_player_background")!
        let albumArt = MPMediaItemArtwork(image: image)
        var songInfo: NSMutableDictionary = [
            MPMediaItemPropertyTitle: "Ants",
            MPMediaItemPropertyArtist: "The App",
            MPMediaItemPropertyArtwork: albumArt
        ]
        MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as [NSObject : AnyObject]
    }
    if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)) {
        println("Receiving remote control events")
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    } else {
        println("Audio Session error.")
    }
在AppDelegate.swift中的

我有这个用于远程控制:

override func remoteControlReceivedWithEvent(event: UIEvent) {
        if event.type == UIEventType.RemoteControl {
        if event.subtype == UIEventSubtype.RemoteControlPlay {
            println("received remote play")
            ViewController.sharedInstance.FuncPausePlay() // these are producing terrible error
        } else if event.subtype == UIEventSubtype.RemoteControlPause {
            println("received remote pause")
            ViewController.sharedInstance.FuncPausePlay() // these are producing terrible error
        } else if event.subtype == UIEventSubtype.RemoteControlTogglePlayPause {
            println("received toggle")
            ViewController.sharedInstance.BackgroundAudio.stop()
        }
    }
}

当我按下播放按钮然后应用程序在我的手机上工作文件(它播放声音)但我在Xcode错误窗口中获取以下信息:

接收远程控制事件 2015-08-31 19:33:42.735 App [1501:292732]无法同时满足约束条件。     可能至少下列列表中的一个约束是您不想要的约束。试试这个:(1)看看每个约束并试着找出你不期望的东西; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到您不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translatesAutoresizingMaskIntoConstraints) (     “”     “”     “”     “”     “”     “” ) 将尝试通过打破约束来恢复 在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以便在调试器中捕获它。 在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助。

然后当我锁定手机时,声音仍在播放(如果很棒)但是当我在遥控器屏幕上按下暂停按钮时(当我的手机被锁定时),那么应用程序正在冻结/停止,我在Xcode中得到以下信息:

收到远程暂停 致命错误:在展开Optional值时意外发现nil (lldb)

我做错了什么?请帮忙。

0 个答案:

没有答案