본문 바로가기
iOS(Swift)

swift 사운드 음악을 제어센터와 잠금화면에 넣기

by xavi2019 2019. 2. 13.

아이폰 iOS 사운드를 백그라운드로 넣으면서

Controll Center와 Lock Screen에 넣는 방법은 아래와 같이 하면 된다.


사운드를 제어센터와 잠금화면에 넣기



import AVFoundation

import MediaPlayer



func setupRemoteCommandCenter(enable: Bool) {

        

        let remoteCommandCenter = MPRemoteCommandCenter.shared()

        

        if enable {

            

            remoteCommandCenter.pauseCommand.addTarget(self, action: #selector(remoteCommandCenterPauseCommandHandler))

            remoteCommandCenter.playCommand.addTarget(self, action: #selector(remoteCommandCenterPlayCommandHandler))

            remoteCommandCenter.stopCommand.addTarget(self, action: #selector(remoteCommandCenterStopCommandHandler))

            remoteCommandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(remoteCommandCenterPlayPauseCommandHandler))

            

        } else {

            

            remoteCommandCenter.pauseCommand.removeTarget(self, action: #selector(remoteCommandCenterPauseCommandHandler))

            remoteCommandCenter.playCommand.removeTarget(self, action: #selector(remoteCommandCenterPlayCommandHandler))

            remoteCommandCenter.stopCommand.removeTarget(self, action: #selector(remoteCommandCenterStopCommandHandler))

            remoteCommandCenter.togglePlayPauseCommand.removeTarget(self, action: #selector(remoteCommandCenterPlayPauseCommandHandler))

            

        }

        

        remoteCommandCenter.pauseCommand.isEnabled = enable

        remoteCommandCenter.playCommand.isEnabled = enable

        remoteCommandCenter.stopCommand.isEnabled = enable

        remoteCommandCenter.togglePlayPauseCommand.isEnabled = enable

        

}



deinit {

        setupRemoteCommandCenter(enable: false)

    }

    

    @objc func remoteCommandCenterPauseCommandHandler() {

        

        // handle pause

       //player?.pause()

        stopSound()

        

    }

    

    @objc func remoteCommandCenterPlayCommandHandler() {

        

        // handle play

        //player?.play()

        playSound()

    }

    

    @objc func remoteCommandCenterStopCommandHandler() {

        

        // handle stop

        //player?.pause()

        stopSound()

    }

    

    @objc func remoteCommandCenterPlayPauseCommandHandler() {

        

        // handle play pause

        if player?.rate == 0.0 {

            // player?.play()

            playSound()

        } else {

            // player?.pause()

            stopSound()

        }

        

}






댓글