Created
December 10, 2018 14:46
-
-
Save bibscy/d5ca0b9b0ed465b83baa493a4e77f98e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewWillAppear(_ animated: Bool) { | |
let url = URL(string: "https://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3")! | |
self.playSound(url: url) | |
addPeriodicTimeObserver() | |
} | |
var player: AVAudioPlayer? | |
func playSound(url: URL) { | |
do { | |
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) | |
try AVAudioSession.sharedInstance().setActive(true) | |
player = try AVAudioPlayer(contentsOf: url) | |
player?.play() | |
} catch let error { | |
print(error.localizedDescription) | |
} | |
} | |
var player2: AVPlayer! | |
var timeObserverToken: Any? | |
func addPeriodicTimeObserver() { | |
// Invoke callback every half second | |
let interval = CMTime(seconds: 0.01, | |
preferredTimescale: CMTimeScale(NSEC_PER_SEC)) | |
// Queue on which to invoke the callback | |
let mainQueue = DispatchQueue.main | |
// Add time observer | |
timeObserverToken = | |
player2?.addPeriodicTimeObserver(forInterval: interval, queue: mainQueue) { | |
[weak self] time in | |
// update player transport UI | |
print("time is \(time)") //it is never called | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment