Html5 音频无法在 Cordova 应用程序中播放
Posted
技术标签:
【中文标题】Html5 音频无法在 Cordova 应用程序中播放【英文标题】:Html5 audio doesn't play in Cordova App 【发布时间】:2015-12-01 00:44:29 【问题描述】:我正在开发一个遗留代码库,它是一个运行 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);
;
【问题讨论】:
您实际尝试播放音频的代码会很有用...在 Cordova 中有多种播放音频的方法。音频标签、媒体插件、其他插件等...您使用哪种方法尝试播放音频?另外,您使用的是什么版本的 Cordova?您在 index.html 中有 CSP 设置吗? 干杯布拉德,代码库非常复杂,但我将粘贴上面的 audio.js。看起来它正在使用媒体插件中的功能。看起来没有在索引中设置 CSP。通过本机 javascript 调用获得音频,但不是最佳的。 【参考方案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 。
【讨论】:
嗨,谢谢布拉德.. 新的 Media() 函数和新的 Audio() 一样有效。似乎不同之处在于 Media 集成了回调。我将玩弄如何停止声音..可能将所有声音放在根范围内,以便正确的函数调用可以播放/暂停声音。感谢您的帮助!以上是关于Html5 音频无法在 Cordova 应用程序中播放的主要内容,如果未能解决你的问题,请参考以下文章
移动后iOS上的cordova-plugin-media-with-compression似乎无法播放音频文件
PhoneGap:无法在 ios 中更改 html5 音频对象的音量