Opera中的声音通知 [英] Sound notifications in Opera

查看:138
本文介绍了Opera中的声音通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Opera 12.16(最新版本)和音频元素社区/论坛/ topic.dml?id = 1582462rel =nofollow>我注意到它不适用于 mp3 文件,但它与 ogg 那些。

I am trying to play an audio element in Opera 12.16 (the latest version right now) and I noticed it doesn't work with mp3 files but it does with ogg ones.

这是我的小提琴,目前只有mp3文件,不适用于Opera:
< a href =http://jsfiddle.net/hMnsd/1/ =nofollow> http://jsfiddle.net/hMnsd/1/

Here's my fiddle currently only with the mp3 file and NOT working on Opera: http://jsfiddle.net/hMnsd/1/

解决方案似乎做了类似的事情(如 w3c网站所述) :

The solution seem to do something like this (as pointed in w3c website):

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio tag.
</audio>

我现在正在使用此功能来生成声音:

I am using now this function to generate the sound:

function notificationSound(){
    var audioElement = document.createElement('audio');
    audioElement.setAttribute('src', "http://"+ document.domain +"/files/notification2.mp3");
    audioElement.setAttribute('type', 'audio/mpeg');
    audioElement.setAttribute('autoplay', 'autoplay');
    //audioElement.load()

    audioElement.addEventListener("load", function() {
        audioElement.play();
    }, true);

}

为了添加 ogg file我试图生成所需的结构但没有成功:

In order to add the ogg file I've tried to generate the needed structure without succeed:

var audioElement = document.createElement('audio');
var audioSource1 = document.createElement('source');
var audioSource2 = document.createElement('source');

//mp3 file
audioSource1.setAttribute('src', "http://"+ document.domain +"/files/notification2.mp3");
audioSource1.setAttribute('type', 'audio/mpeg');

//ogg file
audioSource2.setAttribute('src', "http://"+ document.domain +"/files/notification2.ogg");
audioSource2.setAttribute('type', 'audio/ogg');

audioElement.setAttribute('autoplay', 'autoplay');

audioElement.append(audioSource2); //opera
audioElement.append(audioSource1);

//audioElement.load()
$.get();
audioElement.addEventListener("load", function() {
    audioElement.get(0).play();
}, true);

如何才能在Opera中使用 ogg files?

How could accomplish it to make it work also in Opera with ogg files?

推荐答案

使用特征检测代替浏览器检测:

Use feature detection instead of browser detection:

function isMpeg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
}


function isOgg()
{
    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
}

function isAAC()
{
    var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));
}

alert(isMpeg());
alert(isOgg());
alert(isAAC());

示例: http://jsfiddle.net/Cy9AT/1/

另见: http://diveintohtml5.info/everything.html

或使用 Modernizer

这篇关于Opera中的声音通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆