2011-10-27 126 views
3

我想包含用戶滾動時會自動播放的音頻。我還沒有找到可以做到這一點的工具提示。有誰知道一種方法來完成這個?工具提示/懸停中的音頻?

(注:用戶將被警告的聲音,他們可以訪問頁面之前)

UPDATE

我得到這個工作的感謝Bakudan - ханювиги。但是有沒有使用Bakudan - ханювиги的方法可用的閃回?謝謝!

更新2

使用Bakudan - ханювиги對使用SWFObject添加Flash後備推薦的方法讓我有點糊塗了。我缺乏javascript知識是我迷失的地方。下面是我使用我的音頻代碼:

<script> 

// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com) 
// Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code 

var html5_audiotypes={ //define list of audio file extensions 
"mp3": "audio/mpeg", 
"ogg": "audio/ogg", 
} 

function createsoundbite(sound){ 
var html5audio=document.createElement('audio') 
if (html5audio.canPlayType){ //check support for HTML5 audio 
    for (var i=0; i<arguments.length; i++){ 
     var sourceel=document.createElement('source') 
     sourceel.setAttribute('src', arguments[i]) 
     if (arguments[i].match(/\.(\w+)$/i)) 
      sourceel.setAttribute('type', html5_audiotypes[RegExp.$1]) 
     html5audio.appendChild(sourceel) 
    } 
    html5audio.load() 
    html5audio.playclip=function(){ 
     html5audio.pause() 
     html5audio.currentTime=0 
     html5audio.play() 
    } 
    return html5audio 
} 
else{ 
    return {playclip:function(){throw new Error(
     "Your browser doesn't support HTML5 audio unfortunately")}} 
} 
} 

//Initialize sound clips with 1 fallback file each: 

var mouseoversound=createsoundbite(
    "/messages4u/2011/images/october/laugh.ogg",  
    "/messages4u/2011/images/october/laugh.mp3") 

</script> 

我換了別的閃存而不是錯誤消息。如何使用swfobject來更改此播放Flash音頻文件?我有點失落了。

感謝您的幫助!

+0

我希望你在開玩笑吧! –

+0

@Tomalak Geret'kal - 沒有,根本不是在開玩笑。這將是一個特殊的頁面,而不是一般的訪問。除非他們選擇進入。聽起來有點嚇人......萬聖節的呻吟聲和尖叫聲以及ghoslty的聲音,不知道嗎? – L84

+0

什麼問題?難道你只是將JS函數綁定到播放音頻的mouseover事件?你在哪裏遇到麻煩? – maxedison

回答

2

This是一個良好的開端。我做了一個demo。如果音頻時間稍長一些,請在createoundbite函數中添加stopclip函數,然後將其添加到.mouseleave

編輯:

添加閃存更改else部分。我很熟悉的閃光燈,但基本上使用下列之一:

  • create object - 「使用document.createElement(」對象「); ......等等」
  • 或我認爲這是更好地利用swfobject
+0

太棒了!我有它的工作。 - 使用IE8或更低版本的用戶是否可以使用閃回回退功能? – L84

+0

另一個問題 - 如何在懸停時播放多個聲音。 IE此項目在懸停時有一個聲音。這個項目有另一個懸停聲音。我不確定如何做到這一點。 – L84

+0

非常感謝您的幫助。我用另一個問題更新了我的問題。如果你想我可以把這個問題與我的原始問題分開。但是,如何使用swfobject將閃光燈添加到它?我有點困惑,因爲這是如何完成的。 – L84

2

下面是播放聲音在舊的瀏覽器,並且不需要閃存版本:

var sound = document.createElement("audio"); 
if (!("src" in sound)) { 
    sound = document.createElement("bgsound"); 
} 
document.body.appendChild(sound); 

function playSound(src) { 
    sound.src = src; 
    sound.play && sound.play(); 
} 

... 

playSound("/sounds/something.mp3"); 

編輯:下面是一個使用.hover()當鼠標懸停在你的元素來播放聲音的一個版本:

$(function) { 
    var sound = document.createElement("audio"); 
    if (!("src" in sound)) { 
     sound = document.createElement("bgsound"); 
    } 
    document.body.appendChild(sound); 

    $(".playable").hover(function() { 
     sound.src = this.soundFile; 
     sound.play && sound.play(); 
    }, function() { 
     sound.src = ""; 
    }); 
}); 

設置要播放聲音有一類「可玩」和自定義屬性「音效檔」你的元素包含聲音文件的源url:

<span class="playable" soundFile="/sounds/something.mp3">Something</span> 
<span class="playable" soundFile="/sounds/somethingElse.mp3">Something else</span> 
+0

我會試一試謝謝=> – L84

+0

我試圖讓這個工作,但我有點困惑。如何在鼠標懸停時觸發此操作?另外我怎樣才能每頁添加多個聲音? – L84

+0

@Lynda - 對不起,延遲迴復 - 顯然我爲時太晚,無法幫助你渡過這個萬聖節。不過,我用一些代碼更新了我的答案,這些代碼會在鼠標移過元素時觸發聲音。 – gilly3