2013-02-28 61 views
0

以下代碼適用於我的桌面瀏覽器,但不適用於我的iPhone上的Safari,它顯示alert(),但不會播放音頻。我究竟做錯了什麼?SoundManager 2和iPhone

soundManager.url = '/assets/backend/swf/'; 
soundManager.preferFlash = false; 
soundManager.useHTML5Audio = true; 
soundManager.onready(function(){ 
    function playSong(){ 
     soundManager.createSound({ 
      id: 'song_<?=$song->ID?>', 
      url: '/backend/song/play/<?=$song->ID?>', 
      type: 'audio/mp3' 
     }).play(); 
    } 

    $('#play').click(function(){ 
     alert('playSong()'); 
     playSong(); 
    }); 
}); 

回答

0

從我看到的,iPhone不喜歡「自動播放」的聲音,他們需要聲音從用戶交互播放。

就你而言,iOS瀏覽器一定不會喜歡你創造聲音然後馬上播放它的事實。

給這個變種試一試,看看它是否適合你:

soundManager.url = '/assets/backend/swf/'; 
soundManager.preferFlash = false; 
soundManager.useHTML5Audio = true; 
soundManager.onready(function(){ 
    // Create the sounds here (don't call play) 
    soundManager.createSound({ 
     id: 'song_<?=$song->ID?>', // It is not cool to put PHP here, read below! 
     url: '/backend/song/play/<?=$song->ID?>', 
     type: 'audio/mp3' 
    }); 

    $('#play').click(function(){ 
     var soundId = 'song_<?= $song->ID ?>'; 
     soundManager.play(soundId); 
    }); 
}); 

作爲一個方面說明:我建議你反對這種方式混合JS和PHP。檢查如何「擴大」這個SoundManager2例如像這樣的普通MP3鏈接:

<a href="blabla.mp3">Click here to play blabla</a> 

弄成的onclick播放MP3: SoundManager2 documentation

的問候,祝你好運!