-
Notifications
You must be signed in to change notification settings - Fork 9
/
PlaybackService.swift
50 lines (40 loc) · 1.34 KB
/
PlaybackService.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
47
48
49
50
//
// PlaybackService.swift
// TikTok-Example
//
// Created by shayanbo on 2023/6/30.
//
import Foundation
import SwiftUI
import Combine
import VideoPlayerContainer
class PlaybackService : Service {
private var cancellables = [AnyCancellable]()
private var observation: NSKeyValueObservation?
required init(_ context: Context) {
super.init(context)
let player = context.render.player
observation = player.observe(\.rate) { player, changes in
if player.rate == 0 {
context.plugin.present(.center, transition: .scale(scale: 1.5).combined(with: .opacity)) {
AnyView(
Image(systemName: "play.fill").resizable()
.foregroundColor(.white)
.scaledToFit()
.frame(width: 50, height: 50).opacity(0.5)
)
}
} else {
context.plugin.dismiss()
}
}
context.gesture.observe(.tap(.all)) { [weak player] event in
guard let player = player else { return }
if player.rate == 0 {
player.play()
} else {
player.pause()
}
}.store(in: &cancellables)
}
}