1

I am using below function to play/pause the music and I am implementing remote controls:

in 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)
}
}

//in info.playlist I have added 'Required background modes and add to idem 0 ap plays audio airplay then below code to play even when iphone is locked: -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.")
    }

in AppDelegate.swift I have this for remote control:

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

when I am hitting play button then app works file on my phone (it plays the sound) but I am getting below info in Xcode error window:

Receiving remote control events 2015-08-31 19:33:42.735 The App[1501:292732] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", "", "", "" ) Will attempt to recover by breaking constraint Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

then when I lock my phone, sound is still playing (which if great) but when I am hitting pause button on remote control screen (when my phone is locked) then app is freezing / stopping and I get below info in Xcode :

received remote pause fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

What am I doing wrong? Please help.

M Kovacs
  • 31
  • 1
  • 7

0 Answers0