1

Derived from some other posts I was successfully able to get everything right for a music player.

Last step would be to set MPNowPlayingInfoCenter values.

Unfortunately as soon as I do this, the app loses capability to receive remoteControlEvents should i be in any other View Controller than the one that set up the properties. Same goes for putting the App in Background from any other VC than the "Music VC" (It is a tabbed Application and Music is just one Tab)

My AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //here is other stuff for push-notifications etc.

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

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

    return true
}

MusicVC:

class MusicVC:UIViewController, UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource, UICollectionViewDelegate {

    var myPlayer:AVQueuePlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        //tableView and other setup

        self.becomeFirstResponder()

    }

    //MARK: - InfoCenter Methods
    func setMediaCenterInfo() {

        let mpic = MPNowPlayingInfoCenter.defaultCenter()

        var albumArtWork = MPMediaItemArtwork(image: UIImage(named:"testImage"))

        var fullString = self.currentSong.title as String
        var splitArray = fullString.componentsSeparatedByString(" - ")
        var artistName: String = splitArray[0]
        var titleString: String? = splitArray.count > 1 ? splitArray[1] : nil

        if titleString != nil {

        mpic.nowPlayingInfo = 
            [
            MPMediaItemPropertyArtwork:albumArtWork,
            MPMediaItemPropertyTitle:titleString!,
            MPMediaItemPropertyArtist:artistName
            ]
        } else {
            mpic.nowPlayingInfo = 
            [
            MPMediaItemPropertyArtwork:albumArtWork,
            MPMediaItemPropertyTitle:fullString
            ]
        }

    //Does get set correctly and shows in Info Center as well as Lock-Screen

    }

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

    override func remoteControlReceivedWithEvent(event: UIEvent) {

        if event.type == UIEventType.RemoteControl {

            switch event.subtype {

            case .RemoteControlTogglePlayPause:
                println("TOGGLE PLAY PAUSE")
                //self.playToggleTapped(self)

            case .RemoteControlPlay:
                println("ONLY PLAY BUTTON")
                //self.playToggleTapped(self)

            case .RemoteControlPause:
                println("ONLY PAUSE BUTTON")
                //self.playToggleTapped(self)

            case .RemoteControlNextTrack:
                println("next")
                //self.nexButtonTapped(self)

            case .RemoteControlPreviousTrack:
                //self.previousButtonTapped(self)
                println("previous")

            default:
                break
            }

        }

    }

}

Does the FirstResponder change automatically when showing another VC? Grateful for hints to fix this.

longbow
  • 1,593
  • 1
  • 16
  • 39

1 Answers1

0

If you are still interested in finding a way to do this.

You can create a class to setup you AudioPlayer with a singleton. Inside this AudioPlayer Class you would set MPNowPlayingInfoCenter values and UIApplication.sharedApplication().beginReceivingRemoteControlEvents().

Then from the viewController that 'launched' your player and all viewController showing AFTER the MPNowPlayingInfoCenter values would continue to show up in the foreground and background views. If you call a viewController that is 'BEFORE'(view hierarchy) the 'audio launching' viewController, the audio would continue to play(foreground and background) but your data from the MPNowPlayingInfoCenter would not show.

To have it propagate throughout your entire app, you would have to probably post a notification(within your AudioPlayer) to the AppDelegate(NSNotifcation.addObserver..) when the 'audio launching' happens and then set the MPNowPlayingInfoCenter values within a function in the AppDelegate. I'm doing this now, so let me know if you are still looking for a solution. thanks

Jfusion
  • 11
  • 1
  • 1
    I'm having an issue with this. I'm able to get this to work with multiple sources playing except a WKWebView. I've resignedFirstResponder before and after the source begins playing and update the nowPlayingInfo with no luck. Any ideas? – pir800 Jan 25 '16 at 16:08