jquery - Html5 音频无法在 Cordova App 中播放

标签 jquery ios html cordova audio

我正在处理遗留代码库,它是一个运行 pacman 皮肤版本的 jquery 应用程序。背景音频工作正常,但音效不正常。我怀疑有一个特殊的权限可以放入 config.xml 但没有成功。

谁能指出为什么 ios 不会播放音效,即使音频文件是从同一目录加载的?请让我知道代码的哪些其他部分可能有帮助。

非常感谢。

这是 config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="de.grammatidis.battle" version="1.0.2" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <name>Battle</name>
    <description>Vorsicht, die rasenden Beißerchen haben es auf dich abgesehen! Flüchte so schnell du kannst mit dem Grammatidis-Männchen vor den wilden Zahngeistern und sammle dabei Punkte! 
    </description>
    <author email="kontakt@biloba-it.de" href="http://www.biloba-it.de">
        Biloba IT
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <plugin name="cordova-plugin-device" spec="~1.1.0" />
    <plugin name="cordova-plugin-inappbrowser" spec="~1.1.0" />
    <plugin name="cordova-plugin-media" spec="~1.0.1" />
    <plugin name="cordova-plugin-network-information" spec="~1.1.0" />
</widget>

这是 audio.js

var Sound = function(game) {
    this.files          = {}; 
    this.loaded         = {};
    this.paths          = {};
    this.loop           = {};
    this.playing        = [];
    this.stopped        = [];
    this.playingIndex   = {};
    this.game = game;
    this.volume = 1;
    this.disabled = game.getSetting("soundDisabled") == "true" ? true : false;
    this.multiple = ["snd_eat","snd_eatghost"];
    this.backgroundSounds = ["original","elektro","dance","rock","pop","kids"];
    this.interval       = {};
};

Sound.prototype.load = function(name, path, realload, callback) {
    console.log('Sound.prototype.load');
    var _self = this;

    //save the path
    this.paths[name] = path;

    if(_self.loaded[name] == true) {
        if(callback) callback();
    } else {
        if(realload) {
            //load the audio file
            _self.files[name] = new Media(path,
                function(){},
                function(){},
                function(status){
                    //check if done
                    if(status == 4 && _self.stopped.indexOf(name) == -1) {
                        if(_self.loop[name] == true) {
                            _self.files[name].play();
                        } else {
                            var i = _self.playing.indexOf(name);
                            if(i != -1) {
                                _self.playing.slice(i,1);
                            }
                        }
                    }
                });

            //set the loaded flag
            _self.loaded[name] = true;
            if(callback) callback();

        } else {            
            _self.loaded[name] = false;
            if(callback) callback();
        }
    }
};

Sound.prototype.toggleSound = function() {
    console.log('Sound.prototype.toggleSound');

    if(this.disabled) {
        this.enableSound();
    } else {
        this.disableSound();
    }
};

Sound.prototype.enableSound = function() {
    console.log('Sound.prototype.enableSound');

    this.disabled = false;
    this.game.saveSettings("soundDisabled", "false");
    if(this.game.getSetting("music") == "original") {
        this.play(this.game.getSetting("music"), false, 0.2);
    } else {
        this.play(this.game.getSetting("music"), true, 0.2);
    }
};

Sound.prototype.disableSound = function(callback) {
    console.log('Sound.prototype.disableSound');

    var _self = this;
    this.disabled = true;
    this.game.saveSettings("soundDisabled", "true");

    for (var i = 0; i < this.playing.length; i++) {
        _self.stop(this.playing[i]);
    }
    this.playing = [];
    if(callback) callback();
};

Sound.prototype.isDisabled = function() {
    return this.disabled;
};

Sound.prototype.isLoaded = function(name) {
    console.log('Sound.prototype.isLoaded');

    var _self = this;

    var i = _self.playingIndex[name];
    if(i === undefined || i == null) i = 1;
    _self.playingIndex[name] = i;

    return (_self.loaded[name] == true) || (_self.multiple.indexOf(name) != -1 && _self.loaded[name + "_" + i] == true);
};

Sound.prototype.play = function(name, loop, volume, callback) {
    console.log('Sound.prototype.play');

    var _self = this;

    function doPlay() {
        //remove from the stopped list
        var i = _self.stopped.indexOf(name);
        if(i != -1) {
            _self.stopped.slice(i,1);
        }

        //add to the playing list
        if(_self.playing.indexOf(name) == -1) _self.playing.push(name);

        //check if the sound can be played multiple times
        if(_self.multiple.indexOf(name) != -1) {
            var i = _self.playingIndex[name];
            if(i === undefined || i == null) i = 1;
            i++;
            if(i > 5) i = 1;
            _self.playingIndex[name] = i;
            name = name + "_" + i;
        }

        //set the volumen
        if(volume !== undefined) {
            _self.files[name].setVolume(volume);
        } else {
            _self.files[name].setVolume(_self.volume);
        }

        _self.files[name].play();
        _self.loop[name] = loop;

        if(loop) {
            var duration = _self.files[name].getDuration();
            if(duration != -1) {
                _self.interval[name] = setInterval(function(){
                    _self.files[name].play();
                }, (duration+10) * 1000);
            }
        }
    }

    var blnPlay = true;
    if(Platform.isApp() && this.isBackgroundSound(name) == false) blnPlay = false;

    if(this.disabled == false && blnPlay == true) {
        if(_self.isLoaded(name)) {
            doPlay();
            if(callback) callback();
        } else {
            _self.load(name, _self.paths[name], true, function(){
                doPlay();
                if(callback) callback();
            });
        }
    } else {
        if(callback) callback();
    }
};

Sound.prototype.stop = function(name) {
    console.log('Sound.prototype.stop');

    if(this.files[name] !== undefined) {
        this.stopped.push(name);
        if(this.interval[name] !== undefined){
            clearInterval(this.interval[name]);
        }
        this.files[name].stop();
    }
};

Sound.prototype.pause = function() {
    console.log('Sound.prototype.pause');

    for (var i = 0; i < playing.length; i++) {
        this.files[playing[i]].pause();
    }
};

Sound.prototype.resume = function() {
    console.log('Sound.prototype.resume');

    for (var i = 0; i < playing.length; i++) {
        this.files[playing[i]].play();
    }        
};

Sound.prototype.isBackgroundSound = function(snd) {
    console.log('Sound.prototype.isBackgroundSound');

    return (this.backgroundSounds.indexOf(snd) != -1);
};

最佳答案

那是很多代码。根据我的经验,媒体插件绝对是非常挑剔的(和错误的)。我经常使用它。您能否通过运行如下基本示例来验证是否可以加载和播放一个文件:

var player = new Media(path,   
     function playSuccess() { 
        console.log("success");
        player.release();
     },
     function playError(err) {
        console.log("uh oh: " + err.code);
     });
player.play();

我确实注意到您没有释放资源,这对于长时间运行的游戏来说可能是个问题。

您是否正在尝试通过检测状态功能中的停止来重播/循环播放声音?我认为您可能希望通过再次调用 play(可能包含在 setTimeout(xx.play,0) 中)从成功函数中执行此操作。我之前没有尝试从这些回调中调用播放。

关于jquery - Html5 音频无法在 Cordova App 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010720/

相关文章:

jquery - 焦点显示列表和删除列表上的小部件

ios - Xcode - 从 UIImageView 到新的 ViewController

javascript - 如何使用网络音频 API 播放声音文件 Safari?

javascript - 根据先前的选择选项禁用选择输入

javascript - 如何在可调整大小的函数中获取X Y坐标?

javascript - 如何显示来自带有特殊字符的资源的消息?

jquery - IE 无法识别 jQuery 选择器

ios - 在应用程序中存储短音频文件/流

html - CSS child/nth child/typeof 选择器

html - 压缩文件和 HTML5 文件 API