因为要跨页面使用到uni.createInnerAudioContext组件,所以先在一个vue界面定义了组件
var xpjAudio = null;
xpjAudio = uni.createInnerAudioContext();
var presrc
var statuss
var preduration
var prebooks_id
var index=[1,0,0]
var if_true
export default{
xpjAudio,
presrc,
statuss,
preduration,
prebooks_id,
index,
if_true
}
然后在播放界面引用,但是暂停事件在微信小程序端可以正常使用,但云打包代码之后,将安装包下载到手机后,就无法暂停,并且图标也不会变
<template>
<view>
<view class="position">
<view class="music-head" :style="{paddingTop: statusBarHeight + 'px' }">
<view class="music-head-icon" :style="{paddingTop: statusBarHeight + 'px' }">
<image src="../../static/left_icon.png" class="left" @click="back()"></image>
</view>
<view class="title" :style="{ paddingTop: statusTitle + 'px' }">
{{data.title}}
</view>
</view>
</view>
<view v-if="ischeck">
该功能尚未开发,敬请期待
</view>
<view v-else>
<view class="list_content">
<view class="detail-play">
<image :src="data.coverImgUrl"></image>
</view>
<view class="books">
<view class="books-title">{{ data.title }}</view>
<view class="books-author">{{ data.author }}</view>
</view>
<view :v-if=url class='xpj-audio-box' >
<view class='current-time time-text'>{{format(currentTime)}}</view>
<view class='audio-action'>
<slider
@change='changeAudio'
:activeColor="activeColor"
:block-color="blockColor"
:backgroundColor="backgroundColor"
block-size="18"
:disabled="currentTime==0"
:min='0' :max='duration.toFixed(0)'
:value='currentTime.toFixed(0)'
:step='0.1'>
</slider>
</view>
<view class='duration-time time-text'>{{format(duration)}}</view>
</view>
<view class="play-button">
<view>
<image :src="likeImg" class='play-icon' @click="like()"></image>
</view>
<view @click="like_before_fifty()">
<image src="/pages/index/static/gobackward_back.png" class='play-icon'></image>
</view>
<view @click="before_books()">
<image src="/pages/index/static/forword_book.png" class='play-icon'></image>
</view>
<view>
<image :src='playImage' class='play_icon' @click="startPlay()"></image>
<!-- <image :src='startImg' class='play_icon' v-show='!status'></image>
<image :src='endImg' class='play_icon' v-show='status'></image> -->
</view>
<view @click="next_books()">
<image src="/pages/index/static/next_book.png" class='play-icon'></image>
</view>
<view @click="like_after_fifty()">
<image src="/pages/index/static/goforward_next.png" class='play-icon'></image>
</view>
<view @click="collection()">
<image :src="collectImg" class='play-icon'></image>
</view>
</view>
</view>
<view class="make_up_list">
</view>
<view class="audio_list">
<view class="list-radio">
<image src="/pages/index/static/play_list.png" class="list-image_list"></image>
<view class="list-content">
播放列表
</view>
</view>
<uni-list v-for="(item,index) in list" :key="index">
<view class="list">
<view class="list-content-sum">
<view class="list_amout">
{{index+1}}
</view>
<view>
<image src="/pages/index/static/cicle.png" style="width: 50upx; height: 50upx;" class="list-image"></image>
</view>
<view class="list-content-title">
{{item.title}}
</view>
<view class="list-content-author">
—— {{item.author}}
</view>
</view>
<view>
<image src="/pages/index/static/play.png" style="width: 25px; height: 25px;" class="play_image" @click="listening(index)"></image>
</view>
<view @click="cut_out(index)">
<image src="/pages/index/static/delete.png" style="width: 30px; height: 30px;" class="dalete_image"></image>
</view>
</view>
</uni-list>
</view>
</view>
</view>
</template>
<script>
import common from '../common/common.js'
export default {
data() {
return {
data:
{
"src": '',
"title": '',
"author": '',
"coverImgUrl": ''
},
books_data:
{
user_id:'',
books_id:''
},
history_data:
{
user_id:'',
books_id:'',
collect_time:''
},
url:'',
list:[],
user_id:'',
books_id:'',
index:0,
count:0,
nowcurrentTime:'',
currentTime: '',
duration: 100,
status: '',
like_countt:0,
collect_countt:0,
lag:'',
if_true_like:0,
if_true_collect:0,
if_true_history:0,
like_play:false,
collect_play:false,
likeImg:'/pages/index/static/like-before.png',
likeImg_after:'/pages/index/static/like-after.png',
likeImg_before:'/pages/index/static/like-before.png',
collectImg:'/pages/index/static/collect-before.png',
collectImg_after:'/pages/index/static/collect-after.png',
collectImg_before:'/pages/index/static/collect-before.png',
statusBarHeight: 0,
statusTitle:0,
ischeck:true,
playImage:{}
}
},
// components:{
// xAdudioPlay
// },
props: {
activeColor: {
type: String,
default: '#ff3042'
},
blockColor:{
type:String,
default:'#ff3042'
},
backgroundColor:{
type:String,
default:'gray'
},
blockSize:{
type:Number,
default:12,
},
startImg: {
type:String,
default:'/pages/index/static/play.png',
},
endImg: {
type:String,
default:'/pages/index/static/pause.png',
}
},
created: function(option) {
this.statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'];
this.statusBarHeight-=15
this.statusTitle=this.statusBarHeight-30
console.log("this.statusBarHeight ="+this.statusBarHeight )
console.log("this.statusTitle ="+this.statusTitle)
console.log("1common.xpjAudio.currentTime="+common.xpjAudio.currentTime)
this.currentTime=common.xpjAudio.currentTime
this.nowcurrentTime=this.currentTime
console.log("1this.currentTime="+this.currentTime)
this.url=uni.getStorageSync("listen_src")
common.xpjAudio.src = this.url;
console.log("index common.xpjAudio.src="+common.xpjAudio.src)
uni.setInnerAudioOption({
obeyMuteSwitch: false
})
this.onTimeUpdate();
this.onCanplay();
this.onEnded();
uni.$on('stop',()=> {
common.xpjAudio.stop()
this.status = true
})
},
onLoad : function(option){
var time='2023-03-20 24:00:00';
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
month >= 1 && month <= 9 ? (month = "0" + month) : ""
day >= 0 && day <= 9 ? (day = "0" + day) : ""
this.t = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
console.log("t="+this.t)
console.log(this.t<time)
this.ischeck=this.t<time
console.log("ischeck="+this.ischeck)
// 初始化参数
this.playImage = this.startImg
console.log("this.endImg:");
console.log(this.endImg);
// 接受标志
this.lag=option.lag
console.log("option.lag="+option.lag)
this.if_true_like=option.if_true_like
console.log("option.if_true_like="+option.if_true_like)
this.if_true_collect=option.if_true_collect
console.log("option.if_true_collect="+option.if_true_collect)
this.if_true_history=option.if_true_history
console.log("option.if_true_history="+option.if_true_history)
console.log("2common.xpjAudio.currentTime="+common.xpjAudio.currentTime)
this.currentTime=common.xpjAudio.currentTime
console.log("2this.currentTime="+this.currentTime)
this.user_id=uni.getStorageSync("user_id")
console.log("1user_id="+this.user_id)
this.books_id=uni.getStorageSync("listen_id")
console.log("happy happy like_this.books_id="+this.books_id)
this.data.src=uni.getStorageSync("listen_src")
this.data.title=uni.getStorageSync("listen_title")
this.data.author=uni.getStorageSync("listen_author")
this.data.coverImgUrl=uni.getStorageSync("listen_coverImgUrl")
this.url=uni.getStorageSync("listen_src")
console.log("data.title="+this.data.title)
console.log("data.coverImgUrl="+this.data.coverImgUrl)
// 播放列表
uniCloud.database().collection('audio_books').where({user_id:this.user_id}).field("_id,user_id,books_id,src,title,author,coverImgUrl").get().then(res => {
var adminList = res.result.data
adminList.forEach(async(item)=>{
uniCloud.database().collection('books').where({_id:item.books_id}).field("_id,src,author,title,coverImgUrl,src").get().then(res => {
adminList = res.result.data
adminList.forEach(async(item)=>{
console.log("item="+item)
this.list.push(item)
console.log("list="+this.list)
console.log("123 this.list[item]="+this.list[item])
})
}).catch(err => {console.log(err); })
})
}).catch(err => {console.log(err); })
console.log("12345 like_this.books_id="+this.books_id)
// 点赞是否显示啥
uniCloud.database().collection('books_like').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
console.log("this.like_play="+this.like_play)
if(res.result.data.length==0)
{
this.like_play=false
this.likeImg=this.likeImg_before
}
else
{
this.like_play=true
this.likeImg=this.likeImg_after
}
console.log("this.like_play="+this.like_play)
})
this.history_change()
// 收藏是否显示啥的
uniCloud.database().collection('books_collect').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
if(res.result.data.length==0)
{
this.collect_play=false
this.collectImg=this.collectImg_before
}
else
{
this.collect_play=true
this.collectImg=this.collectImg_after
}
})
// 点赞数
uniCloud.database().collection('books').where({_id:this.books_id}).field("like_count").get().then(res=>{
var armlist=res.result.data
armlist.forEach(async(item)=>{
this.like_countt=item.like_count
console.log("item.like_count="+item.like_count)
})
})
// 收藏数
uniCloud.database().collection('books').where({_id:this.books_id}).field("collect_count").get().then(res=>{
var armlist=res.result.data
armlist.forEach(async(item)=>{
this.collect_countt=item.collect_count
console.log("item.collect_count="+item.collect_count)
})
})
// 起始时间
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
adminList.forEach(async(item)=>{
console.log("数据表起始时间 item.finish_time="+item.finish_time)
this.currentTime=item.finish_time
console.log("代码起始时间 this.currentTime="+this.currentTime)
})
})
// 正在播放的这本书的时长
common.xpjAudio.onCanplay(()=> {
// 必须。可以当做是初始化时长
common.xpjAudio.duration;
// 必须。不然也获取不到时长
setTimeout(() => {
this.duration=common.xpjAudio.duration
common.preduration=this.duration
console.log("common.xpjAudio.duration="+common.xpjAudio.duration); // 401.475918
}, 1000)
})
common.prebooks_id=this.books_id
},
methods: {
init()
{
console.log("2common.xpjAudio.currentTime="+common.xpjAudio.currentTime)
this.currentTime=common.xpjAudio.currentTime
console.log("2this.currentTime="+this.currentTime)
this.user_id=uni.getStorageSync("user_id")
console.log("1user_id="+this.user_id)
this.books_id=uni.getStorageSync("listen_id")
console.log("happy happy like_this.books_id="+this.books_id)
this.data.src=uni.getStorageSync("listen_src")
this.data.title=uni.getStorageSync("listen_title")
this.data.author=uni.getStorageSync("listen_author")
this.data.coverImgUrl=uni.getStorageSync("listen_coverImgUrl")
this.url=uni.getStorageSync("listen_src")
console.log("data.title="+this.data.title)
console.log("data.coverImgUrl="+this.data.coverImgUrl)
console.log("12345 like_this.books_id="+this.books_id)
// 点赞是否显示啥
uniCloud.database().collection('books_like').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
this.like_play=false
this.likeImg=this.likeImg_before
console.log("this.like_play="+this.like_play)
if(res.result.data.length==0)
{
this.like_play=false
this.likeImg=this.likeImg_before
}
else
{
this.like_play=true
this.likeImg=this.likeImg_after
}
console.log("this.like_play="+this.like_play)
})
this.history_change()
// 收藏是否显示啥的
uniCloud.database().collection('books_collect').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
this.collect_play=false
this.collectImg=this.collectImg_before
console.log("this.collect_play="+this.collect_play)
if(res.result.data.length==0)
{
this.collect_play=false
this.collectImg=this.collectImg_before
console.log("thisthisthis")
}
else
{
this.collect_play=true
this.collectImg=this.collectImg_after
}
console.log("this.collect_play="+this.collect_play)
})
// 点赞数
uniCloud.database().collection('books').where({_id:this.books_id}).field("like_count").get().then(res=>{
var armlist=res.result.data
armlist.forEach(async(item)=>{
this.like_countt=item.like_count
console.log("item.like_count="+item.like_count)
})
})
// 收藏数
uniCloud.database().collection('books').where({_id:this.books_id}).field("collect_count").get().then(res=>{
var armlist=res.result.data
armlist.forEach(async(item)=>{
this.collect_countt=item.collect_count
console.log("item.collect_count="+item.collect_count)
})
})
// 起始时间
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
adminList.forEach(async(item)=>{
console.log("数据表起始时间 item.finish_time="+item.finish_time)
this.currentTime=item.finish_time
console.log("代码起始时间 this.currentTime="+this.currentTime)
})
})
common.xpjAudio.onCanplay(()=> {
// 必须。可以当做是初始化时长
common.xpjAudio.duration;
// 必须。不然也获取不到时长
setTimeout(() => {
this.duration=common.xpjAudio.duration
common.preduration=this.duration
console.log("common.xpjAudio.duration="+common.xpjAudio.duration); // 401.475918
}, 1000)
})
common.prebooks_id=this.books_id
},
// 历史记录
history_change()
{
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
month >= 1 && month <= 9 ? (month = "0" + month) : ""
day >= 0 && day <= 9 ? (day = "0" + day) : ""
this.history_data.collect_time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
this.history_data.user_id=this.user_id
this.history_data.books_id=this.books_id
uniCloud.database().collection('books_history').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
if(res.result.data.length==0)
{
uniCloud.database().collection('books_history').add(this.history_data)
}
else
{
uniCloud.database().collection('books_history').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('books_history').doc(_id).update(this.history_data)
})
}
})
},
// 返回上一页
back(currentTime)
{
console.log("1hahaha")
uni.setStorageSync("back_title",this.data.title)
uni.setStorageSync("back_author",this.data.author)
uni.setStorageSync("back_coverImgUrl",this.data.coverImgUrl)
uni.setStorageSync("back_id",this.books_id)
console.log("this.books_id="+this.books_id)
uni.setStorageSync("back_src",this.data.src)
uni.setStorageSync("back_status",this.status)
// uni.setStorageSync("back_currentTime",this.xpjAudio.currentTime)
common.presrc=common.xpjAudio.src
// common.statuss=this.status
console.log("common.xpjAudio.currentTime="+common.xpjAudio.currentTime)
common.preBooks_id=this.books_id
console.log("common.preBooks_id="+common.preBooks_id)
console.log('521314 this.lag='+this.lag)
// 更新书籍进度
if(common.xpjAudio.currentTime<this.duration)
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:parseFloat(this.currentTime)}).then(res=>{
console.log("1333 this.if_true_like="+this.if_true_like)
if(this.if_true_like)
{
console.log("2333 this.if_true_like="+this.if_true_like)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_like=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else if(this.if_true_collect)
{
console.log("2333 this.if_true_collect="+this.if_true_collect)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_collect=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else if(this.if_true_history)
{
console.log("2333 this.if_true_history="+this.if_true_history)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_history=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else
{
console.log('521314 1576 this.lag='+this.lag)
if(this.lag==1)
{
uni.navigateTo({
url:'/pages/home/home',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else
{
uni.navigateTo({
url:'/pages/books_detail/books_detail',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
}
})
})
}
else
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:0}).then(res=>{
if(this.if_true_like)
{
console.log("2333 this.if_true_like="+this.if_true_like)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_like=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else if(this.if_true_collect)
{
console.log("2333 this.if_true_collect="+this.if_true_collect)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_collect=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else if(this.if_true_history)
{
console.log("2333 this.if_true_history="+this.if_true_history)
uni.navigateTo({
url:'/pages/books_detail/books_detail?if_true_history=1',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else
{
if(this.lag==1)
{
uni.navigateTo({
url:'/pages/home/home',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
else
{
uni.navigateTo({
url:'/pages/books_detail/books_detail',
success()
{
let page = getCurrentPages().pop(); //跳转页面成功之后
if (!page) return;
page.onLoad(); //如果页面存在,则重新刷新页面
}
})
}
}
})
})
}
},
listening(index)
{
console.log("123 common.xpjAudio.currentTime<this.duration="+(common.xpjAudio.currentTime<this.duration))
if(common.xpjAudio.currentTime<this.duration)
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:parseFloat(this.currentTime)}).then(res=>{
console.log("yeahyeah")
var book=this.list[index]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
this.books_id=book._id
common.prebooks_id=this.books_id
console.log("5555books_id="+this._id)
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title",book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("listening this.books_id="+this.books_id)
this.history_change()
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
else
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:0}).then(res=>{
console.log("yeahyeah")
var book=this.list[index]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
this.books_id=book._id
common.prebooks_id=this.books_id
console.log("5555books_id="+this._id)
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title",book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("listening this.books_id="+this.books_id)
this.history_change()
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
},
// 删除列表中的书籍
cut_out(index)
{
var book=this.list[index]
console.log("book="+book)
console.log("book._id="+book._id)
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:book._id}).remove()
console.log("cout_out index="+index)
this.list.splice(index,1)
},
// 上一本书
before_books()
{
// common.xpjAudio.onCanplay(()=> {
// // 必须。可以当做是初始化时长
// common.xpjAudio.duration;
// // 必须。不然也获取不到时长
// setTimeout(() => {
// this.duration=common.xpjAudio.duration
// console.log("common.xpjAudio.duration="+common.xpjAudio.duration); // 401.475918
// }, 1000)
// })
console.log("123 common.xpjAudio.currentTime<this.duration="+(common.xpjAudio.currentTime<this.duration))
if(common.xpjAudio.currentTime<this.duration)
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:parseFloat(this.currentTime)}).then(res=>{
this.history_change()
console.log("1count="+this.count)
if(this.count==0)
{
this.count=this.list.length-1
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("2count="+this.count)
}
else
{
console.log("3count="+this.count)
this.count-=1
console.log("4count="+this.count)
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("data.src="+book.src)
console.log("data.title="+book.title)
console.log("data.author="+book.author)
console.log("data.coverImgUrl="+book.coverImgUrl)
}
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
else
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:0}).then(res=>{
this.history_change()
console.log("1count="+this.count)
if(this.count==0)
{
this.count=this.list.length-1
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("2count="+this.count)
}
else
{
console.log("3count="+this.count)
this.count-=1
console.log("4count="+this.count)
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("data.src="+book.src)
console.log("data.title="+book.title)
console.log("data.author="+book.author)
console.log("data.coverImgUrl="+book.coverImgUrl)
}
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
},
// 下一本书
next_books()
{
// common.xpjAudio.onCanplay(()=> {
// // 必须。可以当做是初始化时长
// common.xpjAudio.duration;
// // 必须。不然也获取不到时长
// setTimeout(() => {
// this.duration=common.xpjAudio.duration
// console.log("common.xpjAudio.duration="+common.xpjAudio.duration); // 401.475918
// }, 1000)
// })
console.log("123 common.xpjAudio.currentTime<this.duration="+(common.xpjAudio.currentTime<this.duration))
if(common.xpjAudio.currentTime<this.duration)
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:parseFloat(this.currentTime)}).then(res=>{
this.history_change()
console.log("list.length="+this.list.length)
this.count+=1
console.log("count="+this.count)
if(this.count<this.list.length)
{
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title",book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("data.src="+book.src)
console.log("data.title="+book.title)
console.log("data.author="+book.author)
console.log("data.coverImgUrl="+book.coverImgUrl)
}
else
{
this.count=0
var book=this.list[this.count]
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
}
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
else
{
uniCloud.database().collection('audio_books').where({user_id:this.user_id,books_id:this.books_id}).get().then(res=>{
var adminList = res.result.data
var id
adminList.forEach(async(item)=>{
id=item._id
})
var _id=id
uniCloud.database().collection('audio_books').where({_id:_id}).update({finish_time:0}).then(res=>{
this.history_change()
console.log("list.length="+this.list.length)
this.count+=1
console.log("count="+this.count)
if(this.count<this.list.length)
{
var book=this.list[this.count]
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title",book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
console.log("data.src="+book.src)
console.log("data.title="+book.title)
console.log("data.author="+book.author)
console.log("data.coverImgUrl="+book.coverImgUrl)
}
else
{
this.count=0
var book=this.list[this.count]
uni.setStorageSync("listen_src",book.src)
uni.setStorageSync("listen_title", book.title)
uni.setStorageSync("listen_author",book.author)
uni.setStorageSync("listen_coverImgUrl",book.coverImgUrl)
uni.setStorageSync("listen_id",book._id)
this.data.src=book.src
this.data.title=book.title
this.data.author=book.author
this.data.coverImgUrl=book.coverImgUrl
this.url=book.src
common.prebooks_id=book._id
}
uni.redirectTo({
url:'/pages/index/index',
}).then(res=>{
this.init()
this.currentTime=0
})
})
})
}
},
// 前进十五秒
like_after_fifty()
{
this.currentTime+=15
// this.xpjAudio.seek(this.currentTime)
common.xpjAudio.seek(this.currentTime)
// this.xpjAudio.play()
common.xpjAudio.play()
this.status=true
this.playImage=this.endImg
},
// 后退十五秒
like_before_fifty()
{
this.currentTime-=15
console.log("this.currentTime="+this.currentTime)
if(this.currentTime>=0)
{
// this.xpjAudio.seek(this.currentTime)
// this.xpjAudio.play()
common.xpjAudio.seek(this.currentTime)
common.xpjAudio.play()
this.status=true
this.playImage=this.endImg
}
},
// 点赞
like()
{
console.log("hahahahaha")
if(!this.like_play)
{
this.like_play=true
this.likeImg=this.likeImg_after
this.books_data.user_id=this.user_id
this.books_data.books_id=this.books_id
uniCloud.database().collection('books_like').add(this.books_data)
this.like_countt=this.like_countt+1
uniCloud.database().collection('books').where({_id:this.books_id}).update({like_count:this.like_countt})
}
else
{
this.like_play=false
this.likeImg=this.likeImg_before
uniCloud.database().collection('books_like').where({user_id:this.user_id,books_id:this.books_id}).remove()
if(this.like_countt)
{
this.like_countt=this.like_countt-1
uniCloud.database().collection('books').where({_id:this.books_id}).update({like_count:this.like_countt})
}
}
},
//收藏
collection()
{
console.log("10qswl")
if(!this.collect_play)
{
this.collect_play=true
this.collectImg=this.collectImg_after
this.books_data.user_id=this.user_id
this.books_data.books_id=this.books_id
uniCloud.database().collection('books_collect').add(this.books_data)
this.collect_countt=this.collect_countt+1
uniCloud.database().collection('books').where({_id:this.books_id}).update({collect_count:this.collect_countt})
}
else
{
this.collect_play=false
this.collectImg=this.collectImg_before
// error:collection().where().field().remove()命令不正确,请检查方法名及调用顺序
// 将field这句删掉
// uniCloud.database().collection('books_collect').where({user_id:this.user_id,books_id:this.books_id}).field("_id,user_id,books_id").remove()
uniCloud.database().collection('books_collect').where({user_id:this.user_id,books_id:this.books_id}).remove()
if(this.collect_countt)
{
this.collect_countt=this.collect_countt-1
uniCloud.database().collection('books').where({_id:this.books_id}).update({collect_count:this.collect_countt})
}
}
},
//开始播放事件
startPlay() {
// this.currentTime=common.xpjAudio.currentTime
console.log("4common.xpjAudio.src="+common.xpjAudio.src)
console.log("4this.status="+this.status)
console.log("4nowcurrentTime="+this.nowcurrentTime)
console.log("common.presrc="+common.presrc)
console.log("lag="+this.lag)
if(this.status) {
console.log("1this.status="+this.status)
// this.xpjAudio.pause();
common.xpjAudio.pause(this)
this.status = !this.status;
this.playImage = this.startImg;
}
else if(common.presrc==common.xpjAudio.src&& this.lag==1){
// this.status=false
console.log("2this.status="+this.status)
uni.$emit('stop')
// this.xpjAudio.seek(this.currentTime)
// this.xpjAudio.play()
this.currentTime=this.nowcurrentTime
console.log("10currentTime="+this.currentTime)
common.xpjAudio.seek(this.currentTime)
common.xpjAudio.play()
this.playImage = this.startImg
this.lag=0
}
else
{
console.log("2this.status="+this.status)
uni.$emit('stop')
// this.xpjAudio.seek(this.currentTime)
// this.xpjAudio.play()
console.log("11this.currentTime="+this.currentTime)
common.xpjAudio.seek(this.currentTime)
common.xpjAudio.play()
this.playImage = this.endImg
}
},
//进入可播放状态
onCanplay() {
// this.xpjAudio.onCanplay(() => {
// this.xpjAudio.duration;
// setTimeout(()=>{
// this.duration = this.xpjAudio.duration;
// })
// })
common.xpjAudio.onCanplay(() => {
common.xpjAudio.duration;
setTimeout(()=>{
this.duration =common.xpjAudio.duration;
})
})
},
//音频播放进度更新
onTimeUpdate() {
// this.xpjAudio.onTimeUpdate(() => {
// this.currentTime = this.xpjAudio.currentTime;
// })
common.xpjAudio.onTimeUpdate(() => {
this.currentTime = common.xpjAudio.currentTime;
console.log("common.xpjAudio.currentTime="+common.xpjAudio.currentTime)
})
},
//播放结束事件
onEnded() {
// this.xpjAudio.onEnded(()=> {
common.xpjAudio.onEnded(()=> {
this.status = false;
this.currentTime = 0;
this.books_data.user_id=this.user_id
this.books_data.books_id=this.books_id
uniCloud.database().collection('books_finish').where({user_id:this.user_id,books_id:this.books_id}).field("_id,user_id,books_id").get().then(res=>{
if(res.result.data.length==0)
{
uniCloud.database().collection('books_finish').add(this.books_data)
}
})
console.log("wawawawa")
})
},
// 拖拽播放事件
changeAudio(e) {
let paused = common.xpjAudio.paused;
// this.xpjAudio.pause();
// this.xpjAudio.seek(e.detail.value)
common.xpjAudio.pause();
common.xpjAudio.seek(e.detail.value)
if(!paused) {
// this.xpjAudio.play();
common.xpjAudio.play();
}
},
format(num) {
return '0'.repeat(2 - String(Math.floor(num / 60)).length) + Math.floor(num / 60) + ':' + '0'.repeat(2 -
String(Math.floor(num % 60)).length) + Math.floor(num % 60)
},
},
destroyed(){
// this.xpjAudio.stop();
common.xpjAudio.stop();
},
watch:{
url(newVal){
console.log(newVal)
if(newVal){
// this.xpjAudio.stop();
// this.xpjAudio.src = newVal;
common.xpjAudio.stop();
common.xpjAudio.src = newVal;
this.currentTime = 0;
this.onTimeUpdate();
this.onCanplay();
this.status = false
}
}
}
}
</script>
<style lang="scss">
.position
{
position: -webkit-sticky;
position: sticky;
top: var(--window-top);
z-index: 99;
}
.make_up_list
{
background-color: #f6f6f6;
height: 8px;
width: 100%;
}
.list_content
{
// background-color: #f9f9f9;
}
.music-head
{
width: 100%;
height: 60px;
font-size: 16px;
line-height: 80px;
text-align: center;
color: black;
position: relative;
/* margin-top: 28px; */
background-color: #f9f9f9;
}
.title
{
margin-top: 8px;
margin: 0 auto;
max-width: 200px ;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 10em;
}
.column
{
line-height: 100px;
color: black;
margin-top: -36px;
}
.music-head-icon
{
position: absolute;
left: 8px;
top: 22px;
width: 30px;
height: 30px;
// background: gray;
color: white;
line-height: 100px;
border-radius: 15px;
display: flex;
justify-content: space-evenly;
}
.left
{
width: 40rpx;
height: 40rpx;
margin-top: 5px;
}
.detail-play
{
width:380rpx;
height:380rpx;
margin:50rpx auto 0 auto;
}
.detail-play image
{
width:300rpx;
height:300rpx;
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
margin: auto;
margin-top: 100px;
}
// .audio_list
// {
// background-color: #f5f6f6;
// }
.books
{
display: flex;
flex-direction: column;
margin-top: -6%;
}
.books-title {
display: flex;
align-items: center;
// justify-content: space-between;
// margin-top: 24rpx;
// margin-left: 300rpx;
font-weight: bold;
font-size: 34rpx;
// margin-top: 50rpx;
text-align: center;
// max-width: 60px;
// overflow: hidden;
// text-overflow: ellipsis;
// white-space: nowrap;
// margin-left:40%;
// margin-top: -10%;
margin: 0 auto;
// top: 140%;
// transform: translate(230%,-60%);
}
.books-author
{
color: #999;
font-size: 26rpx;
margin: 0 auto;
// transform: translate(700%,-50%);
// margin-left:30%;
// margin-top: -2%;
// margin-top: 10rpx;
// margin-left: 320rpx;
// text-align: left;
// margin-bottom: 18rpx;
// max-width: 100%;
}
.xpj-audio-box {
display: flex;
justify-content: space-between;
align-items: center;
background: white;
height: 100%;
padding: 0 15rpx;
.time-text{
font-size: 35rpx;
color: #000000;
}
.audio-action{
flex: 1;
}
}
.play-button
{
display: flex;
flex-direction: row;
justify-content: center;
margin-top: 30rpx;
}
.play_icon
{
width: 90rpx;
height: 90rpx;
margin-top: -5upx;
margin-left: 10rpx;
margin-right: 10rpx;
}
.play-icon
{
width: 80rpx;
height: 80rpx;
margin-left: 10rpx;
margin-right: 10rpx;
}
.list-radio
{
display: flex;
flex-direction: row;
margin: 15px 0 5px 5px;
}
.list-image_list
{
width: 40px;
height: 40px;
}
.list-image
{
// left: 6%;
// top: 92%;
// transform: translate(-200%, -200%);
// margin-left: 30%;
margin-left: 5upx;
margin-top: 10upx;
}
.list-content
{
font-size: 28px;
// font-weight: bold;
margin-left: 10px;
}
.list
{
display: flex;
flex-direction: row;
// width: 80px;
height: 100upx;
width: 100%;
margin-bottom: 0px;
}
.list-content-sum
{
display: flex;
flex-direction: row;
align-items: center;
}
.list-content-title
{
// font-weight: bold;
font-size: 14px;
white-space: nowrap;
margin-top: 5upx;
margin-left: 30upx;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 8em;
}
.list-content-author
{
font-size: 12px;
white-space: nowrap;
color: #999;
margin-top: 5upx;
// margin-left: 5upx;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 10em;
}
.list_amout
{
position: absolute;
// left: 7.5%;
// top: 100%;
// transform: translate(-200%, -200%);
margin-left: 20upx;
margin-top: 6upx;
}
.play_image
{
position: absolute;
left: 90%;
top: 125%;
transform: translate(-200%, -200%);
}
.dalete_image
{
// width: 50rpx;
// height: 50rpx;
position: absolute;
left: 105%;
top: 140%;
transform: translate(-200%, -200%);
}
</style>
0 个回复