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

Enable loading of MP3 DataURL in PIXI.Assets #244

Merged
merged 7 commits into from
May 16, 2023
Merged
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 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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, could you add audio/ogg too?

'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