아이폰 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()
}
}
'iOS(Swift)' 카테고리의 다른 글
xcode 시뮬레이터 삭제로 용량확보 (0) | 2022.02.26 |
---|---|
xcode 프로젝트 명 바꾸기 shell script (0) | 2020.04.30 |
애플 개발자 계정 가입하기 (기업) (0) | 2020.04.07 |
Firebase functions 에서 firestore 데이터 읽어와서 푸시 노티피케이션 보내기 (FCM 푸시알림보내기) (0) | 2019.06.28 |
iOS 우는 아이 달래기 / 재우기 (0) | 2019.02.16 |
댓글