Skip to content

Commit 402a17f

Browse files
committed
feat: Add sound-position-timestamp helper to get current time from HLS streams, or approximate the time from startsAt if currentTime is not available
1 parent 8accf39 commit 402a17f

File tree

8 files changed

+260
-889
lines changed

8 files changed

+260
-889
lines changed

.github/actions/assert-build/action.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ runs:
3535
helpers/sound-is-seekable.js
3636
helpers/sound-metadata.js
3737
helpers/sound-position.js
38+
helpers/sound-position-timestamp.js
3839
helpers/stereo-volume-is-adjustable.js
3940
helpers/stereo-volume.js
4041
helpers/stop-sound.js

docs/app/components/docs/custom-position-control.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import Component from '@glimmer/component';
22
import { action } from '@ember/object';
33
import { tracked } from '@glimmer/tracking';
44
export default class DocsCustomPositionControlComponent extends Component {
5-
65
@tracked lastSelectedPosition;
76

87
@action
9-
onChangePosition(position, event) {
8+
onChangePosition(position /* event */) {
109
this.lastSelectedPosition = position;
1110
}
1211
}

docs/app/components/docs/manual-position-control.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Component from '@glimmer/component';
22
import { action } from '@ember/object';
33
import { tracked } from '@glimmer/tracking';
44
export default class DocsCustomPositionControlComponent extends Component {
5-
65
@tracked lastSelectedPosition;
76

87
@action

docs/app/components/docs/queued.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default class Queued extends Component {
1414
this.queue = [
1515
'/sounds/attention.mp3',
1616
'/sounds/internet-on-computers.mp3',
17-
'https://streaming.koop.org/stream.aac';,
17+
'https://streaming.koop.org/stream.aac',
1818
];
1919

2020
let sound = await this.playNext();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import StereoBaseIsHelper from '../-private/helpers/is-helper';
2+
import { add } from 'date-fns';
3+
4+
/**
5+
A helper to get a sound's position timestamp based on the playingDate property in an HLS file, or from the position + a startsAt parameter.
6+
```hbs
7+
8+
this.startsAt = new Date('2024-10-01T00:00:00Z');
9+
{{sound-position-timestamp @identifier}} #=> playingDate property from HLS
10+
{{sound-position-timestamp @identifier startsAt=this.startsAt}} #=> new Date('2024-10-01T00:00:00Z')
11+
```
12+
13+
@class {{sound-position-timestamp}}
14+
@type {Helper}
15+
*/
16+
17+
/**
18+
@method compute
19+
@param {Any} identifier url, urls, url objects, promise that resolves to a url
20+
@param {Float} position
21+
@param {Date} currentTime
22+
@param {Date} startsAt
23+
@return {Date}
24+
*/
25+
export default class SoundPositionTimestamp extends StereoBaseIsHelper {
26+
name = 'sound-position-timestamp';
27+
28+
get result() {
29+
let result;
30+
let position = this.options?.position || this.sound?.position || 0;
31+
let currentTime = this.sound?.currentTime;
32+
let startsAt = this.options?.startsAt;
33+
34+
if (startsAt && !this.sound?.isLoaded) {
35+
return new Date(startsAt);
36+
}
37+
38+
if (currentTime) {
39+
result = new Date(currentTime);
40+
} else if (startsAt) {
41+
result = add(new Date(startsAt), {
42+
seconds: position / 1000,
43+
});
44+
}
45+
46+
if (result instanceof Date && !isNaN(result)) {
47+
return result;
48+
} else {
49+
return undefined;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)