react-native-audio-browser 1.0.0-next.1 → 1.0.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,13 +8,12 @@ func nextPlaybackState(from current: PlaybackState, on event: PlaybackEvent) ->
8
8
  case .trackLoading: return .loading
9
9
  case .trackUnloaded: return PlaybackState.none
10
10
  case .trackEndedNaturally: return .ended
11
- // .error holds against the AVPlayer's own events: an item still connecting when the retry gave
12
- // up can report them late. Leaving .error takes an explicit reload, through .loading.
13
- case .avPlayerWaiting: return current == .error ? nil : .buffering
14
- case .avPlayerPlaying, .audioFrameDecoded: return current == .error ? nil : .playing
11
+ // The AVPlayer's own events can land after a stop, an end or a failure.
12
+ case .avPlayerWaiting: return current.isTerminal ? nil : .buffering
13
+ case .avPlayerPlaying, .audioFrameDecoded: return current.isTerminal ? nil : .playing
15
14
  case .errorOccurred: return .error
16
15
  case .retryCancelled:
17
- guard current != .stopped, current != .ended, current != .error else { return nil }
16
+ guard !current.isTerminal else { return nil }
18
17
  return .paused
19
18
  case .loadSeekCompleted:
20
19
  guard current == .loading else { return nil }
@@ -27,11 +26,14 @@ func nextPlaybackState(from current: PlaybackState, on event: PlaybackEvent) ->
27
26
  guard current != .error, current != .ended else { return nil }
28
27
  return .paused
29
28
  case .bufferingSufficient:
30
- // A buffer refill must not move a terminal state back to .ready: .ended would restart the
31
- // finished track (playWhenReady is still true), and .stopped/.error would skip reload-on-play,
32
- // which starts only from those states. Leaving them takes an explicit load or reload.
33
- guard current != .playing, current != .ended, current != .stopped, current != .error
34
- else { return nil }
29
+ // .ended still has the play intent: a move to .ready would restart the finished track.
30
+ guard current != .playing, !current.isTerminal else { return nil }
35
31
  return .ready
36
32
  }
37
33
  }
34
+
35
+ extension PlaybackState {
36
+ /// Stopped, ended or failed: play() reloads from it, and only an explicit load, through
37
+ /// .loading, leaves it. The AVPlayer's own events and a cancelled retry cannot.
38
+ var isTerminal: Bool { self == .stopped || self == .ended || self == .error }
39
+ }
@@ -188,7 +188,7 @@ final class Player {
188
188
  // A terminal state or a cancelled retry has no item to play, or one parked at its end: reload,
189
189
  // re-resolving a URL that may have expired since. Only .error and a cancelled retry resume where they failed.
190
190
  let resumes = state == .error || load?.reloadOnPlay == true
191
- if newValue, resumes || state == .stopped || state == .ended {
191
+ if newValue, resumes || state.isTerminal {
192
192
  reloadResolving(startFromCurrentTime: resumes)
193
193
  }
194
194
  // While loading, the engine may still hold the previous track's item: a play waits for the new one.
@@ -39,27 +39,34 @@ struct UnconditionalEventTests {
39
39
  }
40
40
  }
41
41
 
42
- @Test func avPlayerWaiting_transitionsToBuffering_exceptFromError() {
42
+ @Test func errorOccurred_alwaysTransitionsToError() {
43
43
  for state in allStates {
44
- #expect(nextPlaybackState(from: state, on: .avPlayerWaiting) == (state == .error ? nil : .buffering))
44
+ #expect(nextPlaybackState(from: state, on: .errorOccurred(dummyError)) == .error)
45
45
  }
46
46
  }
47
+ }
48
+
49
+ // MARK: - AVPlayer events
47
50
 
48
- @Test func avPlayerPlaying_transitionsToPlaying_exceptFromError() {
51
+ @Suite("AVPlayer events — held by .stopped, .ended and .error")
52
+ struct EngineEventTests {
53
+ private let terminalStates: [PlaybackState] = [.stopped, .ended, .error]
54
+
55
+ @Test func avPlayerWaiting_transitionsToBuffering_exceptFromATerminalState() {
49
56
  for state in allStates {
50
- #expect(nextPlaybackState(from: state, on: .avPlayerPlaying) == (state == .error ? nil : .playing))
57
+ #expect(nextPlaybackState(from: state, on: .avPlayerWaiting) == (terminalStates.contains(state) ? nil : .buffering))
51
58
  }
52
59
  }
53
60
 
54
- @Test func audioFrameDecoded_transitionsToPlaying_exceptFromError() {
61
+ @Test func avPlayerPlaying_transitionsToPlaying_exceptFromATerminalState() {
55
62
  for state in allStates {
56
- #expect(nextPlaybackState(from: state, on: .audioFrameDecoded) == (state == .error ? nil : .playing))
63
+ #expect(nextPlaybackState(from: state, on: .avPlayerPlaying) == (terminalStates.contains(state) ? nil : .playing))
57
64
  }
58
65
  }
59
66
 
60
- @Test func errorOccurred_alwaysTransitionsToError() {
67
+ @Test func audioFrameDecoded_transitionsToPlaying_exceptFromATerminalState() {
61
68
  for state in allStates {
62
- #expect(nextPlaybackState(from: state, on: .errorOccurred(dummyError)) == .error)
69
+ #expect(nextPlaybackState(from: state, on: .audioFrameDecoded) == (terminalStates.contains(state) ? nil : .playing))
63
70
  }
64
71
  }
65
72
  }
@@ -1741,3 +1741,68 @@ struct PlayerLivePauseTests {
1741
1741
  #expect(callbacks.errorEvents.isEmpty)
1742
1742
  }
1743
1743
  }
1744
+
1745
+ /// A stopped or ended track plays again only through a reload: an AVPlayer event that lands after
1746
+ /// the stop or the end must not move it out of that state, or the next play resumes the item left
1747
+ /// behind.
1748
+ @Suite("A late AVPlayer event does not cost the next play its reload")
1749
+ @MainActor
1750
+ struct LateEngineEventTests {
1751
+ enum LateAfterStop: String, CaseIterable, Sendable {
1752
+ case liveLatePlayingThenPaused = "live, a late playing then paused status"
1753
+ case liveLateFirstAudio = "live, a late first audio"
1754
+ case onDemandLateWaitingAfterTheRewind = "on-demand, a late waiting status after the rewind"
1755
+ case onDemandLatePlaying = "on-demand, a late playing status"
1756
+
1757
+ var live: Bool {
1758
+ switch self {
1759
+ case .liveLatePlayingThenPaused, .liveLateFirstAudio: true
1760
+ case .onDemandLateWaitingAfterTheRewind, .onDemandLatePlaying: false
1761
+ }
1762
+ }
1763
+
1764
+ var events: [EngineEvent] {
1765
+ switch self {
1766
+ case .liveLatePlayingThenPaused:
1767
+ [.timeControlStatus(.playing, waitingForNoItem: false), .timeControlStatus(.paused, waitingForNoItem: false)]
1768
+ case .liveLateFirstAudio: [.audioDidStart]
1769
+ case .onDemandLateWaitingAfterTheRewind: [.timeControlStatus(.waitingToPlayAtSpecifiedRate, waitingForNoItem: false)]
1770
+ case .onDemandLatePlaying: [.timeControlStatus(.playing, waitingForNoItem: false)]
1771
+ }
1772
+ }
1773
+ }
1774
+
1775
+ @Test(arguments: LateAfterStop.allCases)
1776
+ func aStoppedTrackReloadsOnPlay(_ late: LateAfterStop) throws {
1777
+ let (player, engine, _) = try makePlaying(live: late.live)
1778
+ player.stop()
1779
+ for event in late.events {
1780
+ engine.emit(event)
1781
+ }
1782
+ #expect(player.state == .stopped)
1783
+ engine.resolveAndLoadCalls.removeAll()
1784
+ player.play()
1785
+ #expect(engine.resolveAndLoadCalls.count == 1)
1786
+ }
1787
+
1788
+ @Test func anEndedTrackReloadsOnPlayAfterALateWaitingStatus() throws {
1789
+ let (player, engine, _) = try makePlaying()
1790
+ engine.currentTime = 300
1791
+ engine.duration = 300
1792
+ engine.emit(.playedToEnd)
1793
+ engine.emit(.timeControlStatus(.waitingToPlayAtSpecifiedRate, waitingForNoItem: false))
1794
+ #expect(player.state == .ended)
1795
+ engine.resolveAndLoadCalls.removeAll()
1796
+ player.play()
1797
+ #expect(engine.resolveAndLoadCalls.count == 1)
1798
+ }
1799
+
1800
+ @Test func aSecondStopThenPlayReloads() throws {
1801
+ let (player, engine, _) = try makePlaying(live: true)
1802
+ player.stop()
1803
+ player.stop()
1804
+ engine.resolveAndLoadCalls.removeAll()
1805
+ player.play()
1806
+ #expect(engine.resolveAndLoadCalls.count == 1)
1807
+ }
1808
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "1.0.0-next.1",
3
+ "version": "1.0.0-next.2",
4
4
  "description": "React Native audio playback with a browsable media tree, CarPlay, Android Auto and lock-screen controls, built on Nitro Modules.",
5
5
  "keywords": [
6
6
  "android",