昨天晚上(2016年9月23日晚),微信小程序的开发者工具更新新版本后直接能够支持无appid的游客模式登陆,基本和网上流传的crack版本差很少,同时也有开发者分享了一个功能很全演示demo,我也下载了一份来尝鲜。javascript
看demo的过程当中,我发现了几个小问题,这里单独拿出来分享一下。css
组件中:java
内容分区/scroll-view中,click me to top按钮无效(done)git
接口中:github
录音功能展现中,没法中止录音。(可能问题只存在电脑调试器中)(done)web
录音功能展现中,若直接返回上一级页面,原页面的定时器未被回收(done)小程序
背景音乐功能展现中,首先播放音乐并返回上级,而后再进入播放音乐页面,音乐播放进度会丢失微信小程序
组件中:api
内容分区/swipe 中,swipe最后一张图片后会反方向到第一张图片(to be improved)微信
内容/Text 中,text默认的line删不掉,一开始体验‘remove’方法的话会产生“程序是否是有问题的错觉”
内容/picker 中,时间和日期选择器无效(开始觉得是bug,后来发现是demo源码中没有实现这里的选择器)(to be improved)
接口中:
注:这些在接口栏目中,能在PC上测试的全部的接口都是在PC上测试的,因此可能参考意义并非那么大
/page/component/component-pages/wx-scroll-view/wx-scroll-view.js
16L 将"setAction" 改成 "setData"
scrollToTop: function(e) { this.setData({ scrollTop: 0 }) }
在/page/API/voice/voice.js
中,作如下几点改动
将录音定时器放在全局。
在stop record中清除定时器
onUnload触发时也要清除定时器并还原初始数据
var util = require('../../../util/util.js') var playTimeInterval,interval Page({ data: { ... }, startRecord: function () { ... }, stopRecord: function () { wx.stopRecord() this.setData({recording:false,recordTime:0,formatedRecordTime: '00:00:00'}) clearInterval(interval) }, ... onUnload:function(){ this.stopRecord() console.log("voice page has been unloaded!") } })
/page/API/background-audio/background-audio.js
重写了一些方法
该page生命周期中的onLoad过程当中,先经过api获取是否有背景音乐正在播放,获取其进度,而后将data里的数据更新,在onLoad中增长checkPlaying方法
Page({ onLoad: function () { var that = this ... // 进入的时候应该检测后台是否有音乐正在播放 checkPlaying() function checkPlaying() { wx.getBackgroundAudioPlayerState({ success: function (res) { console.log("播放器状态:",res) console.log("初始化时的webview",that.data.__webviewId__) var _isPlaying,_playTime res.status === 1? _isPlaying = true:_isPlaying = false res.currentPosition/res.duration > 0.95? _playTime = 0:_playTime = res.currentPosition that.setData({ playing:_isPlaying, playTime: _playTime, formatedPlayTime: util.formatTime(_playTime) }) if(_isPlaying) that._enableInterval() } }) } }, ... })
增长了resume方法,用于继续播放(play方法是从头开始播放)
Pager({ ... resume:function(){ var that = this if(this.updateInterval != '') clearInterval(this.updateInterval) wx.playBackgroundAudio({ dataUrl: dataUrl, title: 'Lost Stars', coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R150x150M000000Jhxf24CFL06.jpg?max_age=2592000', complete: function () { that.setData({ playing:true }) wx.seekBackgroundAudio({ position: that.data.playTime, complete:function(){ console.log("resume ok") } }) } }) }, ... })
在onLoad中增长了背景音乐的两个状态的监听器,用于同步背景音乐状态的变化和UI的变化
Page({ onLoad: function () { ... // 设置播放中止监听事件 wx.onBackgroundAudioStop(function () { console.log("音乐中止了,当前webview: ",that.data.__webviewId__) that.setData({ playing: false, playTime: 0, formatedPlayTime: '00:00:00' }) }) // 设置播放开始监听事件 wx.onBackgroundAudioPlay(function(){ console.log("音乐开始了,当前webview: ",that.data.__webviewId__) // 开始后须要更新当前歌曲的秒数 if(that.data.playing === false) that.setData({ playing:true }) if(that.updateInterval >= 0) return that._enableInterval() }) // 设置播放暂停监听事件 wx.onBackgroundAudioPause(function(){ console.log("音乐暂停了,当前webview: ",that.data.__webviewId__) // 暂停后,不须要继续更新歌曲秒数 if(that.data.playing === true) that.setData({ playing:false }) if(that.updateInterval >= 0) clearInterval(that.updateInterval) }) ... },
同时在wxml中增长了重播的按键(文字的形式),简单的在两个block中加入text
<block wx:if="{{playing === true}}"> <view class="page-body-button" bindtap="stop"> <image src="/image/stop.png"></image> </view> <view class="page-body-button" bindtap="pause"> <image src="/image/pause.png"></image> </view> <view class="page-body-button" bindtap="play"> <text class="restart">重播</text> </view> </block> <block wx:if="{{playing === false}}"> <view class="page-body-button"></view> <view class="page-body-button" bindtap="resume"> <image src="/image/play.png"></image> </view> <view class="page-body-button" bindtap="play"> <text class="restart">重播</text> </view> </block>
再加一个样式在wxss
.page-body-button .restart{ line-height: 80px; }
在修复背景音乐一些小bug的过程当中,我发现,首先进入播放音乐界面播放歌曲,再返回上一个界面,这时候若是再进入播放音乐界面,那么播放器的功能没有问题,可是在该页面中用于更新UI的定时器会按进出次数递增。
为了解决上面的问题,我有下面几个想法:
在这个pager的onUnload阶段,对背景音乐的监听器进行手动的清除(问题是目前的文档中并无合适的api)
不使用背景音乐变化的监听器,包括wx.onBackgroundAudioPause
|wx.onBackgroundAudioStop
|wx.onBackgroundAudioPlay
,直接手动管理定时器。(我尝试过这样作,可是没有效果,定时器个数仍是会随着进出次数的增长而增长,但愿是个人实现错了)
目前在PR已经发了,原项目地址github