sách gpt4 ai đã đi

swift - AV基金会代表

In lại 作者:行者123 更新时间:2023-11-30 14:03:27 26 4
mua khóa gpt4 Nike

我正在尝试播放不同的声音文件,并希望在其中一个声音文件播放完毕时收到通知。所以我集成了委托(delegate)方法:

extension ViewController: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {

if (player.url!.isEqual(self.sndAlgo1)) {
self.audioController.audioPlayer!.stop()
self.lblInstruction.text = "Alarmieren Sie den Notruf"
}

if (player.url!.isEqual(self.sndNotruf)) {
self.audioController.audioPlayer!.stop()
self.lblInstruction.text = "Notruf wurde abgesetzt"
}
}
}

为了调用声音文件,我为每个声音文件创建了一个属性:

var sndAlgo1 = NSURL(string: "")!
var sndAlgo2 = NSURL(string: "")!
var sndNotruf = NSURL(string: "")!

func initializeSounds() {
self.sndAlgo1 = self.audioController.createAudioFilePath("Algo1", type: "mp3")
self.sndAlgo2 = self.audioController.createAudioFilePath("Algo2", type: "mp3")
self.sndNotruf = self.audioController.createAudioFilePath("Notruf", type: "mp3")
}

override func viewDidAppear(animated: Bool) {
self.audioController.audioPlayer?.delegate = self
self.audioController.startAudioPlayerWithURL(self.sndAlgo1)
}

@IBAction func btnEmergencyPressed(sender: UIButton) {
self.lblInstruction.text = "Bitte warten"
self.audioController.startAudioPlayerWithURL(self.sndNotruf)
}

声音播放正确,但委托(delegate)方法仅调用一次。我猜想每次使用播放器后都会调用它。是不是我使用的方法不对?

最佳

1 Câu trả lời

我猜你的方法startAudioPlayerWithURL创建了一个新的AVAudioPlayer实例。

每次创建新实例时,都需要设置AVPlayer的委托(delegate)。

您的代码将如下所示:

var sndAlgo1 = NSURL(string: "")!
var sndAlgo2 = NSURL(string: "")!
var sndNotruf = NSURL(string: "")!

func initializeSounds() {
self.sndAlgo1 = self.audioController.createAudioFilePath("Algo1", type: "mp3")
self.sndAlgo2 = self.audioController.createAudioFilePath("Algo2", type: "mp3")
self.sndNotruf = self.audioController.createAudioFilePath("Notruf", type: "mp3")
}

override func viewDidAppear(animated: Bool) {
self.audioController.startAudioPlayerWithURL(self.sndAlgo1)
self.audioController.audioPlayer?.delegate = self
}

@IBAction func btnEmergencyPressed(sender: UIButton) {
self.lblInstruction.text = "Bitte warten"
self.audioController.startAudioPlayerWithURL(self.sndNotruf)
self.audioController.audioPlayer?.delegate = self
}

我不确定它是否有效,因为我不知道你的方法 startAudioPlayerWithURL 的作用。

要改进您的代码,您可以将委托(delegate)交给 audioController 并在每次调用 startAudioPlayerWithURL 时重用它

关于swift - AV基金会代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703315/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com