哦,嘿!我一直在努力弄清楚如何让我的AVAudioPlayerNode()在到达SwiftUI项目中的文件末尾时自动停止播放。正如您在下面看到的,我的音频文件不是通过AVAudioEngine,而是AVAudioPlayer有一个audioPlayerDidFinishPlaying函数,这很好。但是,对于使用play函数遍历引擎的音频文件,没有类似的内容,我也不知道如何使它的行为类似。
我试过建造这样的东西
func audioPlayerDidFinishPlaying2(_ player: AVAudioPlayerNode, successfully flag: Bool) {
if flag {
isPlaying = false
print("Playback Engine Stopped")
}
}但这看上去什么都没做。
通过调用stopPlayback2()手动停止引擎的回放很好。
我打电话给引擎播放器
do {
try self.audioPlayer.play(self.audioURL)
}
catch let error as NSError {
print(error.localizedDescription)
}我看过其他的,所以这里和这里的帖子,但这两个解决方案都不适合我。如果您有任何建议,我将非常感谢您的意见!谢谢!!
AudioPlayer.swift
class AudioPlayer: NSObject, ObservableObject, AVAudioPlayerDelegate {
let objectWillChange = PassthroughSubject<AudioPlayer, Never>()
var isPlaying = false {
didSet {
objectWillChange.send(self)
}
}
var audioPlayer: AVAudioPlayer!
func startPlayback (audio: URL) {
let playbackSession = AVAudioSession.sharedInstance()
do {
try playbackSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} catch {
print("Playing over the device's speakers failed")
}
do {
audioPlayer = try AVAudioPlayer(contentsOf: audio)
audioPlayer.delegate = self
audioPlayer.play()
isPlaying = true
} catch {
print("Playback failed.")
}
}
func stopPlayback() {
audioPlayer.stop()
isPlaying = false
}
func stopPlayback2() {
engine.stop()
isPlaying = false
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
isPlaying = false
print("Playback Stopped")
}
}
let engine = AVAudioEngine()
let speedControl = AVAudioUnitVarispeed()
let pitchControl = AVAudioUnitTimePitch()
func play(_ url: URL) throws {
let file = try! AVAudioFile(forReading: url)
let avPlayer = AVAudioPlayerNode()
engine.attach(avPlayer)
engine.attach(pitchControl)
engine.attach(speedControl)
engine.connect(avPlayer, to: speedControl, format: nil)
engine.connect(speedControl, to: pitchControl, format: nil)
engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
avPlayer.scheduleFile(file, at: nil)
isPlaying = true
try engine.start()
avPlayer.play()
}
```
[1]: https://stackoverflow.com/questions/34238432/avaudioengine-avaudioplayernode-didfinish-method-like-avaudioplayer
[2]: https://stackoverflow.com/questions/59080708/calling-stop-on-avaudioplayernode-after-finished-playing-causes-crash发布于 2021-08-31 17:54:18
与其使用avPlayer.scheduleFile(file, at: nil),不如使用带有完成处理程序的方法的形式:
avPlayer.scheduleFile(file, at: nil) {
//call your completion function here
print("Done playing")
}https://stackoverflow.com/questions/69003265
复制相似问题