Ios 用户通话后快速启动音乐
我不太确定我是否错过了某个功能或什么,但当用户的电话铃响,或者他们询问siri或任何阻止我的应用程序音频播放的事情时。当用户完成任务后,它不会重新启动我的应用程序播放 我想知道我是否缺少一个功能,或者苹果iOS应用程序是否可以做到这一点 我认为这与:Ios 用户通话后快速启动音乐,ios,swift,background-music,Ios,Swift,Background Music,我不太确定我是否错过了某个功能或什么,但当用户的电话铃响,或者他们询问siri或任何阻止我的应用程序音频播放的事情时。当用户完成任务后,它不会重新启动我的应用程序播放 我想知道我是否缺少一个功能,或者苹果iOS应用程序是否可以做到这一点 我认为这与: func setupRemoteTransportControls() { // Get the shared MPRemoteCommandCenter let commandCenter = MPRemoteCommandCent
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")
}
}
您需要将音频会话设置为。如果不设置此模式,则将使用默认模式 您可以在
didfishLaunching
中设置模式
e、 g
您还需要实现
您需要将音频会话设置为。如果不设置此模式,则将使用默认模式 您可以在
didfishLaunching
中设置模式
e、 g
您还需要实现
您的av会话是如何配置的?我们目前没有AVSession-我只发现这是必需的。不确定这是否会进入我的viewController,但您的av会话是如何配置的?我们目前没有AVSession-我只发现这是必需的。我不确定这是否会进入我的viewController,所以我补充说,我没有看到任何东西告诉我会话是否被中断。所以我补充说,我没有看到任何东西告诉我会话是否被中断。
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
}
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.
}