2016-03-25 4 views
0

私はAVAudioPlayerを使用して曲を再生しています。曲が30秒のマークになると、動作を開始したい(機能を呼び出す)ようにします。Swift:特定の部分のスパークアクション(avaudioplayer)

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 
} 

しかし、私は歌が30秒を打ったときに似ていますが、何かをしたいと思います:私は歌がこのように終了したときにこれを行う方法を知っています。これは可能ですか?今はNSTimersを使用する必要がありますが、信頼性がありません。

ありがとうございました!

答えて

0

これは高価なのかどうかはわかりませんが、うまくいきます。

まず私は "checkForStrobe" ロットを呼び出すループを実行します。

var dpLink : CADisplayLink? 
dpLink = CADisplayLink(target: self, selector: "checkForStrobe") 
dpLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) 

その後、私は単に私のcheckForStrobe関数内でこれを置く:

func checkForStrobe(){ 
    //This is being called repeatedly checking if its time to run strobe 
    if(backgroundAudio.currentTime >= 38.5 && backgroundAudio.currentTime <= 38.6){ 
     runStrobe() 
    } 
    if(backgroundAudio.currentTime >= 63.5 && backgroundAudio.currentTime <= 63.6){ 
     stopStrobe() 
    } 
    if(backgroundAudio.currentTime >= 125.0 && backgroundAudio.currentTime <= 125.1){ 
     runStrobe() 
    } 
    if(backgroundAudio.currentTime >= 150.0 && backgroundAudio.currentTime <= 150.1){ 
     stopStrobe() 
    } 
} 

これは私の問題を解決しました。

関連する問題