• uni-app 背景音频 熄屏或者退回桌面之后不在播放


    解决办法:下载安卓原生插件 ,并在离线打包中配置前台服务配置

    1、将插件导入到HBuilder X中

    在这里插入图片描述

    2、manifest.json中配置如下

    "nativePlugins" : {
    		    "XZH-musicNotification" : {
    		        "page" : "page/story/storyDetails",
    		        "favour" : "false",
    		        "themeColor" : "#ffffff",
    		        "titleColor" : "#ffffff",
    		        "artistColor" : "#ffffff",
    		        "__plugin_info__" : {
    		            "name" : "XZH-musicNotification",
    		            "description" : "Android音乐通知栏",
    		            "platforms" : "Android",
    		            "url" : "",
    		            "android_package_name" : "",
    		            "ios_bundle_id" : "",
    		            "isCloud" : false,
    		            "bought" : -1,
    		            "pid" : "",
    		            "parameters" : {
    		                "page" : {
    		                    "des" : "页面路径(如:pages/*/*)",
    		                    "key" : "xzh_page",
    		                    "value" : ""
    		                },
    		                "favour" : {
    		                    "des" : "是否开启收藏按钮",
    		                    "key" : "xzh_favour",
    		                    "value" : ""
    		                },
    		                "themeColor" : {
    		                    "des" : "widget背景色(请使用 ARGB 格式)",
    		                    "key" : "xzh_theme_color",
    		                    "value" : ""
    		                },
    		                "titleColor" : {
    		                    "des" : "widget歌名字体色(请使用 ARGB 格式)",
    		                    "key" : "xzh_title_color",
    		                    "value" : ""
    		                },
    		                "artistColor" : {
    		                    "des" : "widget歌手字体色(请使用 ARGB 格式)",
    		                    "key" : "xzh_artist_color",
    		                    "value" : ""
    		                }
    		            }
    		        }
    		    }
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    在这里插入图片描述

    3、封装 JS 调用的方法

    代码: musicNotification.js
    提取码:7zbd

    4、使用

    (1)在app.vue 里面调用初始化的方法

    			this.$mc.init({
    				//点击通知栏跳转页面
    				path: '/pages/story/storyDetails.vue',
    				//设置状态栏小图标,只有 android 8.0 以上才有效
    				icon: '/static/image/logo.png',
    			})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (2)需要用到播放器的页面,进行创建生成

    import music from '@/utils/musicNotification.js'
    
    data() {
    			return {
    				isInitPlay:false
    			}
    }onLoad() {
    
    	//有问题哈,这个背景音频的方法在其他页面也执行了,所以需要加个判断是否是当前页面
    	let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
    	let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
    	if (currentRoute == "pages/story/storyDetails") {
    		this.isaudio=true;
    	}
    	//判断用户是否前往设置通知权限 在返回后重新设置故事信息
    	if(uni.getStorageSync("musicIsUpdata")==1){
    		this.updataInfoX(this.storyList[this.current])
    		uni.setStorageSync("musicIsUpdata",0)
    	}
    	this.audio=this.$audioUtils.initContext();//获取封装的背景音频
    }onUnload() {
    	let timer=setTimeout(()=>{
    		// 添加移除 播放器
    		plus.globalEvent.removeEventListener('musicNotificationPause')
    		uni.removeStorageSync("createMusic");
    		music.playOrPause(JSON.parse(false))
    		music.cancel();
    		clearTimeout(timer)
    	},300)
    },
    mounted(){
    	let _this=this;
    	//切换下一首
    	plus.globalEvent.addEventListener('musicNotificationNext', function(e) {
    		_this.onNext();
    		music.playOrPause(JSON.parse(true));
    	});
    	//切换上一首
    	plus.globalEvent.addEventListener('musicNotificationPrevious', function(e) {
    		_this.onPre();
    		music.playOrPause(JSON.parse(true));
    	});
    	//暂停与播放
    	plus.globalEvent.addEventListener('musicNotificationPause', function(e) {
    		_this.onSwitch()
    	}); 
    	//音频进入可以播放状态
    	this.audio.onCanplay(function(e){
    		if(this.isaudio){
    				this.timeLength=Math.floor(_this.audio.duration)	
    				if(this.isHide){
    					//更新音频播放
    					this.Notification()
    				}	
    		}
    	})
    }onHide() {
    	this.isHide=true;
    	//显示音频播放器
    	this.Notification();
    },
    methods:{
    			// 提示用户给予通知权限
    			updataInfo(storyInfo){
    				let Info=storyInfo;
    				  let res = music.update({
    				  	songName: Info.title,
    				  	favour: false, //这里我写死, 
    				  	picUrl: Info.img_url
    				  });
    				  switch (res.code) {
    				  	case -1: //未知错误
    				  		return;
    				  	case -2: //没有权限
    				  		uni.showModal({ 
    				  			title: '开启通知说明',
    				  			cancelText:"前往设置",   
    				  			confirmText:"本次忽略",   
    				  			content: '尊敬的用户您好,应用需要开启通知以保证程序在后台能够正常运行。如果您选择不开启,应用在后台一段时间将被系统回收,导致播放中断,强烈建议您开启!(忽略后下次启动可继续在此页面点击前往设置)',
    				  			success: function (res) {
    				  				if (res.confirm) {
    				  				} else if (res.cancel) {
    				  					music.openPermissionSetting(); //没有权限,跳转设置页面
    				  					uni.setStorageSync("musicIsUpdata",1)
    				  				}
    				  			}
    				  		});
    				  		return;
    				  }  				
    			},
    			// 更新通知信息
    			updataInfoX(storyInfo){			
    				let res = music.update({
    					songName: storyInfo.title,
    					favour: false, //这里我写死, 
    					picUrl: storyInfo.img_url
    				});
    				console.log(res)
    				switch (res.code) {
    					
    					
    					case -1: //未知错误
    						return;
    					case -2: //没有权限
    						return;
    				}
    			},
    			Notification(){
    				if(this.isHide){
    					if(this.isInitPlay){
    						this.updataInfoX(this.storyList[this.current])
    					}else{
    						let storyList=this.storyList[this.current]
    						music.createNotification(res=>{
    							this.updataInfo(storyList)
    							music.playOrPause(JSON.parse(true))
    							this.isInitPlay=true
    						})
    					}
    				}
    			},
    			lockActivity(){
    				// 设置锁屏页,同时返回授权状态
    				if (!music.openLockActivity({
    						lock: !this.isLockActivity
    					})) {
    					// 没有权限,弹出一个对话框
    					uni.showModal({
    						content: 'android 10 以上需要开启悬浮窗权限才能打开锁屏页',
    						success: (res) => {
    							if (res.confirm) {
    								// 用户点击确定,注册授权监听
    								plus.globalEvent.addEventListener('openLockActivity', ({
    									type
    								}) => {
    									if (type) {
    										// 获得悬浮窗权限,重新设置锁屏页
    										this.lockActivity()
    									}
    									// 注销授权监听
    									plus.globalEvent.removeEventListener('openLockActivity');
    								});
    								/**
    								 * 没有悬浮窗权限,跳转设置页面
    								 * android 10 以上没有这权限打不开锁屏页
    								 */
    								music.openOverlaySetting();
    							}
    						}
    					});
    				} else {
    					// 有权限,就修改前端状态
    					this.isLockActivity = !this.isLockActivity;
    				}
    			},
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
  • 相关阅读:
    关于 在国产麒麟系统上使用QProcess配合管道命令执行shell命令获取预期结果输出失败 的解决方法
    c++ 变量常量指针练习题
    精智屏如何实现秒级计划任务
    DCDC电源模块 HRA(B) W1~25W系列 隔离宽电压输入 高电压稳压输出
    JSX return里面如何用if判断
    如何快速高效全面的学习云计算和虚拟化技术
    尚硅谷_宋红康_IntelliJ IDEA 常用快捷键一览表
    安全协议之-TLS握手过程详解
    Java版企业电子招标采购系统源码—企业战略布局下的采购寻源
    509 - RAID! (UVA)
  • 原文地址:https://blog.csdn.net/weixin_45784582/article/details/125899816