swift 在用户 phone 通话后开始播放音乐
swift start music after user phone call
我不太确定我是否缺少某个功能或其他东西,但是当用户 phone 响铃或者他们询问 siri 或任何阻止我的应用程序音频播放的东西时。当用户完成他们的任务时,它不会重新启动我的应用程序播放。
我想知道我是否缺少某个功能,或者 Apple iOS 应用程序不能执行此操作吗?
我认为这与以下内容有关:
func setupRemoteTransportControls() {
// Get the shared MPRemoteCommandCenter
let commandCenter = MPRemoteCommandCenter.shared()
// Add handler for Play Command
commandCenter.playCommand.addTarget { [unowned self] event in
if self.player?.rate == 0.0 {
self.player?.play()
return .success
}
return .commandFailed
}
// Add handler for Pause Command
commandCenter.pauseCommand.addTarget { [unowned self] event in
if self.player?.rate == 1.0 {
self.player?.pause()
return .success
}
return .commandFailed
}
// self.nowplaying(artist: "Anna", song: "test")
}
我发现我需要添加这部分,但我该如何调用它?
func handleInterruption(notification: Notification) {
guard let userInfo = notification.userInfo,
let interruptionTypeRawValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruptionType = AVAudioSession.InterruptionType(rawValue: interruptionTypeRawValue) else {
return
}
switch interruptionType {
case .began:
print("interruption began")
case .ended:
print("interruption ended")
default:
print("UNKNOWN")
}
}
您需要将音频会话设置为 AVAudioSessionCategoryPlayback
. If you don't set this mode, you will have the default mode AVAudioSessionCategorySoloAmbient
。
您可以在didFinishLaunching
中设置模式。
例如
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Get the singleton instance.
let audioSession = AVAudioSession.sharedInstance()
do {
// Set the audio session category, mode, and options.
try audioSession.setCategory(.playback, mode: .default, options: [])
} catch {
print("Failed to set audio session category.")
}
// Other post-launch configuration.
return true
}
您还需要实施 interruption observation
func setupNotifications() {
// Get the default notification center instance.
let nc = NotificationCenter.default
nc.addObserver(self,
selector: #selector(handleInterruption),
name: AVAudioSession.interruptionNotification,
object: nil)
}
@objc func handleInterruption(notification: Notification) {
// To be implemented.
}
我不太确定我是否缺少某个功能或其他东西,但是当用户 phone 响铃或者他们询问 siri 或任何阻止我的应用程序音频播放的东西时。当用户完成他们的任务时,它不会重新启动我的应用程序播放。
我想知道我是否缺少某个功能,或者 Apple iOS 应用程序不能执行此操作吗?
我认为这与以下内容有关:
func setupRemoteTransportControls() {
// Get the shared MPRemoteCommandCenter
let commandCenter = MPRemoteCommandCenter.shared()
// Add handler for Play Command
commandCenter.playCommand.addTarget { [unowned self] event in
if self.player?.rate == 0.0 {
self.player?.play()
return .success
}
return .commandFailed
}
// Add handler for Pause Command
commandCenter.pauseCommand.addTarget { [unowned self] event in
if self.player?.rate == 1.0 {
self.player?.pause()
return .success
}
return .commandFailed
}
// self.nowplaying(artist: "Anna", song: "test")
}
我发现我需要添加这部分,但我该如何调用它?
func handleInterruption(notification: Notification) {
guard let userInfo = notification.userInfo,
let interruptionTypeRawValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruptionType = AVAudioSession.InterruptionType(rawValue: interruptionTypeRawValue) else {
return
}
switch interruptionType {
case .began:
print("interruption began")
case .ended:
print("interruption ended")
default:
print("UNKNOWN")
}
}
您需要将音频会话设置为 AVAudioSessionCategoryPlayback
. If you don't set this mode, you will have the default mode AVAudioSessionCategorySoloAmbient
。
您可以在didFinishLaunching
中设置模式。
例如
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Get the singleton instance.
let audioSession = AVAudioSession.sharedInstance()
do {
// Set the audio session category, mode, and options.
try audioSession.setCategory(.playback, mode: .default, options: [])
} catch {
print("Failed to set audio session category.")
}
// Other post-launch configuration.
return true
}
您还需要实施 interruption observation
func setupNotifications() {
// Get the default notification center instance.
let nc = NotificationCenter.default
nc.addObserver(self,
selector: #selector(handleInterruption),
name: AVAudioSession.interruptionNotification,
object: nil)
}
@objc func handleInterruption(notification: Notification) {
// To be implemented.
}