SWIFT中的audioPlayerDidFinishPlaying函数 [英] audioPlayerDidFinishPlaying function in SWIFT
问题描述
我正在使用AVFoundation框架快速编写一个简单的播放器.除了我的播放器不断重复播放同一首歌曲外,其他所有内容似乎都正常运行.我的播放列表中只有一首歌,所以这很有意义.我要尝试执行的操作是检查 audioPlayerDidFinishPlaying 标志以确保已完成播放,然后将其停止.我不确定如何在这里实现调用以获取标志的代码.
I am writing a simple player in swift using the AVFoundation framework. Everything seems to be working except my player keeps playing the same song over and over again. I only have one song in my play list so this makes sense. what I am trying to do is check the audioPlayerDidFinishPlaying flag to make sure it is done playing and then I will make it stop. I am not sure how to implement the call to get the flag here is my code.
mp3Player?.play()
**if (mp3Player?.audioPlayerDidFinishPlaying:successfully) {**
mp3Player?.stop()
}
在目标c中获取标志的正确方法是:
the proper way to get the flag in objective c is:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"audioPlayerDidFinishPlaying");
}
有人可以解释如何在SWIFT中实现相同的功能吗?这是我在SWIFT中的第一个程序.我试过阅读SWIFT中的void函数,但是没有运气.
Can anyone explain how to implement the same function in SWIFT? This is my first program in SWIFT. I tried reading about void functions in SWIFT but no luck.
我遇到的错误是无法将类型'AVAudioPlayer.Type'的值转换为预期的参数类型'AVAudioPlayer'
the error I am getting is Cannot convert value of type 'AVAudioPlayer.Type' to expected argument type 'AVAudioPlayer'
推荐答案
您可以交叉检查是否在开始播放音频时将numberOfLoops
设置为0
并将delegate
设置为self
或播放器所在的控制器正在玩.
You can cross check that while start playing the audio numberOfLoops
set to 0
and delegate
is set to self
or controller where your player is playing.
mp3Player?.numberOfLoops = 0
mp3Player?.delegate = self
在同一类中,实现AVAudioPlayer
的delegate
方法,如下所示.
In the same class implement the delegate
method of AVAudioPlayer
like as shown below.
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
//You can stop the audio
player.stop()
}
这篇关于SWIFT中的audioPlayerDidFinishPlaying函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!