Skip to content

Commit

Permalink
[audio] Time measurements should all be in seconds (expo#33320)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjhughes authored Nov 29, 2024
1 parent 22c6897 commit 349ddb0
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export default function Player(props: Props) {
const _toggleShouldCorrectPitch = () =>
props.setRate(props.playbackRate, !props.shouldCorrectPitch);

const _seekForward = () => props.setPosition(props.currentTime + 5000);
const _seekForward = () => props.setPosition(props.currentTime + 5);

const _seekBackward = () => props.setPosition(Math.max(0, props.currentTime - 5000));
const _seekBackward = () => props.setPosition(Math.max(0, props.currentTime - 5));

const _renderReplayButton = () => {
return (
Expand Down Expand Up @@ -172,7 +172,7 @@ export default function Player(props: Props) {
}}
/>
<Text style={{ width: 100, textAlign: 'right' }} adjustsFontSizeToFit numberOfLines={1}>
{_formatTime(props.currentTime / 1000)} / {_formatTime(props.duration / 1000)}
{_formatTime(props.currentTime)} / {_formatTime(props.duration)}
</Text>
{_renderReplayButton()}
</View>
Expand Down
2 changes: 1 addition & 1 deletion docs/public/static/data/unversioned/expo-audio.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/expo-audio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Update docs and API so all units of time are returned as seconds, not milliseconds. ([#33320](https://github.com/expo/expo/pull/33320) by [@alanjhughes](https://github.com/alanjhughes))

### 💡 Others

## 0.2.4 — 2024-11-19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ class AudioModule : Module() {

Property("currentTime") { ref ->
runOnMain {
ref.player.currentPosition
ref.currentTime
}
}

Property("duration") { ref ->
runOnMain {
ref.player.duration
ref.duration
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ class AudioModule : Module() {
}

AsyncFunction("seekTo") { ref: AudioPlayer, seekTime: Double ->
ref.player.seekTo(seekTime.toLong())
ref.player.seekTo((seekTime * 1000L).toLong())
}.runOnQueue(Queues.MAIN)

Function("setPlaybackRate") { ref: AudioPlayer, rate: Float ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class AudioPlayer(
private var samplingEnabled = false
private var visualizer: Visualizer? = null

val currentTime get() = player.currentPosition / 1000
val duration get() = player.duration / 1000

init {
addPlayerListeners()
player.setAudioAttributes(AudioAttributes.DEFAULT, true)
Expand Down Expand Up @@ -107,12 +110,12 @@ class AudioPlayer(

return mapOf(
"id" to id,
"currentTime" to player.currentPosition,
"currentTime" to currentTime,
"playbackState" to playbackStateToString(player.playbackState),
"timeControlStatus" to if (player.isPlaying) "playing" else "paused",
"reasonForWaitingToPlay" to null,
"mute" to isMuted,
"duration" to player.duration,
"duration" to duration,
"playing" to player.isPlaying,
"loop" to isLooping,
"isLoaded" to if (player.playbackState == Player.STATE_ENDED) true else isLoaded,
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-audio/build/AudioModule.types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-audio/build/AudioModule.types.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/expo-audio/ios/AudioModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public class AudioModule: Module {
}

Property("currentTime") { player in
player.ref.currentItem?.currentTime().seconds
player.currentTime
}

Property("duration") { player in
if player.ref.status == .readyToPlay {
(player.ref.currentItem?.duration.seconds ?? 0.0) * 1000
player.duration
} else {
0.0
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public class AudioModule: Module {
AsyncFunction("seekTo") { (player: AudioPlayer, seconds: Double) in
await player.ref.currentItem?.seek(
to: CMTime(
seconds: seconds / 1000,
seconds: seconds,
preferredTimescale: CMTimeScale(NSEC_PER_SEC)
)
)
Expand Down
16 changes: 10 additions & 6 deletions packages/expo-audio/ios/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public class AudioPlayer: SharedRef<AVPlayer> {
private var samplingEnabled = false
private var tapInstalled = false

private var duration: Double {
(ref.currentItem?.duration.seconds ?? 0.0) * 1000
var duration: Double {
ref.currentItem?.duration.seconds ?? 0.0
}

var currentTime: Double {
ref.currentItem?.currentTime().seconds ?? 0.0
}

init(_ ref: AVPlayer, interval: Double) {
Expand Down Expand Up @@ -65,15 +69,15 @@ public class AudioPlayer: SharedRef<AVPlayer> {
}

func currentStatus() -> [String: Any] {
let duration = ref.status == .readyToPlay ? duration : 0.0
let currentDuration = ref.status == .readyToPlay ? duration : 0.0
return [
"id": id,
"currentTime": (ref.currentItem?.currentTime().seconds ?? 0) * 1000,
"currentTime": currentTime,
"playbackState": statusToString(status: ref.status),
"timeControlStatus": timeControlStatusString(status: ref.timeControlStatus),
"reasonForWaitingToPlay": reasonForWaitingToPlayString(status: ref.reasonForWaitingToPlay),
"mute": ref.isMuted,
"duration": duration,
"duration": currentDuration,
"playing": ref.timeControlStatus == .playing,
"loop": isLooping,
"isLoaded": ref.currentItem?.status == .readyToPlay,
Expand Down Expand Up @@ -183,7 +187,7 @@ public class AudioPlayer: SharedRef<AVPlayer> {
let interval = CMTime(seconds: updateInterval, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
timeToken = ref.addPeriodicTimeObserver(forInterval: interval, queue: nil) { time in
self.updateStatus(with: [
"currentTime": time.seconds * 1000
"currentTime": time.seconds
])
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-audio/src/AudioModule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export declare class AudioPlayer extends SharedObject<AudioEvents> {
isBuffering: boolean;

/**
* The current position through the audio item, in seconds.
* The current position through the audio item in seconds.
*/
currentTime: number;

Expand Down

0 comments on commit 349ddb0

Please sign in to comment.