Skip to content

Commit

Permalink
Add an audiobook player in the Test App (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored Sep 21, 2023
1 parent 192fd36 commit 4113b1a
Show file tree
Hide file tree
Showing 30 changed files with 1,423 additions and 742 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file. Take a look

## [Unreleased]

* Support for Xcode 15.

### Added

#### Navigator
Expand All @@ -27,6 +29,7 @@ All notable changes to this project will be documented in this file. Take a look

#### Navigator

* `AudioSession` and `NowPlayingInfo` are now stable!
* You need to provide the configuration of the Audio Session to the constructor of `PublicationSpeechSynthesizer`, instead of `AVTTSEngine`.


Expand Down
42 changes: 22 additions & 20 deletions Sources/Navigator/Audiobook/AudioNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public protocol _AudioNavigatorDelegate: _MediaNavigatorDelegate {}
/// * Readium Audiobook
/// * ZAB (Zipped Audio Book)
///
/// **WARNING:** This API is experimental and may change or be removed in a future release without
/// notice. Use with caution.
open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
/// **WARNING:** This API is experimental and may change or be removed in a
/// future release without notice. Use with caution.
open class _AudioNavigator: _MediaNavigator, AudioSessionUser, Loggable {
public weak var delegate: _AudioNavigatorDelegate?

private let publication: Publication
public let publication: Publication
private let initialLocation: Locator?
public let audioConfiguration: _AudioSession.Configuration
public let audioConfiguration: AudioSession.Configuration

public init(
publication: Publication,
initialLocation: Locator? = nil,
audioConfig: _AudioSession.Configuration = .init(
audioConfig: AudioSession.Configuration = .init(
category: .playback,
mode: .default,
routeSharingPolicy: .longForm,
Expand All @@ -47,7 +47,7 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
}

deinit {
_AudioSession.shared.end(for: self)
AudioSession.shared.end(for: self)
}

/// Current playback info.
Expand Down Expand Up @@ -106,7 +106,7 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
return
}

let session = _AudioSession.shared
let session = AudioSession.shared
switch player.timeControlStatus {
case .paused:
session.user(self, didChangePlaying: false)
Expand Down Expand Up @@ -135,7 +135,7 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
}

self.shouldPlayNextResource { playNext in
if playNext, self.goToNextResource() {
if playNext, self.goForward() {
self.play()
}
}
Expand Down Expand Up @@ -281,28 +281,30 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
return go(to: locator, animated: animated, completion: completion)
}

@discardableResult
public func goForward(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
false
/// Indicates whether the navigator can go to the next content portion
/// (e.g. page or audiobook resource).
public var canGoForward: Bool {
publication.readingOrder.indices.contains(resourceIndex + 1)
}

@discardableResult
public func goBackward(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
false
/// Indicates whether the navigator can go to the next content portion
/// (e.g. page or audiobook resource).
public var canGoBackward: Bool {
publication.readingOrder.indices.contains(resourceIndex - 1)
}

@discardableResult
public func goToNextResource(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
public func goForward(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
goToResourceIndex(resourceIndex + 1, animated: animated, completion: completion)
}

@discardableResult
public func goToPreviousResource(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
public func goBackward(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
goToResourceIndex(resourceIndex - 1, animated: animated, completion: completion)
}

@discardableResult
public func goToResourceIndex(_ index: Int, animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
private func goToResourceIndex(_ index: Int, animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
guard publication.readingOrder.indices ~= index else {
return false
}
Expand Down Expand Up @@ -339,7 +341,7 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
}

public func play() {
_AudioSession.shared.start(with: self, isPlaying: false)
AudioSession.shared.start(with: self, isPlaying: false)

if player.currentItem == nil, let location = initialLocation {
go(to: location)
Expand All @@ -355,7 +357,7 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {
player.seek(to: CMTime(seconds: time, preferredTimescale: 1000))
}

public func seek(relatively delta: Double) {
public func seek(by delta: Double) {
seek(to: currentTime + delta)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Navigator/CBZ/CBZNavigatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab

public weak var delegate: CBZNavigatorDelegate?

private let publication: Publication
public let publication: Publication
private let initialIndex: Int

private let pageViewController: UIPageViewController
Expand Down
Loading

0 comments on commit 4113b1a

Please sign in to comment.