Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chux0519 committed Sep 24, 2016
1 parent 2b49c6e commit 85e76a8
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 20 deletions.
Binary file added image/fix/bgm01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/bgm15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/fix/scroll-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 110 additions & 13 deletions page/API/background-audio/background-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,155 @@ var dataUrl = 'http://ws.stream.qqmusic.qq.com/C100001wmp4t06stlC.m4a?fromtag=38
Page({
onLoad: function () {
var that = this

// 设置播放停止监听事件
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)
})

// 进入的时候应该检测后台是否有音乐正在播放
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()
}
})
}
},
data: {
playing: false,
playTime: 0,
formatedPlayTime: '00:00:00'
},
play: function (res) {
play: function () {
var that = this
if(this.updateInterval >= 0)
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 (res) {
complete: function () {
that.setData({
playing: true
playing : true
})
that._enableInterval()
}
})
this._enableInterval()
},
seek: function (e) {
clearInterval(this.updateInterval)
resume:function(){
var that = this
wx.seekBackgroundAudio({
position: e.detail.value,
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 () {
// 实际会延迟两秒左右才跳过去
setTimeout(function () {
that._enableInterval()
}, 2000)
that.setData({
playing:true
})
wx.seekBackgroundAudio({
position: that.data.playTime,
complete:function(){
console.log("resume ok")
}
})
}
})
},
seek: function (e) {
var that = this
clearInterval(this.updateInterval)
// 此时正在播放
if(this.data.playing === true)
wx.seekBackgroundAudio({
position: e.detail.value,
complete: function(){
that._enableInterval()
}
})
// 此时是暂停状态
else
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: e.detail.value,
complete:function(){
console.log("seek and play ok")
that._enableInterval()
}
})
}
})
},
pause: function () {
var that = this
wx.pauseBackgroundAudio({
success: function () {
// 清除定时器
that.setData({
playing: false
playing:false
})
clearInterval(that.updateInterval)
}
})
},
stop: function () {
var that = this
wx.stopBackgroundAudio({
success: function (res) {
// 清除定时器
clearInterval(that.updateInterval)
// 设置初始数据
that.setData({
playing: false,
playTime: 0,
Expand All @@ -73,6 +168,8 @@ Page({
wx.getBackgroundAudioPlayerState({
success: function (res) {
that.setData({
// 正在播放
playing:true,
playTime: res.currentPosition,
formatedPlayTime: util.formatTime(res.currentPosition + 1)
})
Expand Down
10 changes: 8 additions & 2 deletions page/API/background-audio/background-audio.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
<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="play">
<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>
<view class="page-body-button"></view>
<!-- <view class="page-body-button"></view> -->
</view>
</view>

Expand Down
3 changes: 3 additions & 0 deletions page/API/background-audio/background-audio.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
width: 250rpx;
text-align: center;
}
.page-body-button .restart{
line-height: 80px;
}
10 changes: 8 additions & 2 deletions page/API/voice/voice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var util = require('../../../util/util.js')
var playTimeInterval
var playTimeInterval,interval

Page({
data: {
Expand All @@ -15,7 +15,7 @@ Page({
this.setData({ recording: true })

var that = this
var interval = setInterval(function () {
interval = setInterval(function () {
that.data.recordTime += 1
that.setData({
formatedRecordTime: util.formatTime(that.data.recordTime)
Expand All @@ -37,6 +37,8 @@ Page({
},
stopRecord: function () {
wx.stopRecord()
this.setData({recording:false,recordTime:0,formatedRecordTime: '00:00:00'})
clearInterval(interval)
},
playVoice: function () {
var that = this
Expand Down Expand Up @@ -83,5 +85,9 @@ Page({
tempFilePath: '',
formatedRecordTime: util.formatTime(0)
})
},
onUnload:function(){
this.stopRecord()
console.log("voice page has been unloaded!")
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Page({
console.log(e)
},
scrollToTop: function(e) {
this.setAction({
this.setData({
scrollTop: 0
})
},
Expand Down
5 changes: 3 additions & 2 deletions util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function formatTime(time) {
time = time % 3600
var minute = parseInt(time / 60)
time = time % 60
var second = time
// 这里秒钟也取整
var second = parseInt(time)

return ([hour, minute, second]).map(function (n) {
n = n.toString()
Expand All @@ -17,4 +18,4 @@ function formatTime(time) {

module.exports = {
formatTime: formatTime
}
}

0 comments on commit 85e76a8

Please sign in to comment.