2017-02-22 5 views
0

Я использую эту функцию для создания звука, который хорошо работает на настольном компьютере и Android, и работает изначально на iOS, когда я использую touchhevent, чтобы запустить его. Мне нужно позже заменить звук другим звуковым файлом, однако на iOS он не запускается - я предполагаю, потому что для воспроизведения звука требуется другое взаимодействие с пользователем.Измените звук в WebAudioAPI без взаимодействия с пользователем на iOS

Это приложение VR в гарнитуре, поэтому такое взаимодействие с пользователем невозможно. Есть ли другой способ замены звука или другого взаимодействия с пользователем без клика, которое я могу использовать как движение?

Я видел это http://matt-harrison.com/perfect-web-audio-on-ios-devices-with-the-web-audio-api/

который, кажется, есть другое решение, но я не хочу, чтобы предварительно загрузить все файлы (они достаточно большой и есть 10 из них), который, кажется, быть здесь требованием - плюс я использую функцию паузы в коде, который у меня есть. Есть ли какие-то легкие способы обойти это?

var AudioContext = AudioContext || webkitAudioContext, context = new AudioContext(); 

function createSound(filename) { 
console.log('createSound()'); 

var url = cdnPrefix + '/' + filename; 
var buffer; 


context = new AudioContext(); 

var request = new XMLHttpRequest(); 
request.open('GET', url, true); 
request.responseType = 'arraybuffer'; 

// Decode asynchronously 
request.onload = function() { 
    context.decodeAudioData(request.response, function(b) { 
     buffer = b; 
     play(); 
    }); 
} 
request.send(); 


var sourceNode = null, 
    startedAt = 0, 
    pausedAt = 0, 
    playing = false, 
    volume = context.createGain(); 

var play = function() { 

    if(playing || !buffer) 
     return; 

    var offset = pausedAt; 

    sourceNode = context.createBufferSource(); 
    sourceNode.connect(context.destination); 
    sourceNode.connect(volume); 
    volume.gain.value = 1; 

    sourceNode.buffer = buffer; 
    sourceNode.start(0, offset); 
    sourceNode.onended = onEnded; 

    sourceNode.onstatechange = onStateChange; 
    sourceNode.onloaded = onLoaded; 
    //sourceNode.loop = true; 
    startedAt = context.currentTime - offset; 
    pausedAt = 0; 
    playing = true; 
    $(document).trigger("voiceoverPlay"); 

    if(isPaused == true) 
     pause(); 
}; 

function onEnded(event){ 
    $(document).trigger("voiceoverEnded"); 
    play(); 
} 

function onStateChange(event){ 
    console.log('onStateChange',event); 
} 

function onLoaded(event){ 
    console.log('onLoaded',event); 
} 


var pause = function() { 
    var elapsed = context.currentTime - startedAt; 
    stop(); 
    pausedAt = elapsed; 
    $(document).trigger("voiceoverPause"); 
}; 

var stop = function() { 
    if (sourceNode) { 
     sourceNode.disconnect(); 
     if(playing === true) 
      sourceNode.stop(0); 
     sourceNode = null; 
    } 
    pausedAt = 0; 
    startedAt = 0; 
    playing = false; 
}; 

var getPlaying = function() { 
    return playing; 
}; 

var getCurrentTime = function() { 
    if(pausedAt) { 
     return pausedAt; 
    } 
    if(startedAt) { 
     return context.currentTime - startedAt; 
    } 
    return 0; 
}; 

var setCurrentTime = function(time) { 
    pausedAt = time; 
}; 

var getDuration = function() { 
    return buffer.duration; 
}; 

return { 
    getCurrentTime: getCurrentTime, 
    setCurrentTime: setCurrentTime, 
    getDuration: getDuration, 
    getPlaying: getPlaying, 
    play: play, 
    pause: pause, 
    stop: stop 
}; 

}

ответ

0

Вам нужно сенсорное событие для каждого звука.

В итоге я использовал SoundJS, который намного лучше.