【发布时间】:2016-08-21 11:19:12
【问题描述】:
我有一些 NSSound 对象想要转换为 AVAudioPlayer 实例。我有与NSSound 对象关联的文件路径(NSURLs),但原始文件可能不存在。这是我目前所拥有的:
class SoundObj: NSObject {
var path: NSURL?
var sound: NSSound?
var player: AVAudioPlayer
}
let aSound = SoundObj()
aSound.path = NSURL(fileURLWithPath: "file:///path/to/sound.m4a")
aSound.sound = NSSound(contentsOfURL: aSound.path)!
do {
try aSound.player = AVAudioPlayer(contentsOfURL: aSound.path)
} catch {
// perhaps use AVAudioPlayer(data: ...)?
}
如何将NSSound 对象转换为AVAudioPlayer 实例?
【问题讨论】:
-
如果音频文件不存在,这条线不会崩溃吗?
aSound.sound = NSSound(contentsOfURL: aSound.path)! -
是的,但对于这种情况,
.sound永远不会为空。 -
老实说,我认为您无法从 NSSound 获取声音数据。
-
@brimstone 有办法验证吗?
-
我不太确定你在问什么。你想要一个自定义的
AVAudioPlayer初始化器,它接受NSSound并返回AVAudioPlayer?我认为AVAsset可能更适合您的需求。
标签: swift macos audio avaudioplayer nssound