【发布时间】:2015-11-17 08:49:03
【问题描述】:
我需要在设备开始输出音频时立即启动操作。我正在使用 AVPlayer 并正在从 Parse 流式传输音频文件。使用等待 (AVPlayer.currentTime() != nil) 和 (AVPlayer.rate > 0) 等其他方法不够准确,我需要确切知道从设备输出音频的时间。我尝试使用 AVAudioEngine,然后附加一个具有 AVAudioNodeBus 的 AVAudioNode,但我无法让它工作。任何建议或技术都会非常好,谢谢!
这是我的 AudioEngine 代码。我在实例级别实例化 AudioEngine。创建standardFormat 时,我不知道standardFormatWithSampleRate 或通道块使用什么。当我尝试 installTapOnBus 时,我不知道该块使用什么,所以我输入了 nil,但这也会触发错误。任何帮助将不胜感激,我对 iOS 开发人员非常陌生,并且已多次阅读 Apple 的文档,但我无法完全理解它,也无法在网上找到任何最近的示例。
class TableViewController: UITableViewController, AVAudioPlayerDelegate {
var iDArray = [String]()
var NameArray = [String]()
var durationInSeconds = Double()
var currentSong = String()
override func viewDidLoad() {
super.viewDidLoad()
let ObjectIDQuery = PFQuery(className: "Songs")
ObjectIDQuery.findObjectsInBackgroundWithBlock {
(objectsArray: [PFObject]?, error: NSError?) -> Void in
//objectsArray!.count != 0
var objectIDs = objectsArray!
for i in 0...objectIDs.count-1 {
self.iDArray.append(objectIDs[i].valueForKey("objectId") as! String)
self.NameArray.append(objectIDs[i].valueForKey("SongName") as! String)
self.tableView.reloadData()
}
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
func grabSong () {
let songQuery = PFQuery(className: "Songs")
songQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block: {
(object: PFObject?, error : NSError?) -> Void in
if let audioFile = object?["SongFile"] as? PFFile {
let audioFileUrlString: String = audioFile.url!
let audioFileUrl = NSURL(string: audioFileUrlString)!
AudioPlayer = AVPlayer(URL: audioFileUrl)
AudioPlayer.play()
})
}
func audioFunction() {
var audioPlayerNode = AVAudioNode()
var audioBus = AVAudioNodeBus()
var standardFormat = AVAudioFormat(standardFormatWithSampleRate: <#T##Double#>, channels: <#T##AVAudioChannelCount#>)
AudioEngine.attachNode(audioPlayerNode)
audioPlayerNode.outputFormatForBus(audioBus)
audioPlayerNode.installTapOnBus(audioBus, bufferSize: 100, format: standardFormat, block: nil)
if AudioEngine.running == true {
print("the audio engine is running")
} else {
print("the audio engine is NOTTT running")
}
}
func attachNode(audioNode : AVAudioNode) {
AudioEngine.attachNode(audioNode)
AudioEngine.outputNode
print(AudioEngine.outputNode.description)
if AudioEngine.running == true {
print("the audio engine is running")
} else {
print("the audio engine is NOTTT running")
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return iDArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
cell?.textLabel!.text = NameArray[indexPath.row]
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
SelectedSongNumber = indexPath.row
grabSong()
}
}
我应该改用 AVAudioSession 吗?还是 AVCaptureSession?
【问题讨论】:
-
我很想给
AVPlayer添加一个水龙头。为什么不显示您的AVAudioEngine代码?我很好奇为什么这不起作用。 -
抱歉,刚刚添加了它
-
您实际上是如何使用
AVAudioEngine播放流式音频文件的? -
我更新了更多我的代码,因为它有助于清除任何东西。我实际上并没有使用 AudioEngine 播放流式音频文件。我使用 AVPlayer 构建了应用程序,现在我意识到要获得我正在寻找的精度,我需要以某种方式监控音频输出,而我发现这样做的唯一方法(我认为)是附加一个 AVAudioNode
-
我真的只想知道音频何时开始从设备输出。它不必连接到我的 AVPlayer,我只是想监控硬件的某些部分何时开始使用......这甚至可能吗?