Giter Site home page Giter Site logo

Comments (8)

bigtimebuddy avatar bigtimebuddy commented on May 29, 2024

Why is your build replacing new XMLHttpRequest with new Object?

from sound.

talhaozdemir avatar talhaozdemir commented on May 29, 2024

Because Facebook doesn't like the "XMLHttpRequest" string, so we replace it like that.

image

from sound.

bigtimebuddy avatar bigtimebuddy commented on May 29, 2024

In order to convert base64 string to an arraybuffer, you can do that without XMLHttpRequest. For instance: https://stackoverflow.com/a/21797381

The hacky solution is to override _loadUrl to have it just work.

A better option is to just pass the ArrayBuffer to the sound library:

import { sound } from '@pixi/sound';
import url from './mysound.mp3';

const arrayBuffer = base64toArrayBuffer(url);
sound.add('mysound', arrayBuffer);

from sound.

talhaozdemir avatar talhaozdemir commented on May 29, 2024

Thanks for the example.
I tried like the code above, asset is loaded but when I try to play sound I get error like

image

Here is my load function;

  loadSound(key, src, json, callback) {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
    for (var i = 0; i < chars.length; i++) {
      lookup[chars.charCodeAt(i)] = i;
    }

    var decode = function (base64) {
      var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
      if (base64[base64.length - 1] === '=') {
        bufferLength--;
        if (base64[base64.length - 2] === '=') {
          bufferLength--;
        }
      }
      var arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
      for (i = 0; i < len; i += 4) {
        encoded1 = lookup[base64.charCodeAt(i)];
        encoded2 = lookup[base64.charCodeAt(i + 1)];
        encoded3 = lookup[base64.charCodeAt(i + 2)];
        encoded4 = lookup[base64.charCodeAt(i + 3)];
        bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
        bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
        bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
      }
      return arraybuffer;
    };

    const arrayBuffer = decode(src);
    sound.add(key, arrayBuffer);
    console.log(arrayBuffer);
    sound._sounds[key].addSprites(json.spritemap);
    console.log(sound._sounds[key]);
    callback();
  }

from sound.

talhaozdemir avatar talhaozdemir commented on May 29, 2024

I don't know that it is important or not but when I console the arrayBuffer with delay, the byteLength of it is 0.

image

from sound.

bigtimebuddy avatar bigtimebuddy commented on May 29, 2024

This worked for me:
https://codesandbox.io/s/pixijs-sound-base64-example-vj6xcj?file=/src/index.ts

from sound.

talhaozdemir avatar talhaozdemir commented on May 29, 2024

After adding the line

const data = str.split(",").slice(1).join(",");

It is working for me too. Thank you so much.

from sound.

bigtimebuddy avatar bigtimebuddy commented on May 29, 2024

Yeah you need to chop off the mime type and just interpret the data. Glad it works!

from sound.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.