科尔多瓦(安卓)。最小化应用程序时禁用音频播放
Posted
技术标签:
【中文标题】科尔多瓦(安卓)。最小化应用程序时禁用音频播放【英文标题】:Cordova (Android). Disable audio play when application is minimized 【发布时间】:2019-06-03 16:38:51 【问题描述】:启动时,声音开始,最小化时不要停止。我能怎么做?通过
document.addEventListener("pause", onPause, false);
document.addEventListener("resume", onResume, false);
不能。 Pause Event Listener 做不到。帮助。抱歉英语不好。
附注要求写很多var app =
initialize: function ()
this.bindEvents();
,
bindEvents: function ()
document.addEventListener('deviceready', this.onDeviceReady, false);
,
onDeviceReady: function ()
app.receivedEvent('deviceready');
var myMedia = new Media("/android_asset/www/audio/sample.mp3")
myMedia.play(
playAudioWhenScreenIsLocked: false
);
,
receivedEvent: function ()
;
app.initialize();
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>SOOOUND!</title>
</head>
<body>
SOOOUND!
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
【问题讨论】:
【参考方案1】:您必须将Pause Event Listener 添加到您的应用程序中,以便在它进入后台时进行监听。
document.addEventListener("onDeviceReady", function()
// Adds pause event
document.addEventListener("pause", manageAppPause);
// Adds resume event
document.addEventListener("resume", manageAppResume);
);
function manageAppPause()
// Check if audio is playing
// If it is, pause it
function manageAppResume()
// If you want, check if the audio was playing before the app was put into background
// If so, resume audio
【讨论】:
不——你实际上必须在 manageAppPause 中输入你需要的任何代码才能真正暂停正在播放的任何音频...... 这样做了:function manageAppPause() audioElement.pause(); 功能 manageAppResume() audioElement.play(); 检查它并在 manageAppPause 中添加一个 console.log("Paused") 以确保该事件实际上正在触发【参考方案2】:var app =
initialize: function ()
this.bindEvents();
,
bindEvents: function ()
document.addEventListener('deviceready', this.onDeviceReady, false);
,
onDeviceReady: function ()
var myMedia = new Media("/android_asset/www/audio/sample.mp3")
myMedia.play(
playAudioWhenScreenIsLocked: false
);
document.addEventListener('pause', onPause, false);
document.addEventListener('resume', onResume, false);
function onPause()
myMedia.pause();
;
function onResume()
myMedia.play();
;
,
;
app.initialize();
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title>SOOOUND!</title>
</head>
<body>
SOOOUND!
</body>
</html>
【讨论】:
以上是关于科尔多瓦(安卓)。最小化应用程序时禁用音频播放的主要内容,如果未能解决你的问题,请参考以下文章