慕课网-微信小程序入门与实战-常用组件API开发技巧项目实战-第6章构建新闻详情页面-音乐播放最终章
音乐播放最终章
1.还存在一个bug,当音乐播放时,离开当前页面A去到页面B时,页面B上的音乐图标显示正在播放中,但是播放的歌并不是页面B的歌,是页面A的歌
2.修改文件 app.js,添加一个全局变量
App({
globalData: {
g_isPlayingMusic: false,
g_currentMusicPostId: null
}
})
3.进入目录 pages/posts/post-detail,修改文件 post-detail.js
var postsData = require('../../../data/posts-data.js')
var app = getApp();
Page({
data: {
isPlayingMusic: false
},
onLoad: function(option) {
var postId = option.id;
this.data.currentPostId = postId;
var postData = postsData.postList[postId];
// 如果在onLoad方法中,不是异步的去执行一个数据绑定
// 则不需要使用 this.setData 方法
// 只需要对 this.data 赋值即可实现数据绑定
this.setData({
postData: postData
})
// var postsCollected = {
// 1:"true",
// 2:"false",
// 3:"true"
// ...
// }
var postsCollected = wx.getStorageSync('posts_collected')
if (postsCollected) {
var postCollected = postsCollected[postId]
if (!postCollected) {
this.setData({
collected: false
})
} else {
this.setData({
collected: postCollected
})
}
} else {
var postsCollected = {};
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
this.setData({
isPlayingMusic: true
});
}
this.setMusicMonitor();
},
setMusicMonitor: function(event) {
var that = this;
wx.onBackgroundAudioPlay(function() {
that.setData({
isPlayingMusic: true
})
app.globalData.g_isPlayingMusic = true;
app.globalData.g_currentMusicPostId = that.data.currentPostId;
});
wx.onBackgroundAudioPause(function() {
that.setData({
isPlayingMusic: false
})
app.globalData.g_isPlayingMusic = false;
app.globalData.g_currentMusicPostId = null;
});
},
onCollectionTap: function(event) {
this.getPostsCollectedSyc();
//this.getPostsCollectedAsy();
},
getPostsCollectedAsy: function() {
var that = this;
wx.getStorage({
key: "posts_collected",
success: function(res) {
var postsCollected = res.data;
var postCollected = postsCollected[that.data.currentPostId];
// 收藏变成未收藏,未收藏变成收藏
postCollected = !postCollected;
postsCollected[that.data.currentPostId] = postCollected;
that.showToast(postsCollected, postCollected);
}
})
},
getPostsCollectedSyc: function() {
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.data.currentPostId];
// 收藏变成未收藏,未收藏变成收藏
postCollected = !postCollected;
postsCollected[this.data.currentPostId] = postCollected;
this.showToast(postsCollected, postCollected);
},
showModal: function(postsCollected, postCollected) {
var that = this;
wx.showModal({
title: '收藏',
content: postCollected ? '收藏该文章?' : '取消收藏该文章?',
showCancel: 'true',
cancelText: '取消',
cancelColor: '#333',
confirmText: '确认',
confirmColor: '#405f80',
success: function(request) {
if (request.confirm) {
// 更新文章是否的缓存值
wx.setStorageSync('posts_collected', postsCollected);
// 更新数据绑定变量,从而实现切换图片
if (!postsCollected) {
this.setData({
collected:false
})
} else {
that.setData({
collected: postCollected
})
}
}
}
})
},
showToast: function(postsCollected, postCollected) {
// 更新文章是否的缓存值
wx.setStorageSync('posts_collected', postsCollected);
// 更新数据绑定变量,从而实现切换图片
if (!postsCollected) {
this.setData({
collected: false
})
} else {
that.setData({
collected: postCollected
})
}
wx.showToast({
title: postCollected ? "收藏成功" : "取消成功",
duration: 1000,
icon: "success"
})
},
onSharpTap: function(event) {
var itemList = [
"分享给微信好友",
"分享到朋友圈",
"分享到QQ",
"分享到微博"
]
wx.showActionSheet({
itemList: itemList,
itemColor: "#405f80",
success: function(res) {
// ret.cannel 用户是不是点击了取消按钮
// ret.tapIndex 数组元素的序号,从0开始
wx.showModal({
title: '用户' + itemList[res.tapIndex],
content: '用户是否取消?' + res.cancel + '现在无法实现分享功能,什么时候能支持呢',
})
}
})
},
onMusicTap: function(event) {
var currentPostId = this.data.currentPostId;
var postData = postsData.postList[currentPostId].music;
var isPlayingMusic = this.data.isPlayingMusic;
if (isPlayingMusic) {
wx.pauseBackgroundAudio();
this.setData({
isPlayingMusic: false
})
} else {
wx.playBackgroundAudio({
dataUrl: postData.url,
title: postData.title,
coverImgUrl: postData.coverImg
})
this.setData({
isPlayingMusic: true
})
}
}
})
posted on 2019-11-21 11:34 herisson_pan 阅读(3) 评论(0) 收藏 举报
浙公网安备 33010602011771号