这是我的代码:
$(document).on('click', 'div.play',function () {
play = $(this);
var stop = $('.stop');
contentId = play.attr('id');
nexttrack = nexttrackPlay(play);
if(!play.parent().hasClass('stop')) {
if(song ==undefined){
initAudio(play);
}
else
{
song.src = play.attr('audiourl');
song.load();
}
playerInit(play);
stop.each(function () {
$(this).removeClass('stop');
});
playAudio();
}
else{
if(song.paused){
playAudio();
}
else{
stopAudio();
}
}
});
function initAudio(elem) {
var url = elem.attr('audiourl');
song = document.getElementsByTagName('audio')[0];
console.log(song);
song.src = url;
song.preload = 'metadata';
song.load();
song.addEventListener('play', function () {
$('.play-play').addClass('stop');
play.parent().addClass('stop');
play.removeClass('pause');
$('.button-play').addClass('stop');
song.removeEventListener('play',function(){ });
},false);
song.addEventListener('pause', function () {
play.addClass('pause');
$('.play-play').removeClass('stop');
$('.button-play').removeClass('stop');
song.removeEventListener('play',function(){ });
},false);
}
function playAudio() {
song.load();
song.play();
}
function stopAudio() {
song.pause();
}
在Android和台式机上可以完美运行,但是在IOS上,首先单击不起作用,当单击另一首歌曲,然后再次单击时,它将开始播放。
最佳答案
我发现问题和解决方案)
一切都在标题中,我发送了Content-type:application / octet-stream标题,这是IOS的问题,然后将其更改为audio / mpeg并开始加载音频。
关于ios - iOS 10移动audio.play()工作不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46093019/