SwiftでAVPlayerで曲の再生が終了したときに追跡する東の方法は何ですか?
Avplayerの再生が終了したときに呼び出される関数はありますか、またはavplayerクラス参照とタイマーを組み合わせる必要がありますか?
このような何かが動作します:
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
}
完了したら(またはdeinit
で)オブザーバーを削除することを忘れないでください!
AVAudioPlayerDelegate
プロトコルを実装するオブジェクトを作成し、それをAVAudioPlayer
オブジェクトのデリゲートとして使用する必要があります。次に、それらをリンクします。例えば:
audioPlayer = try! AVAudioPlayer(contentsOf: audioFileUrl)
audioPlayer.delegate = self
デリゲートは、特定のイベントに応答するメソッドを実装できます。これは、オーディオの再生が終了すると起動します。
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
// ...
}
for 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
}
Swift 3の別のバージョン
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
func playerDidFinishPlaying(sender: Notification) {
// Do Something
}
より完全なソリューションはこちらです:
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{
}
}
}
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
}
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")
}
}
}