質問編集履歴
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,31 +12,55 @@
|
|
12
12
|
|
13
13
|
@IBOutlet weak var slider: UISlider!
|
14
14
|
|
15
|
+
private var token: dispatch_once_t = 0
|
15
16
|
|
16
17
|
|
18
|
+
|
17
|
-
func set() {
|
19
|
+
func setPlayer() {
|
20
|
+
|
21
|
+
if let url = NSURL(string: ("動画URL")!) {
|
22
|
+
|
23
|
+
let player = AVPlayer(URL: url)
|
24
|
+
|
25
|
+
let playerlayer = AVPlayerLayer(player: player)
|
26
|
+
|
27
|
+
self.player = player
|
28
|
+
|
29
|
+
playerlayer.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.rame.height)
|
30
|
+
|
31
|
+
self.layer.addSublayer(playerLayer)
|
32
|
+
|
33
|
+
player.play()
|
34
|
+
|
35
|
+
playerlayer.addSublayer(slider.layer)
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
func setSlider() {
|
18
44
|
|
19
45
|
slider.value = 0
|
20
46
|
|
21
47
|
slider.minimumValue = 0
|
22
48
|
|
23
|
-
slider.maximumValue =
|
49
|
+
slider.maximumValue = 60
|
24
50
|
|
25
51
|
let interval : Double = Double(0.5 * slider.maximumValue) / Double(slider.bounds.maxX)
|
26
52
|
|
27
53
|
let time : CMTime = CMTimeMakeWithSeconds(interval, Int32(NSEC_PER_SEC))
|
28
54
|
|
29
|
-
|
55
|
+
player?.addPeriodicTimeObserverForInterval(time, queue: dispatch_get_main_queue()) { (time) -> Void in
|
30
56
|
|
31
57
|
let time = CMTimeGetSeconds(self.player!.currentTime())
|
32
58
|
|
33
|
-
let value = Float(self.slider.maximumValue - self.slider.minimumValue) * Float(time) / Float(
|
59
|
+
let value = Float(self.slider.maximumValue - self.slider.minimumValue) * Float(time) / Float(60) + Float(self.slider.minimumValue)
|
34
60
|
|
35
|
-
self.finish(time)
|
61
|
+
self.finish(time)
|
36
62
|
|
37
|
-
self.slider.value = value self.timeLabel.text = "0:0\(Int(time))
|
63
|
+
self.slider.value = value self.timeLabel.text = "0:0\(Int(time))
|
38
|
-
|
39
|
-
}
|
40
64
|
|
41
65
|
}
|
42
66
|
|
@@ -44,17 +68,21 @@
|
|
44
68
|
|
45
69
|
|
46
70
|
|
47
|
-
func finish() {
|
71
|
+
func finish(time: Float64) {
|
48
72
|
|
49
73
|
if time >= Float64(60) {
|
50
74
|
|
51
|
-
|
75
|
+
dispatch_once(&self.token) {
|
52
76
|
|
53
|
-
self.player?.pause()
|
77
|
+
self.player?.pause()
|
54
78
|
|
55
|
-
self.player?.seekToTime(CMTimeMakeWithSeconds(0, Int32(NSEC_PER_SEC)))
|
79
|
+
self.player?.seekToTime(CMTimeMakeWithSeconds(0, Int32(NSEC_PER_SEC)))
|
56
80
|
|
57
|
-
}
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
58
86
|
|
59
87
|
}
|
60
88
|
|
1
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
```swift
|
2
|
-
|
3
1
|
現在CustomViewでAVPayerを使って動画を流しているのですが、
|
4
2
|
|
5
3
|
addPeriodicTimeObserverForIntervalこのメソッドのところを記述してから遷移してもメモリが破棄されなくなってしまいました。何が原因なのでしょうか?
|
6
4
|
|
7
5
|
|
6
|
+
|
7
|
+
```swift
|
8
8
|
|
9
9
|
class CustomView {
|
10
10
|
|
@@ -28,12 +28,38 @@
|
|
28
28
|
|
29
29
|
player?.addPeriodicTimeObserverForInterval(time, queue: dispatch_get_main_queue()) { (time) -> Void in
|
30
30
|
|
31
|
-
|
31
|
+
let time = CMTimeGetSeconds(self.player!.currentTime())
|
32
|
+
|
33
|
+
let value = Float(self.slider.maximumValue - self.slider.minimumValue) * Float(time) / Float(10) + Float(self.slider.minimumValue)
|
34
|
+
|
35
|
+
self.finish(time)
|
36
|
+
|
37
|
+
self.slider.value = value self.timeLabel.text = "0:0\(Int(time))
|
32
38
|
|
33
39
|
}
|
34
40
|
|
41
|
+
}
|
42
|
+
|
35
43
|
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
func finish() {
|
48
|
+
|
49
|
+
if time >= Float64(60) {
|
50
|
+
|
51
|
+
dispatch_once(&self.token) {
|
52
|
+
|
53
|
+
self.player?.pause()
|
54
|
+
|
55
|
+
self.player?.seekToTime(CMTimeMakeWithSeconds(0, Int32(NSEC_PER_SEC)))
|
56
|
+
|
57
|
+
}
|
36
58
|
|
37
59
|
}
|
38
60
|
|
39
61
|
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
1分たったら、動画を止めるメソッドです。
|