3

我注意到Safari 中的音频播放音量不是 100%,即使我将分配的控件设置为“1”。

是否有其他人可以这样做,或者这可能是脚本中的问题?

使用此控件(如下所示),音频在 Firefox 中播放率为 100%。

更新:显然我用于音量的控件在 Safari 中无法识别(见下文),所以我现在正在寻找一种方法,在 Safari 中使用下面的这个播放器以 100% 的音量播放音频。

根据 Safari 开发者库,阅读量总是返回 1,这是我设置播放器的值;但是,Safari 中的音量略低于其他浏览器。

问题可能出在我的脚本中,还是仅仅是 Safari 的问题?

另外,我注意到更改音量参数会减弱 Safari 中的音量,但 1 不等于 100%;因此,它确实有一些效果,但仍然存在问题。

这是我的 HTML:

<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="src/jquery.ubaplayer.js"></script>
<script>
jQuery(document).ready(function() { 
   jQuery.noConflict();
   jQuery(function(){
            jQuery("#ubaPlayer").ubaPlayer({
            codecs: [{name:"MP3", codec: 'audio/mpeg;'}]
            });

    });
    jQuery('a[rel=vidbox]').click(function () {

        if (jQuery("#ubaPlayer").ubaPlayer("playing") === true) {
            jQuery("#ubaPlayer").ubaPlayer("pause");
            }
        return false;
    });
})
</script>
</head>

<body>
<div id="ubaPlayer"></div>
    <ul class="controls">
        <li><a class="audioButton" href="mp3/dontthinktwice.mp3">
        Don't Think Twice (Bob Dylan)</a></li>
        <li><a class="audioButton" href="mp3/livingforthecity.mp3">
        Living for the City (Stevie Wonder)</a></li>
    </ul>
</body>

音频播放器 Javascript:

(function($) {
var defaults = {
    audioButtonClass: "audioButton",
    autoPlay: null,
    codecs: [{
        name: "OGG",
        codec: 'audio/ogg; codecs="vorbis"'
    }, {
        name: "MP3",
        codec: 'audio/mpeg'
    }],
    continuous: false,
    extension: null,
    flashAudioPlayerPath: "libs/swf/player.swf",
    flashExtension: ".mp3",
    flashObjectID: "audioPlayer",
    loadingClass: "loading",
    loop: false,
    playerContainer: "player",
    playingClass: "playing",
    swfobjectPath: "libs/swfobject/swfobject.js",
    volume: 1
},
    currentTrack, isPlaying = false,
    isFlash = false,
    audio, $buttons, $tgt, $el, playTrack, resumeTrack, pauseTrack, methods = {
        play: function(element) {
            $tgt = element;
            currentTrack = _methods.getFileNameWithoutExtension($tgt.attr("href"));
            isPlaying = true;
            $tgt.addClass(defaults.loadingClass);
            $buttons.removeClass(defaults.playingClass);

            if (isFlash) {
                if (audio) {
                    _methods.removeListeners(window);
                }
                audio = document.getElementById(defaults.flashObjectID);
                _methods.addListeners(window);
                audio.playFlash(currentTrack + defaults.extension);
            } else {
                if (audio) {
                    audio.pause();
                    _methods.removeListeners(audio);
                }
                audio = new Audio("");
                _methods.addListeners(audio);
                audio.id = "audio";
                audio.loop = defaults.loop ? "loop" : "";
                audio.volume = defaults.volume;
                audio.src = currentTrack + defaults.extension;
                audio.play();
            }
        },

        pause: function() {
            if (isFlash) {
                audio.pauseFlash();
            } else {
                audio.pause();
            }

            $tgt.removeClass(defaults.playingClass);
            isPlaying = false;
        },

        resume: function() {
            if (isFlash) {
                audio.playFlash();
            } else {
                audio.play();
            }
            $tgt.addClass(defaults.playingClass);
            isPlaying = true;
        },

        playing: function() {
            return isPlaying;
        }
    },

    _methods = {
        init: function(options) {
            var types;

            //set defaults
            $.extend(defaults, options);
            $el = this;

            //listen for clicks on the controls
            $(".controls").bind("click", function(event) {
                _methods.updateTrackState(event);
                return false;
            });
            $buttons = $("." + defaults.audioButtonClass);

            types = defaults.codecs;
            for (var i = 0, ilen = types.length; i < ilen; i++) {
                var type = types[i];
                if (_methods.canPlay(type)) {
                    defaults.extension = [".", type.name.toLowerCase()].join("");
                    break;
                }
            }

            if (!defaults.extension || isFlash) {
                isFlash = true;
                defaults.extension = defaults.flashExtension;
            }

            if (isFlash) {
                $el.html("<div id='" + defaults.playerContainer + "'/>");
                $.getScript(defaults.swfobjectPath, function() {
                    swfobject.embedSWF(defaults.flashAudioPlayerPath, defaults.playerContainer, "0", "0", "9.0.0", "swf/expressInstall.swf", false, false, {
                        id: defaults.flashObjectID
                    }, _methods.swfLoaded);
                });
            } else {
                if (defaults.autoPlay) {
                    methods.play(defaults.autoPlay);
                }
            }
        },

        updateTrackState: function(evt) {
            $tgt = $(evt.target);
            if (!$tgt.hasClass("audioButton")) {
                return;
            }
            if (!audio || (audio && currentTrack !== _methods.getFileNameWithoutExtension($tgt.attr("href")))) {
                methods.play($tgt);
            } else if (!isPlaying) {
                methods.resume();
            } else {
                methods.pause();
            }
        },

        addListeners: function(elem) {
            $(elem).bind({
                "canplay": _methods.onLoaded,
                "error": _methods.onError,
                "ended": _methods.onEnded
            });
        },

        removeListeners: function(elem) {
            $(elem).unbind({
                "canplay": _methods.onLoaded,
                "error": _methods.onError,
                "ended": _methods.onEnded
            });
        },

        onLoaded: function() {
            $buttons.removeClass(defaults.loadingClass);
            $tgt.addClass(defaults.playingClass);

            audio.play();
        },

        onError: function() {
            $buttons.removeClass(defaults.loadingClass);
            if (isFlash) {
                _methods.removeListeners(window);
            } else {
                _methods.removeListeners(audio);
            }
        },

        onEnded: function() {
            isPlaying = false;
            $tgt.removeClass(defaults.playingClass);
            currentTrack = "";
            if (isFlash) {
                _methods.removeListeners(window);
            } else {
                _methods.removeListeners(audio);
            }

            if (defaults.continuous) {
                var $next = $tgt.next().length ? $tgt.next() : $(defaults.audioButtonClass).eq(0);
                methods.play($next);
            }

        },

        canPlay: function(type) {
            if (!document.createElement("audio").canPlayType) {
                return false;
            } else {
                return document.createElement("audio").canPlayType(type.codec).match(/maybe|probably/i) ? true : false;
            }
        },

        swfLoaded: function() {
            if (defaults.autoPlay) {
                setTimeout(function() {
                    methods.play(defaults.autoPlay);
                }, 500);
            }
        },

        getFileNameWithoutExtension: function(fileName) {
            //this function take a full file name and returns an extensionless file name
            //ex. entering foo.mp3 returns foo
            //ex. entering foo returns foo (no change)
            var fileNamePieces = fileName.split('.');
            fileNamePieces.pop();
            return fileNamePieces.join(".");
        }
    };

$.fn.ubaPlayer = function(method) {
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === "object" || !method) {
        return _methods.init.apply(this, arguments);
    } else {
        $.error("Method " + method + " does not exist on jquery.ubaPlayer");
    }
};})(jQuery);
4

2 回答 2

3

您是在谈论移动 Safari 吗?在这种情况下,根本无法使用 Javascript 设置音量。

阅读以下内容:http: //www.ibm.com/developerworks/library/wa-ioshtml5/

如果您在谈论桌面 Safari,我的直觉是它的浏览器在这里有问题,而不是您的代码。

于 2013-07-18T10:28:20.900 回答
1

可能对您使用有帮助

  • Web Audio API https://www.html5rocks.com/en/tutorials/webaudio/intro/带有增益(不是音量)控制。对所有浏览器都非常统一(但是 IE 不支持)
  • 在 html5/web 音频 API 之上使用抽象,例如https://howlerjs.com/
  • 我可以看到的最后一个选项 - 将您的 html5 音频传递给 WebAudio API 源并使用 JS 控制增益(您可以在此处找到更多详细信息)它与选项 #1 完全相同,但几乎不需要更改您的代码。
于 2019-02-24T21:27:06.993 回答