Skip to content

Commit

Permalink
Enable loading of MP3 DataURL in PIXI.Assets (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
zprodev authored May 16, 2023
1 parent 8d93b9c commit 702da5b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/soundAsset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { utils, extensions, ExtensionType } from '@pixi/core';
import { AssetExtension, LoadAsset, LoaderParser, LoaderParserPriority } from '@pixi/assets';
import { supported, extensions as exts } from './utils/supported';
import { supported, extensions as exts, mimes } from './utils/supported';
import { Options, Sound } from './Sound';
import { getInstance } from './instance';

Expand Down Expand Up @@ -33,7 +33,7 @@ const soundAsset = {
{
const ext = utils.path.extname(url).slice(1);

return !!supported[ext];
return !!supported[ext] || mimes.some((mime) => url.startsWith(`data:${mime}`));
},

/** Load the sound file, this is mostly handled by Sound.from() */
Expand Down
8 changes: 7 additions & 1 deletion src/utils/supported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const extensions: string[] = [
'caf',
];

const mimes: string[] = [
'audio/mpeg',
'audio/ogg',
];

/**
* The list of browser supported audio formats.
* @readonly
Expand Down Expand Up @@ -74,5 +79,6 @@ validateFormats();
export {
validateFormats,
supported,
extensions
extensions,
mimes,
};
7 changes: 7 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Assets } from 'pixi.js';
import { sound, Sound, utils, webaudio, htmlaudio, filters, SoundLibrary, IMediaInstance } from '../src';
import { dataUrlMp3, dataUrlOgg } from './resources/dataUrlResources';
import path from 'path';

declare global
Expand All @@ -15,7 +16,10 @@ const manifest: Record<string, string> = {
'alert-7': path.join(__resources, 'alert-7.mp3'),
'alert-12': path.join(__resources, 'alert-12.mp3'),
'musical-11': path.join(__resources, 'musical-11.mp3'),
'alert-4-ogg': path.join(__resources, 'alert-4.ogg'),
silence: path.join(__resources, 'silence.mp3'),
dataUrlMp3,
dataUrlOgg
};

// Import the library
Expand Down Expand Up @@ -90,7 +94,10 @@ export function suite(useLegacy = false): void
expect(results['alert-7']).toBeInstanceOf(Sound);
expect(results['alert-12']).toBeInstanceOf(Sound);
expect(results['musical-11']).toBeInstanceOf(Sound);
expect(results['alert-4-ogg']).toBeInstanceOf(Sound);
expect(results.silence).toBeInstanceOf(Sound);
expect(results.dataUrlMp3).toBeInstanceOf(Sound);
expect(results.dataUrlOgg).toBeInstanceOf(Sound);
});

it('should get a reference by alias', () =>
Expand Down
Binary file added test/resources/alert-4.ogg
Binary file not shown.
Loading

0 comments on commit 702da5b

Please sign in to comment.