2015-01-06 18 views

risposta

43

Qualcosa di simile a questo funziona:

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 
} 

Non dimenticare di rimuovere l'osservatore quando il gioco è fatto (o in deinit)!

+0

Funziona, grazie mille! – Mega4alik

+1

Quando l'ho provato, ho ricevuto questo errore: "Terminazione dell'app a causa dell'eccezione non rilevata 'NSInvalidArgumentException', motivo: '- [myApp.MyViewController playerDidFinishPlaying]: selettore non riconosciuto inviato all'istanza". Mi potete aiutare per favore? –

+0

@Llg dimenticato il: in "playerDidFinishPlaying:" – LightMan

6

È necessario creare un oggetto che implementa il protocollo AVAudioPlayerDelegate e utilizzarlo come delegato dell'oggetto AVAudioPlayer. Quindi collegarli, ad esempio:

audioPlayer = try! AVAudioPlayer(contentsOf: audioFileUrl) 
audioPlayer.delegate = self 

Il delegato può implementare metodi che rispondono a determinati eventi. Questo uno fuochi quando l'audio termina la riproduzione:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
    // ... 
} 
+0

Questo funziona per me non accettato. – imagngames

+4

@Flimm sta usando un AVPlayer e non un AVAudioPlayer – Edward

2

Per Swif3 si avrà bisogno di cambiare come segue:

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 
} 
6

Un'altra versione di Swift 3

NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item) 

func playerDidFinishPlaying(sender: Notification) { 

    // Do Something 
} 
+0

Mike Carpenter, ho provato la tua soluzione ma non riesco a farla funzionare. Si prega di vedere la prossima "risposta" per il mio codice. –

+0

Che errore hai ricevuto? Ho questo codice in alcuni progetti e si compila ed esegue senza problemi. –

0

una soluzione più completa è qui:

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{ 

     } 
    } 


} 
0
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") 
     } 
    } 
} 
Problemi correlati