Is there any possibility to control HTML5 audio volume on IOS?

拈花ヽ惹草 提交于 2019-11-29 07:52:30

I always thought dynamic volume control and certainly crossfading, as we all know, is impossible. Apple has dictated that it be so. Except.... apparently the folk at Kodo Games (specifically Nicola Hibbert?) have pulled it off and made the code available to anyone. I just checked it on iOS 8. Fading and crossfading seem to work!

Check it out (new site that works): Getting Started with Web Audio API

To add volume change support to iOS and not rewrite your entire app from html5 audio to web audio for that, you can combine these audio solutions via createMediaElementSource.

Please see more details at Mozilla website https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createMediaElementSource

I worked around this by simply stopping all sounds and setting a variable isMuted to true or false, so I can check this everywhere a sound is played:

// set the variable
var isMuted = false;

// toggle audio
function toggleMute() {

    var toggleAudioBtn = $(".toggleAudio");

        if (isMuted == false) {
            sounds.stop();
            isMuted = true;
            toggleAudioBtn.css("background-image", "url('images/ui/btn_audio_mute.png')");
        } else {
            isMuted = false;
            toggleAudioBtn.css("background-image", "url('images/ui/btn_audio.png')");
        }

};

// for every sound check if sound is muted
if(isMuted == false) {
    sound.play();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!