我们如何使用AVAudioPlayerNode
/访问 Apple Music 库中的歌曲AVAudioEngine
进行播放和处理?
我在苹果论坛上问过这个问题。
我们如何使用AVAudioPlayerNode
/访问 Apple Music 库中的歌曲AVAudioEngine
进行播放和处理?
我在苹果论坛上问过这个问题。
“Apple Music”可能指:
由于 DRM 限制,无法播放从 Apple Music macOS 或 iOS 客户端下载到您设备上的 Apple Music 目录中的曲目。但是,您可以使用 macOS 音乐应用程序或 Finder 应用程序播放您拥有的以及已同步到设备上的音频文件,如下所示:
NSAppleMusicUsageDescription
密钥添加到您的 Info.plist 文件中,以及其对应的值AVAudioSession
和AVAudioEngine
MPMediaPickerController
像下面的示例中那样使用,也可以自己制作MPMediaQuery
)AVAudioFile
从该 URL创建一个AVAudioPlayerNode
集来播放AVAudioFile
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController {
let engine = AVAudioEngine()
override func viewDidLoad() {
super.viewDidLoad()
let mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker.allowsPickingMultipleItems = false
mediaPicker.showsItemsWithProtectedAssets = false // These items usually cannot be played back
mediaPicker.showsCloudItems = false // MPMediaItems stored in the cloud don't have an assetURL
mediaPicker.delegate = self
mediaPicker.prompt = "Pick a track"
present(mediaPicker, animated: true, completion: nil)
}
func startEngine(playFileAt: URL) {
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
let avAudioFile = try AVAudioFile(forReading: playFileAt)
let player = AVAudioPlayerNode()
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: avAudioFile.processingFormat)
try engine.start()
player.scheduleFile(avAudioFile, at: nil, completionHandler: nil)
player.play()
} catch {
assertionFailure(String(describing: error))
}
}
}
extension ViewController: MPMediaPickerControllerDelegate {
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
guard let item = mediaItemCollection.items.first else {
print("no item")
return
}
print("picking \(item.title!)")
guard let url = item.assetURL else {
return print("no url")
}
dismiss(animated: true) { [weak self] in
self?.startEngine(playFileAt: url)
}
}
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}