Skip to content

Commit

Permalink
fix: [#2599] Update Sound canPlayFile to handle querystrings (#2600)
Browse files Browse the repository at this point in the history
===:clipboard: PR Checklist :clipboard:===

- [x] :pushpin: issue exists in github for these changes
- [x] :microscope: existing tests still pass
- [x] :see_no_evil: code conforms to the [style guide](https://github.com/excaliburjs/Excalibur/blob/main/STYLEGUIDE.md)
- [x] 📐 new tests written and passing / old tests updated with new scenario(s)
- [x] 📄 changelog entry added (or not needed)

==================

Closes #2599

## Changes:

- Update regex parsing sound paths to determine audio support
  • Loading branch information
eonarheim authored Mar 11, 2023
1 parent 51c21e9 commit af4a72c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ are returned

### Fixed

- Fixed issue where `ex.Sound` would get confused parsing and playing sound files with a querystring in their path
- Fixed issue where `ex.ColliderComponent` was not deeply cloning the stored `ex.Collider` causing them to be shared across clones.
- Fixed issue where `ex.GraphicsComponent` was not deeploy cloning the
stored `ex.Graphics` causing them to be shared across clones.
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Util/Sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Logger } from './Log';
export function canPlayFile(file: string): boolean {
try {
const a = new Audio();
const filetype = /.*\.([A-Za-z0-9]+)$/;
const filetype = /.*\.([A-Za-z0-9]+)(?:(?:\?|\#).*)*$/;
const type = file.match(filetype)[1];
if (a.canPlayType('audio/' + type)) {
return true;
Expand Down
25 changes: 25 additions & 0 deletions src/spec/SoundSpec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ex from '@excalibur';
import { canPlayFile } from '../engine/Util/Sound';
import { delay } from '../engine/Util/Util';
import { WebAudio } from '../engine/Util/WebAudio';
import { TestUtils } from './util/TestUtils';
Expand All @@ -20,6 +21,30 @@ describe('Sound resource', () => {
expect(sut).toBeDefined();
});

it('can detect playability of files', () => {
expect(canPlayFile('coin.mp3')).toBe(true);
});

it('can detect playability of files', () => {
expect(canPlayFile('coin.mp3?12234')).toBe(true);
});

it('can detect playability of files with multiple dots', () => {
expect(canPlayFile('coin.f74d9d70.mp3')).toBe(true);
});

it('can detect playability of files with querystrings', () => {
expect(canPlayFile('coin.f74d9d70.mp3?1678266496281')).toBe(true);
});

it('can detect playability of files with hash', () => {
expect(canPlayFile('coin.f74d9d70.mp3#1678266496281')).toBe(true);
});

it('can detect playability of files with querystring and hash', () => {
expect(canPlayFile('coin.f74d9d70.mp3?_=1234#1678266496281')).toBe(true);
});

it('should fire processed event', async () => {
const processedSpy = jasmine.createSpy('fake');
sut.once('processed', processedSpy);
Expand Down

0 comments on commit af4a72c

Please sign in to comment.