forked from readium/swift-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaNavigator.swift
106 lines (79 loc) · 3.15 KB
/
MediaNavigator.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Copyright 2021 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
/// Protocol for a navigator rendering an audio or video based publication.
///
/// **WARNING:** This API is experimental and may change or be removed in a future release without
/// notice. Use with caution.
public protocol _MediaNavigator: Navigator {
/// Total duration in the publication, if known.
var totalDuration: Double? { get }
/// Volume of playback, from 0.0 to 1.0.
var volume: Double { get set }
/// Speed of playback.
/// Default is 1.0
var rate: Double { get set }
/// Returns whether the resource is currently playing or not.
var state: MediaPlaybackState { get }
/// Resumes or start the playback.
func play()
/// Pauses the playback.
func pause()
/// Seeks to the given time in the current resource.
func seek(to time: Double)
/// Seeks relatively from the current time in the current resource.
func seek(relatively delta: Double)
}
public extension _MediaNavigator {
/// Toggles the playback.
func playPause() {
switch state {
case .loading, .playing:
pause()
case .paused:
play()
}
}
}
/// Status of a played media resource.
public enum MediaPlaybackState {
case paused
case loading
case playing
}
/// Holds metadata about a played media resource.
public struct MediaPlaybackInfo {
/// Index of the current resource in the `readingOrder`.
public let resourceIndex: Int
/// Indicates whether the resource is currently playing or not.
public let state: MediaPlaybackState
/// Current playback position in the resource, in seconds.
public let time: Double
/// Duration in seconds of the resource, if known.
public let duration: Double?
/// Progress in the resource, from 0 to 1.
public var progress: Double {
guard let duration = duration else {
return 0
}
return time / duration
}
}
public protocol _MediaNavigatorDelegate: NavigatorDelegate {
/// Called when the playback updates.
func navigator(_ navigator: _MediaNavigator, playbackDidChange info: MediaPlaybackInfo)
/// Called when the navigator finished playing the current resource.
/// Returns whether the next resource should be played. Default is true.
func navigator(_ navigator: _MediaNavigator, shouldPlayNextResource info: MediaPlaybackInfo) -> Bool
/// Called when the ranges of buffered media data change.
/// Warning: They may be discontinuous.
func navigator(_ navigator: _MediaNavigator, loadedTimeRangesDidChange ranges: [Range<Double>])
}
public extension _MediaNavigatorDelegate {
func navigator(_ navigator: _MediaNavigator, playbackDidChange info: MediaPlaybackInfo) {}
func navigator(_ navigator: _MediaNavigator, shouldPlayNextResource info: MediaPlaybackInfo) -> Bool { true }
func navigator(_ navigator: _MediaNavigator, loadedTimeRangesDidChange ranges: [Range<Double>]) {}
}