Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.4k stars 976 forks source link

iOS Problem #1056

Closed jramassamy closed 2 years ago

jramassamy commented 2 years ago

Hi, I've problems with iOS using Tone.js (version is 14.7.77, the latest)

https://piano-as-a-game.herokuapp.com/

It'll be used to train french piano learners so I'm trying to have every devices possible checked.

So, here is my code, when I click on my button (touchEvents included), I set everything in my soundAuthorized() function :

async soundAuthorized() {
    this.sound = !this.sound;
    this.pianoSong = new Tone.Sampler({
      urls: {
        "C4": "C4.mp3",
        "D#4": "Ds4.mp3",
        "F#4": "Fs4.mp3",
        "A4": "A4.mp3",
      },
      release: 1,
      baseUrl: "https://tonejs.github.io/audio/salamander/",
    }).toDestination();
    Tone.context.resume();
    await Tone.start();
    this.toneLoadingState = 'LOADED';
    console.log('audio is ready');
  }

And after that I play my random notes.

Working on every devices except iOs, what did I forgot ?

jramassamy commented 2 years ago

Finally, I've used this library: unmute

Added the .js file in index.html with: <script src="./assets/unmute.js"></script> Then in my file: declare var unmute: any; and I call the code writed behind "usage" on page load & after click (to be sure).

MichaelShingo commented 6 months ago

I had the exact same problem, Tone.js doesn't play any sound on the newer iPhones and iPads when the device is muted. My solution was to play a silent mp3 on user interaction, using a native HTML audio element to activate the sound. I do this before playing the sound that I actually want to hear.

    <audio>
        <source src="/silent.mp3" type="audio/mp3"></source>
    </audio>

You can get the audio element to start playing on user interaction. So select the audio element, and call it's play() method right before starting the audio you actually want to hear.

audioElement.play(); I tested on the following devices on LambdaTest (highly recommended if you don't own an iOS device):

iPhone 8 13.4 iPhone 12 17.2 iPhone 13 Mini 17.2 iPhone 14 17.2 iPhone 15 17.2