2017-09-06 14 views
0

私のアプリでは、テーブルビューの別のセルが押されるたびに別のオーディオファイルを再生したい。以下は私の実装ですが、私はエラーを受信し続ける:同じAVAudioEngineから複数の異なるオーディオファイルを再生するにはどうすればよいですか? (エラー:!nodeimpl-> HasEngineImpl())

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: !nodeimpl->HasEngineImpl()'

私は、このエラーは、オーディオエンジンがすでにノードが含まれているため、それがクラッシュしていることを意味だと思います。しかし私は私の場合にそれを修正する方法がわかりません。

var audioPlayerFile : AVAudioFile! 
var audioEngine = AVAudioEngine() 
var pitchPlayer = AVAudioPlayerNode() 
var timePitch = AVAudioUnitTimePitch() 
var delay = AVAudioUnitDelay() 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     pitchPlayer.stop() 
     audioEngine.stop() 
     audioEngine.reset() 

    let filePathResource = myConversation.conversation[indexPath.row].fileName 

    print("file:", filePathResource) 
    if (filePathResource != nil) { 
     setUpAudioFilePath(filePathRes: filePathResource!) 
    } 

    timePitch.pitch = 0 
    delay.delayTime = 0 

    //append more effect: 
    audioEngine.connect(pitchPlayer, to: timePitch, format: audioPlayerFile.processingFormat) 
    audioEngine.connect(timePitch, to: delay, format: audioPlayerFile.processingFormat) 
    audioEngine.connect(delay, to: audioEngine.outputNode, format: audioPlayerFile.processingFormat) 

    pitchPlayer.scheduleFile(audioPlayerFile, at: nil, completionHandler: nil) 
    do { try audioEngine.start()} 
    catch { 
     print("Error: Starting Audio Engine") 
    } 

    pitchPlayer.play() 

} 

func setUpAudioFilePath (filePathRes: String) { 

    if let filePath = Bundle.main.path(forResource: filePathRes, ofType: "m4a") { 

     let filePathUrl = NSURL.fileURL(withPath: filePath) 
     do { audioPlayerFile = try AVAudioFile(forReading: filePathUrl) } 
     catch { 
      print("Error: Reading Audio Player File") 
     } 
     audioEngine.attach(pitchPlayer) 
     audioEngine.attach(timePitch) 
     audioEngine.attach(delay) 

    } else { 
     print("Error: filePath is empty") 
    } 
} 

答えて

0

複数の音声を再生するにはミキサーが必要だと思います。 AVAudioEngineには既にoutputNodeで接続されたミキサーがあります。 試用版

audioEngine.connect(pitchPlayer, to: timePitch, format: audioPlayerFile.processingFormat) 
audioEngine.connect(timePitch, to: delay, format: audioPlayerFile.processingFormat) 
audioEngine.connect(delay, to: audioEngine.mainMixerNode, format: audioPlayerFile.processingFormat) 
関連する問題