Skip to content

Commit ac949fe

Browse files
committed
docs: add Playback from Segments docs
1 parent 55ca190 commit ac949fe

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

doc/segments-playback.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Segments Playback
2+
3+
The player supports a feature called 'Playback from Segments'. This lets you to click on a segment in your app (i.e. a paragraph) to begin playback from that segment. If the segment is already playing then it will be paused instead.
4+
5+
The segments playback in the iOS Player is based on the [Segments Playback in the Web Player](https://github.com/beyondwords-io/player/blob/main/doc/segments-playback.md). The iOS SDK cannot automatically identify segments, instead you should be able to manually link each text segment with its beyonwordas marker.
6+
7+
You can find an example of how segment playback can be integrated in your app in [PlaybackFromSegmentsViewController.swift](../Example/Example/PlaybackFromSegmentsViewController.swift)
8+
9+
## How it works
10+
11+
To highlight the current segment you have to listen for the `CurrentSegmentUpdated` event, then find the correspondig UI element to the `currentSegment` and apply the desired styling to it.
12+
13+
```swift
14+
extension PlaybackFromSegmentsViewController : PlayerDelegate {
15+
public func player(_ playerView: PlayerView, onEvent event: PlayerEvent, settings: PlayerSettings) {
16+
if (self.playerView !== playerView) { return }
17+
18+
if (event.type == "CurrentSegmentUpdated") {
19+
let text = settings.currentSegment?.marker.flatMap { self.segments[$0] }
20+
for view in self.contentView.arrangedSubviews {
21+
guard let label = view as? UILabel else { continue }
22+
if (label.text == text) {
23+
label.backgroundColor = .lightGray
24+
} else {
25+
label.backgroundColor = .clear
26+
}
27+
}
28+
}
29+
}
30+
}
31+
```
32+
33+
To change the current time of the player when a segment is clicked you have to set `UITapGestureRecognizer` and call `setCurrentSegment` with the correspondig marker.
34+
35+
```swift
36+
@objc private func segmentTapped(_ gesture: UITapGestureRecognizer) {
37+
guard let label = gesture.view as? UILabel else { return }
38+
guard let marker = self.segments.first(where: { $0.value == label.text })?.key else { return }
39+
playerView.setCurrentSegment(segmentMarker: marker)
40+
}
41+
```

0 commit comments

Comments
 (0)