Html5音频无法在Cordova App中播放

时间:2015-12-01 00:44:30

标签: jquery ios html5 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);
};

1 个答案:

答案 0 :(得分: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)中)。我之前没有尝试过从这些回调中调用play。