|
|
@ -52,10 +52,24 @@ public class MusicService: NSObject { |
|
|
// MARK: - 音频会话设置 |
|
|
// MARK: - 音频会话设置 |
|
|
private func setupAudioSession() { |
|
|
private func setupAudioSession() { |
|
|
do { |
|
|
do { |
|
|
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.allowAirPlay, .allowBluetooth]) |
|
|
// 设置音频会话类别,添加 mixWithOthers 选项以确保通知正常显示 |
|
|
|
|
|
try AVAudioSession.sharedInstance().setCategory( |
|
|
|
|
|
.playback, |
|
|
|
|
|
mode: .default, |
|
|
|
|
|
options: [.allowAirPlay, .allowBluetooth, .mixWithOthers] |
|
|
|
|
|
) |
|
|
try AVAudioSession.sharedInstance().setActive(true) |
|
|
try AVAudioSession.sharedInstance().setActive(true) |
|
|
|
|
|
print("🎵 [MusicService] ✅ 音频会话设置成功") |
|
|
} catch { |
|
|
} catch { |
|
|
print("设置音频会话失败: \(error)") |
|
|
print("🎵 [MusicService] ❌ 设置音频会话失败: \(error)") |
|
|
|
|
|
// 尝试备用配置 |
|
|
|
|
|
do { |
|
|
|
|
|
try AVAudioSession.sharedInstance().setCategory(.playback) |
|
|
|
|
|
try AVAudioSession.sharedInstance().setActive(true) |
|
|
|
|
|
print("🎵 [MusicService] ✅ 使用备用音频会话配置") |
|
|
|
|
|
} catch { |
|
|
|
|
|
print("🎵 [MusicService] ❌ 备用音频会话配置也失败: \(error)") |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -570,30 +584,51 @@ public class MusicService: NSObject { |
|
|
private func setupRemoteCommandCenter() { |
|
|
private func setupRemoteCommandCenter() { |
|
|
let commandCenter = MPRemoteCommandCenter.shared() |
|
|
let commandCenter = MPRemoteCommandCenter.shared() |
|
|
|
|
|
|
|
|
commandCenter.playCommand.addTarget { [unowned self] _ in |
|
|
// 清除之前的目标,避免重复添加 |
|
|
self.resume() |
|
|
commandCenter.playCommand.removeTarget(nil) |
|
|
|
|
|
commandCenter.pauseCommand.removeTarget(nil) |
|
|
|
|
|
commandCenter.nextTrackCommand.removeTarget(nil) |
|
|
|
|
|
commandCenter.previousTrackCommand.removeTarget(nil) |
|
|
|
|
|
commandCenter.stopCommand.removeTarget(nil) |
|
|
|
|
|
|
|
|
|
|
|
// 启用命令 |
|
|
|
|
|
commandCenter.playCommand.isEnabled = true |
|
|
|
|
|
commandCenter.pauseCommand.isEnabled = true |
|
|
|
|
|
commandCenter.nextTrackCommand.isEnabled = true |
|
|
|
|
|
commandCenter.previousTrackCommand.isEnabled = true |
|
|
|
|
|
commandCenter.stopCommand.isEnabled = true |
|
|
|
|
|
|
|
|
|
|
|
commandCenter.playCommand.addTarget { [weak self] _ in |
|
|
|
|
|
print("🎵 [RemoteCommand] 播放命令触发") |
|
|
|
|
|
self?.resume() |
|
|
return .success |
|
|
return .success |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandCenter.pauseCommand.addTarget { [unowned self] _ in |
|
|
commandCenter.pauseCommand.addTarget { [weak self] _ in |
|
|
self.pause() |
|
|
print("🎵 [RemoteCommand] 暂停命令触发") |
|
|
|
|
|
self?.pause() |
|
|
return .success |
|
|
return .success |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandCenter.nextTrackCommand.addTarget { [unowned self] _ in |
|
|
commandCenter.nextTrackCommand.addTarget { [weak self] _ in |
|
|
self.next() |
|
|
print("🎵 [RemoteCommand] 下一首命令触发") |
|
|
|
|
|
self?.next() |
|
|
return .success |
|
|
return .success |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandCenter.previousTrackCommand.addTarget { [unowned self] _ in |
|
|
commandCenter.previousTrackCommand.addTarget { [weak self] _ in |
|
|
self.previous() |
|
|
print("🎵 [RemoteCommand] 上一首命令触发") |
|
|
|
|
|
self?.previous() |
|
|
return .success |
|
|
return .success |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandCenter.stopCommand.addTarget { [unowned self] _ in |
|
|
commandCenter.stopCommand.addTarget { [weak self] _ in |
|
|
self.stop() |
|
|
print("🎵 [RemoteCommand] 停止命令触发") |
|
|
|
|
|
self?.stop() |
|
|
return .success |
|
|
return .success |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
print("🎵 [MusicService] ✅ 远程控制中心设置完成") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func updateNowPlayingInfo(song: [String: Any]) { |
|
|
private func updateNowPlayingInfo(song: [String: Any]) { |
|
|
@ -641,7 +676,11 @@ public class MusicService: NSObject { |
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Double(currentPosition) / 1000.0 |
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Double(currentPosition) / 1000.0 |
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0 |
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0 |
|
|
|
|
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo |
|
|
// 确保在主线程更新 |
|
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo |
|
|
|
|
|
print("🎵 [MusicService] ✅ 媒体会话进度已更新: \(currentPosition/1000)s/\(duration/1000)s") |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 启用媒体会话回调 |
|
|
/// 启用媒体会话回调 |
|
|
@ -714,9 +753,15 @@ public class MusicService: NSObject { |
|
|
/// 更新媒体会话播放状态 |
|
|
/// 更新媒体会话播放状态 |
|
|
public func updateMediaSessionPlaybackState(isPlaying: Bool) { |
|
|
public func updateMediaSessionPlaybackState(isPlaying: Bool) { |
|
|
self.isPlaying = isPlaying |
|
|
self.isPlaying = isPlaying |
|
|
var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [:] |
|
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0 |
|
|
// 确保在主线程更新 |
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo |
|
|
DispatchQueue.main.async { |
|
|
|
|
|
var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [:] |
|
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0 |
|
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo |
|
|
|
|
|
|
|
|
|
|
|
print("🎵 [MusicService] ✅ 媒体会话播放状态已更新: \(isPlaying ? "播放中" : "已暂停")") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if isPlaying { |
|
|
if isPlaying { |
|
|
startProgressTimer() |
|
|
startProgressTimer() |
|
|
|