Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

probable fix for the notification seek #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "audio_x",
"version": "1.0.5",
"version": "1.0.6-0",
"description": "The audio player for the gen-x",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import ChangeNotifier from 'helpers/notifier';

import {
attachMediaSessionHandlers,
updateMetaData
resetPositionState,
updateMetaData,
updatePositionState
} from 'mediasession/mediasessionHandler';
import { READY_STATE } from 'states/audioState';
import {
Expand Down Expand Up @@ -159,6 +161,7 @@ class AudioX {
});

updateMetaData(mediaTrack);
resetPositionState();
audioInstance.load();
}

Expand All @@ -185,6 +188,7 @@ class AudioX {
.play()
.then(() => {
console.log('PLAYING');
updatePositionState();
})
.catch(() => {
console.warn('cancelling current audio playback, track changed');
Expand Down
8 changes: 8 additions & 0 deletions src/events/baseEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
getReadableErrorMessage
} from 'helpers/common';
import ChangeNotifier from 'helpers/notifier';
import {
resetPositionState,
updatePositionState
} from 'mediasession/mediasessionHandler';
import { AudioState, EventListenerCallbackMap } from 'types';
import { ERROR_EVENTS } from './errorEvents';

Expand All @@ -21,6 +25,7 @@ const BASE_EVENT_CALLBACK_MAP: EventListenerCallbackMap = {
},
`audiox_baseEvents_state_state_${e.type}`
);
resetPositionState();
},

DURATION_CHANGE: (e, audioInstance: HTMLAudioElement) => {
Expand Down Expand Up @@ -51,6 +56,7 @@ const BASE_EVENT_CALLBACK_MAP: EventListenerCallbackMap = {
},
`audiox_baseEvents_state_state_${e.type}`
);
updatePositionState();
},

LOADED_DATA: (e, audioInstance: HTMLAudioElement) => {
Expand Down Expand Up @@ -187,6 +193,8 @@ const BASE_EVENT_CALLBACK_MAP: EventListenerCallbackMap = {
},
`audiox_baseEvents_state_${e.type}`
);
resetPositionState();
updatePositionState();
},

WAITING: (e: Event, audioInstance: HTMLAudioElement) => {
Expand Down
37 changes: 27 additions & 10 deletions src/mediasession/mediasessionHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AudioX } from 'audio';
import { metaDataCreator } from 'helpers/common';
import { isValidWindow, metaDataCreator } from 'helpers/common';
import ChangeNotifier from 'helpers/notifier';
import { AudioState } from 'types';

Expand All @@ -24,13 +24,30 @@ export const attachMediaSessionHandlers = () => {
};

export const updatePositionState = () => {
ChangeNotifier.listen('AUDIO_X_STATE', (data: AudioState) => {
if (data?.duration && data?.playbackRate && data?.progress) {
navigator.mediaSession.setPositionState({
duration: data.duration,
playbackRate: data.playbackRate,
position: data.progress
});
}
});
if ('setPositionState' in navigator.mediaSession) {
const audioState = ChangeNotifier.getLatestState(
'AUDIO_X_STATE'
) as AudioState;
const { currentTime, duration } = AudioX.getAudioInstance();

navigator.mediaSession.setPositionState({
duration: duration,
playbackRate: audioState.playbackRate,
position: currentTime
});
}
};

export const resetPositionState = () => {
if (isValidWindow && 'setPositionState' in navigator.mediaSession) {
// Reset position state when media is reset.
const { duration } = AudioX.getAudioInstance();
console.log('reseting position state');
console.log({ duration });
navigator.mediaSession.setPositionState({
position: 0.0,
duration: 0.0,
playbackRate: 1
});
}
};