[英]Apple Music & AVAudioEngine in Swift
How can we access songs in the Apple Music library with AVAudioPlayerNode
/ AVAudioEngine
for playback and processing?我们如何使用
AVAudioPlayerNode
/ AVAudioEngine
访问 Apple Music 库中的歌曲进行播放和处理?
I have asked this question in Apple forum .我在苹果论坛上问过这个问题。
"Apple Music" may refer to: “苹果音乐”可以指:
Due to DRM restrictions it is not possible to play tracks from the Apple Music catalogue downloaded onto your device from the Apple Music macOS or iOS clients.由于 DRM 限制,无法播放从 Apple Music macOS 或 iOS 客户端下载到您设备上的 Apple Music 目录中的曲目。 However you can play audio files you own and which you've synced onto your device using the macOS Music app or the Finder app , as follows:
但是,您可以使用 macOS 音乐应用程序或 Finder 应用程序播放您拥有的以及已同步到设备上的音频文件,如下所示:
NSAppleMusicUsageDescription
key to your Info.plist file, and its corresponding valueNSAppleMusicUsageDescription
键添加到您的 Info.plist 文件中,及其对应的值AVAudioSession
and AVAudioEngine
AVAudioSession
和AVAudioEngine
MPMediaPickerController
like in the example below or you can make your own MPMediaQuery
)MPMediaPickerController
或者你可以制作自己的MPMediaQuery
)AVAudioFile
from that URLAVAudioFile
AVAudioPlayerNode
set to play that AVAudioFile
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.