ios 迅速。AV播放器。歌曲播放完毕后如何追踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27805657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift. AVPlayer. How to track when song finished playing?
提问by Mega4alik
What is the east way to track when song finished playing with AVPlayer in Swift?
在 Swift 中使用 AVPlayer 播放完歌曲后,东方的跟踪方式是什么?
Is there any function which is called when avplayer finished playing, or I should combine timer with avplayer class references?
avplayer 完成播放时是否有任何函数被调用,或者我应该将计时器与 avplayer 类引用结合使用?
回答by Jernej Strasner
Something like this works:
像这样的工作:
func play(url: NSURL) {
let item = AVPlayerItem(URL: url)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}
Don't forget to remove the observer when you're done (or in deinit
)!
不要忘记在完成(或在deinit
)后移除观察者!
回答by Flimm
You need to create an object that implements the AVAudioPlayerDelegate
protocol, and use that as the delegate of the AVAudioPlayer
object. Then link them together, for example:
您需要创建一个实现AVAudioPlayerDelegate
协议的对象,并将其用作AVAudioPlayer
对象的委托。然后将它们链接在一起,例如:
audioPlayer = try! AVAudioPlayer(contentsOf: audioFileUrl)
audioPlayer.delegate = self
The delegate can implement methods that responds to certain events. This one fires when the audio finishes playing:
委托可以实现响应特定事件的方法。当音频播放完毕时触发:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
// ...
}
回答by Ajharul Islam
for Swift 4.2
对于 Swift 4.2
func play(url: URL) {
let item = AVPlayerItem(url: url)
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
@objc func playerDidFinishPlaying(sender: Notification) {
// Your code here
}
回答by Mike Carpenter
Another version for Swift 3
Swift 3 的另一个版本
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
func playerDidFinishPlaying(sender: Notification) {
// Do Something
}
回答by Martian2049
a more complete solution is here:
更完整的解决方案在这里:
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController,AVAudioPlayerDelegate {
var player: AVAudioPlayer = AVAudioPlayer()
@IBAction func play(_ sender: UIButton) {
player.play()
player.currentTime=14*60-10
print(player.currentTime)
}
@IBAction func pause(_ sender: UIButton) {
player.pause()
}
@IBAction func replay(_ sender: UIButton) {
player.currentTime=0
}
override func viewDidLoad() {
super.viewDidLoad()
do{
let audioPath = Bundle.main.path(forResource: "elon", ofType: "mp3")
player = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: audioPath!))
player.prepareToPlay()
player.delegate = self
}
catch{
print(error)
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool){
print(flag)
print("here")
if flag == true{
}
}
}
回答by Mohd Ahmed
For Swif3 you will need to change as follow:
对于 Swif3,您需要进行如下更改:
func play(url: NSURL) {
let item = AVPlayerItem(URL: url)
NotificationCenter.default.addObserver(self,selector:Selector("playerDidFinishPlaying"), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying() {
// Your code here
}
回答by Lizzz
import AVFoundation
var AVPlayerCustom:AVAudioPlayer = AVAudioPlayer()
class PlayerModule: NSObject, AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("Finish")
}
func playWithData(data: Data, proc: Int) {
//print(data)
do {
AVPlayerCustom = try AVAudioPlayer(data: data)
AVPlayerCustom.delegate = self as! AVAudioPlayerDelegate
AVPlayerCustom.prepareToPlay()
AVPlayerCustom.play()
}
catch {
print("error1")
}
}
}