-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCallAudioPlayer.swift
46 lines (37 loc) · 982 Bytes
/
CallAudioPlayer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// CallAudioPlayer.swift
// Telegram
//
// Created by keepcoder on 04/05/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import AVFoundation
class CallAudioPlayer : NSObject, AVAudioPlayerDelegate {
private var player: AVAudioPlayer?;
public var completion:(()->Void)?
let tone: URL
init(_ url:URL, loops:Int, completion:(()->Void)? = nil) {
self.tone = url
self.player = try? AVAudioPlayer(contentsOf: url)
self.completion = completion
super.init()
player?.numberOfLoops = loops
player?.delegate = self
}
func play() {
player?.play()
}
func stop() {
player?.stop()
player?.delegate = nil
player = nil
}
deinit {
stop()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
self.completion?()
player.delegate = nil
}
}