Skip to content

Commit bf5c4ea

Browse files
authored
Fix unmuting through setDocumentState. (ampproject#29536)
1 parent b3a6723 commit bf5c4ea

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

extensions/amp-story/1.0/amp-story-page.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ export class AmpStoryPage extends AMP.BaseElement {
495495
this.pauseAllMedia_(true /** rewindToBeginning */);
496496
}
497497

498+
if (!this.storeService_.get(StateProperty.MUTED_STATE)) {
499+
this.muteAllMedia();
500+
}
501+
498502
if (this.animationManager_) {
499503
this.animationManager_.cancelAll();
500504
}
@@ -525,7 +529,12 @@ export class AmpStoryPage extends AMP.BaseElement {
525529
this.startMeasuringAllVideoPerformance_();
526530
this.preloadAllMedia_()
527531
.then(() => this.startListeningToVideoEvents_())
528-
.then(() => this.playAllMedia_());
532+
.then(() => {
533+
this.playAllMedia_();
534+
if (!this.storeService_.get(StateProperty.MUTED_STATE)) {
535+
this.unmuteAllMedia();
536+
}
537+
});
529538
});
530539
this.prefersReducedMotion_()
531540
? this.maybeFinishAnimations_()

extensions/amp-story/1.0/amp-story.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,11 +1546,6 @@ export class AmpStory extends AMP.BaseElement {
15461546
if (!oldPage) {
15471547
this.registerAndPreloadBackgroundAudio_();
15481548
}
1549-
1550-
if (!this.storeService_.get(StateProperty.MUTED_STATE)) {
1551-
oldPage && oldPage.muteAllMedia();
1552-
this.activePage_.unmuteAllMedia();
1553-
}
15541549
},
15551550
// Third and last step contains all the actions that can be delayed after
15561551
// the navigation happened, like preloading the following pages, or

extensions/amp-story/1.0/test/test-amp-story-page.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
import {Action, AmpStoryStoreService} from '../amp-story-store-service';
1718
import {AmpDocSingle} from '../../../../src/service/ampdoc-impl';
1819
import {AmpStoryPage, PageState, Selectors} from '../amp-story-page';
19-
import {AmpStoryStoreService} from '../amp-story-store-service';
2020
import {Deferred} from '../../../../src/utils/promise';
2121
import {LocalizationService} from '../../../../src/service/localization';
2222
import {MediaType} from '../media-pool';
@@ -34,6 +34,7 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
3434
let element;
3535
let gridLayerEl;
3636
let page;
37+
let storeService;
3738
let isPerformanceTrackingOn;
3839

3940
const nextTick = () => new Promise((resolve) => win.setTimeout(resolve, 0));
@@ -55,7 +56,7 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
5556
.stub(Services, 'localizationForDoc')
5657
.returns(localizationService);
5758

58-
const storeService = new AmpStoryStoreService(win);
59+
storeService = new AmpStoryStoreService(win);
5960
registerServiceBuilder(win, 'story-store', function () {
6061
return storeService;
6162
});
@@ -207,6 +208,34 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
207208
});
208209
});
209210

211+
it('should unmute audio when state becomes active', (done) => {
212+
env.sandbox.stub(page, 'loadPromise').returns(Promise.resolve());
213+
214+
storeService.dispatch(Action.TOGGLE_MUTED, false);
215+
216+
const videoEl = win.document.createElement('video');
217+
videoEl.setAttribute('src', 'https://example.com/video.mp3');
218+
gridLayerEl.appendChild(videoEl);
219+
220+
let mediaPoolMock;
221+
222+
page.buildCallback();
223+
page
224+
.layoutCallback()
225+
.then(() => page.mediaPoolPromise_)
226+
.then((mediaPool) => {
227+
mediaPoolMock = env.sandbox.mock(mediaPool);
228+
mediaPoolMock.expects('unmute').once();
229+
230+
page.setState(PageState.PLAYING);
231+
232+
win.requestAnimationFrame(() => {
233+
mediaPoolMock.verify();
234+
done();
235+
});
236+
});
237+
});
238+
210239
it('should perform media operations on fie video when active', (done) => {
211240
const iframe = win.document.createElement('iframe');
212241
const fiePromise = installFriendlyIframeEmbed(iframe, gridLayerEl, {
@@ -394,6 +423,32 @@ describes.realWin('amp-story-page', {amp: true}, (env) => {
394423
});
395424
});
396425

426+
it('should mute audio when state becomes active', (done) => {
427+
storeService.dispatch(Action.TOGGLE_MUTED, false);
428+
429+
const videoEl = win.document.createElement('video');
430+
videoEl.setAttribute('src', 'https://example.com/video.mp3');
431+
gridLayerEl.appendChild(videoEl);
432+
433+
let mediaPoolMock;
434+
435+
page.buildCallback();
436+
page
437+
.layoutCallback()
438+
.then(() => page.mediaPoolPromise_)
439+
.then((mediaPool) => {
440+
mediaPoolMock = env.sandbox.mock(mediaPool);
441+
mediaPoolMock.expects('mute').withExactArgs(videoEl).once();
442+
443+
page.setState(PageState.NOT_ACTIVE);
444+
445+
win.requestAnimationFrame(() => {
446+
mediaPoolMock.verify();
447+
done();
448+
});
449+
});
450+
});
451+
397452
it('should stop the advancement when state becomes paused', async () => {
398453
page.buildCallback();
399454
const advancementStopStub = env.sandbox.stub(page.advancement_, 'stop');

extensions/amp-story/1.0/test/test-amp-story.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,30 +1030,6 @@ describes.realWin(
10301030
expect(unmuteStub).not.to.have.been.called;
10311031
expect(playStub).not.to.have.been.called;
10321032
});
1033-
1034-
it('should mute the page and unmute the next page upon navigation', async () => {
1035-
await createStoryWithPages(4, [
1036-
'cover',
1037-
'page-1',
1038-
'page-2',
1039-
'page-3',
1040-
]);
1041-
1042-
story.storeService_.dispatch(Action.TOGGLE_MUTED, false);
1043-
1044-
await story.layoutCallback();
1045-
const coverMuteStub = env.sandbox.stub(
1046-
story.getPageById('cover'),
1047-
'muteAllMedia'
1048-
);
1049-
const firstPageUnmuteStub = env.sandbox.stub(
1050-
story.getPageById('page-1'),
1051-
'unmuteAllMedia'
1052-
);
1053-
await story.switchTo_('page-1');
1054-
expect(coverMuteStub).to.have.been.calledOnce;
1055-
expect(firstPageUnmuteStub).to.have.been.calledOnce;
1056-
});
10571033
});
10581034

10591035
it('should remove the muted attribute on unmuted state change', async () => {

0 commit comments

Comments
 (0)