- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试播放具有不同效果的声音。在之前的 viewController 中,我录制了一个声音,然后在下一个屏幕中,它可以与效果一起播放。第一次它工作正常,但第二次它崩溃并出现如下错误:
2015-08-07 13:00:45.900 Pitch Perfect[9643:1121173] 13:00:45.900 ERROR: AVAudioEngine.mm:253: AttachNode: required condition is false: !nodeimpl->HasEngineImpl() 2015-08-07 13:00:45.953 Pitch Perfect[9643:1121173] Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: !nodeimpl->HasEngineImpl()'
import UIKit
import AVFoundation
class PlaySoundsViewController: UIViewController, AVAudioPlayerDelegate {
var receivedAudio:RecordedAudio!
var audioPlayer: AVAudioPlayer!
var disabledButton:UIButton!
var firstTime:Bool = true
var audioEngine:AVAudioEngine!
var audioFile:AVAudioFile!
var audioPlayerNode:AVAudioPlayerNode!
var audioStopped:Bool!
var typeOfSound:IntegerLiteralType!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var reverbButton: UIButton!
@IBOutlet weak var echoButton: UIButton!
@IBOutlet weak var darthButton: UIButton!
@IBOutlet weak var chipmonkButton: UIButton!
@IBOutlet weak var snailButton: UIButton!
@IBOutlet weak var rabbitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
audioPlayer = AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl, error: nil)
audioPlayer.enableRate=true
audioPlayer.delegate=self
var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
audioPlayerNode=AVAudioPlayerNode();
audioEngine = AVAudioEngine()
audioFile = AVAudioFile(forReading: receivedAudio.filePathUrl, error: nil)
audioStopped=true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playAnimalSound(animal: String) {
if audioStopped==false {
resetAudio(typeOfSound)
}
typeOfSound = 1
audioPlayer.currentTime=0
switch animal {
case "snail":
audioPlayer.rate=0.5
case "rabbit":
audioPlayer.rate=2.0
default:
showMessage("Sound not found. How can it be?")
}
audioPlayer.play()
stopButton.hidden=false
stopButton.enabled=true
}
@IBAction func playSnailSound(sender: UIButton) {
highlightButton(sender)
playAnimalSound("snail")
}
@IBAction func playRabbitSound(sender: UIButton) {
highlightButton(sender)
playAnimalSound("rabbit")
}
func soundEnded() {
stopButton.hidden=true
disabledButton.enabled=true;
if(audioEngine.running) {
audioEngine.stop()
audioEngine.reset();
}
}
func playAudioWithVariablePitch(pitch: Float, type: String) {
if audioStopped==false {
resetAudio(typeOfSound)
}
audioEngine.attachNode(audioPlayerNode)
switch type {
case "normal":
typeOfSound = 2
var changePitchEffect = AVAudioUnitTimePitch()
changePitchEffect.pitch = pitch
audioEngine.attachNode(changePitchEffect)
audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)
case "reverb":
typeOfSound = 3
var changeReverbEffect = AVAudioUnitReverb()
changeReverbEffect.loadFactoryPreset(AVAudioUnitReverbPreset(rawValue: 4)!)
changeReverbEffect.wetDryMix=50;
audioEngine.attachNode(changeReverbEffect)
audioEngine.connect(audioPlayerNode, to: changeReverbEffect, format: nil)
audioEngine.connect(changeReverbEffect, to: audioEngine.outputNode, format: nil)
case "delay":
typeOfSound = 3
var changeDelayEffect = AVAudioUnitDelay()
audioEngine.attachNode(changeDelayEffect)
audioEngine.connect(audioPlayerNode, to: changeDelayEffect, format: nil)
audioEngine.connect(changeDelayEffect, to: audioEngine.outputNode, format: nil)
default:
showMessage("oops, there was an internal problem. Never mind")
}
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: soundEnded)
audioEngine.startAndReturnError(nil)
stopButton.hidden=false
stopButton.enabled=true
audioPlayerNode.play()
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
if flag {
stopButton.hidden=true
disabledButton.enabled=true;
audioStopped=true
println("I hid stopButton and enabled the disabled one")
}
}
@IBAction func playDelaySound(sender: UIButton) {
highlightButton(sender)
playAudioWithVariablePitch(0, type: "delay")
}
@IBAction func playReverbSound(sender: UIButton) {
highlightButton(sender)
playAudioWithVariablePitch(0, type: "reverb")
}
@IBAction func playChipmunkSound(sender: UIButton) {
highlightButton(sender)
playAudioWithVariablePitch(1000.0, type: "normal")
}
@IBAction func playDarthVaderSound(sender: UIButton) {
highlightButton(sender)
playAudioWithVariablePitch(-900.0, type: "normal")
}
@IBAction func stopPlaying(sender: UIButton) {
resetAudio(typeOfSound)
stopButton.hidden=true
stopButton.enabled=false;
disabledButton.enabled=true;
}
func highlightButton(button: UIButton) {
if firstTime {
firstTime=false
} else {
disabledButton.enabled=true;
}
button.enabled=false;
disabledButton=button;
}
func resetAudio(type: IntegerLiteralType) {
switch type {
case 1 :
audioPlayer.stop()
println("case 1")
case 2 :
println("case 2")
if audioEngine.running {
audioEngine.stop()
}
audioEngine.reset()
case 3 :
audioEngine.stop()
default:
break
}
audioStopped=true;
}
func showMessage(msg: String) {
var message=UIAlertView(title: "Alert", message: msg, delegate: nil, cancelButtonTitle: "ok I won't panic")
}
}
有人知道为什么会崩溃吗?我研究了 AVAudioEngine、AVAudioPlayer 和 AVAudioPlayerNode 类,但没有结果。
谢谢
最佳答案
我知道这是一个老问题,但我没有在上面看到正确的答案。
它崩溃的原因实际上在错误消息中概述了:
AttachNode: required condition is false: !nodeimpl->HasEngineImpl()
换句话说,在附加节点时,该节点必须尚未附加到引擎 (!nodeimpl->HasEngineImpl()
)。
解决方案是在尝试再次添加之前使用 audioEngine.detachNode
删除节点。
关于swift - 在 Swift 中使用 AudioEngine 重复声音时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876311/
在 iOS 中为 audioEngine 创建自定义实时音频效果的最佳方法是什么?我想在低级别处理音频,如何正确处理?一定要使用 AudioUnitExtension 吗?更简单的意思是,是否可以从
背景 - 我在 Apple 最近的 WWDC 上发布的以下视频列表中看到了一个名为“AVAudioEngine in Practice”的视频,用于将音效应用于音频。 https://develope
如何捕捉 SKScene 暂停或 audioEngine 停止的时刻? 我有两个 SKScenes:GameScene 和 EndScene,我在游戏过程中使用 GameScene 的 audioEn
我正在尝试播放具有不同效果的声音。在之前的 viewController 中,我录制了一个声音,然后在下一个屏幕中,它可以与效果一起播放。第一次它工作正常,但第二次它崩溃并出现如下错误: 2015-0
我正在尝试做一些非常简单的事情,但我只是在断断续续的过程中取得了进步。我现在只想将音频数据从麦克风获取到文件中。一旦我克服了这个障碍,我将对 block 中的数据进行更多处理。我已经厌倦了我在 AVA
我目前正在尝试让 Apple 的新音频引擎与我当前的音频设置一起工作。具体来说,我正在尝试使用音频引擎更改音高,根据 this post 这显然是可能的. 我还研究了其他音高改变解决方案,包括 Dir
我有以下代码用于生成给定频率和持续时间的音频。它大致基于这个在 Android 上做同样事情的答案(感谢:@Steve Pomeroy): https://stackoverflow.com/a/37
我是一名优秀的程序员,十分优秀!