2***@qq.com
2***@qq.com
  • 发布:2022-08-06 14:50
  • 更新:2022-08-06 14:50
  • 阅读:21

用uni.createInnerAudioContext()遇到的bug

分类:uni-app

为什么当用uni.createInnerAudioContext()播放音频的时候,退出该页面,音频正常播放,但当再次返回到该页面,音频的currentTime是从0开始,也是从0开始播放,并且,播放状态还是暂停状态,是背景还在播放,但跳转到该页面却不播放的诡异状态,我该咋改呢

播放页面的代码

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,  

                xpjAudio: null,  
                currentTime: 0,  
                duration: 100,  
                status: '',  
                user_id:'',  

                likeImg:'../../static/点赞-before.png',  
                likeImg_after:'../../static/点赞-after.png',  
                likeImg_before:'../../static/点赞-before.png',  

                count_like:0,  
                count_collect:0,  

                collectImg:'../../static/收藏-before.png',  
                collectImg_after:'../../static/收藏-after.png',  
                collectImg_before:'../../static/收藏-before.png',  
            }  
        },  
        props: {  
            activeColor: {  
                type: String,  
                default: '#F7B52C'  
            },  
            blockColor:{  
                type:String,  
                default:'#F7B52C'  
            },  
            backgroundColor:{  
                type:String,  
                default:'gray'  
            },  
            blockSize:{  
                type:Number,  
                default:12,  
            },  
            startImg: {  
                type:String,  
                default:'../../static/播放.png',  
            },  
            endImg: {  
                type:String,  
                default:'../../static/暂停.png',  
            }  
        },  
        onLoad(){  
            // this.status=uni.getStorageSync("back_status")  
            // console.log("this.status="+this.status)  

            console.log("this.status="+this.status)  
            console.log("this.currentTime="+this.currentTime)  

            this.xpjAudio = uni.createInnerAudioContext();  
            this.xpjAudio.src = this.url;  
            this.xpjAudio.title=this.data.title;  

            uni.setInnerAudioOption({  
                obeyMuteSwitch: false    
            })  
            this.onTimeUpdate();  
            this.onCanplay();  
            this.onEnded();  

            uni.$on('stop',()=> {  
                this.xpjAudio.stop()  
                this.status = true  
            })  

            this.user_id=uni.getStorageSync("_id")  
            console.log("user_id="+this.user_id)  

            uni.setStorageSync("books_id","62ea07b0d3209100011c68f1")  
            this.books_id=uni.getStorageSync("books_id")  

            uni.setStorageSync("src","https://static.quhouse.com/audio/6103-359339075946911-20210129121317-209600.mp3")  
            uni.setStorageSync("title", "恭喜发财")  
            uni.setStorageSync("author","樊登")  
            uni.setStorageSync("coverImgUrl","https://img.1ting.com/images/special/75/s150_f84ef5082b0420f74cd2546b986ab0fc.jpg")  

            this.data.src=uni.getStorageSync("src")  
            this.data.title=uni.getStorageSync("title")  
            this.data.author=uni.getStorageSync("author")  
            this.data.coverImgUrl=uni.getStorageSync("coverImgUrl")  
            this.url=uni.getStorageSync("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)=>{  
                    this.list.push(item)  

                    this.count=this.list.findIndex(item=>item.books_id==this.books_id)  
                    console.log("count="+this.count)  
                    // 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)  
                    //  })    
                    // }).catch(err => {console.log(err);  })  
                })  
            }).catch(err => {console.log(err);  })  

            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)  
                    })  
                }  
            })  
        },  
        methods: {  
            back()  
            {  
                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_status",this.status)  
                // uni.setStorageSync("back_currentTime",this.xpjAudio.currentTime)  

                uni.navigateTo({  
                    url:'/pages/back/back'  
                })  
            },  
            back_home()  
            {  
                console.log("2heiehihei")  
                uni.navigateTo({  
                    url: '/pages/ucenter/ucenter'  
                })  
            },  
            listening(index)  
            {  
                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  

                uni.setStorageSync("src",book.src)  
                uni.setStorageSync("title", book.title)  
                uni.setStorageSync("author",book.author)  
                uni.setStorageSync("coverImgUrl",book.coverImgUrl)  
            },  
            cut_out(index)  
            {  
                var book=this.list[index]  
                console.log("book="+book)  
                console.log("book._id="+book._id)  
                uniCloud.database().collection('audio_books').where({_id:book._id}).remove()  

                this.list.splice(index,1)  
            },  
            before_books()  
            {  
                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  

                    uni.setStorageSync("src",book.src)  
                    uni.setStorageSync("title", book.title)  
                    uni.setStorageSync("author",book.author)  
                    uni.setStorageSync("coverImgUrl",book.coverImgUrl)  

                    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  

                    uni.setStorageSync("src",book.src)  
                    uni.setStorageSync("title", book.title)  
                    uni.setStorageSync("author",book.author)  
                    uni.setStorageSync("coverImgUrl",book.coverImgUrl)  

                    console.log("data.src="+book.src)  
                    console.log("data.title="+book.title)  
                    console.log("data.author="+book.author)  
                    console.log("data.coverImgUrl="+book.coverImgUrl)  
                }  
            },  
            next_books()  
            {  
                console.log("list.length="+this.list.length)  

                // this.count=this.list.findIndex(item=>item.books_id==this.books_id)  
                // console.log("count="+this.count)  

                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  

                    uni.setStorageSync("src",book.src)  
                    uni.setStorageSync("title", book.title)  
                    uni.setStorageSync("author",book.author)  
                    uni.setStorageSync("coverImgUrl",book.coverImgUrl)  

                    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("src",book.src)  
                    uni.setStorageSync("title", book.title)  
                    uni.setStorageSync("author",book.author)  
                    uni.setStorageSync("coverImgUrl",book.coverImgUrl)  

                    this.data.src=book.src  
                    this.data.title=book.title  
                    this.data.author=book.author  
                    this.data.coverImgUrl=book.coverImgUrl  
                    this.url=book.src  
                }  
            },  
            like_after_fifty()  
            {  
                this.currentTime+=15  
                this.xpjAudio.seek(this.currentTime)  
                this.xpjAudio.play()  
                this.status=true  
            },  
            like_before_fifty()  
            {  
                this.currentTime-=15  
                console.log("this.currentTime="+this.currentTime)  

                if(this.currentTime>=0)  
                {         
                    this.xpjAudio.seek(this.currentTime)  
                    this.xpjAudio.play()  
                    this.status=true  
                }  
            },  
            // 点赞  
            like()  
            {  
                this.count_like++;  

                if(this.count_like%2)  
                {  
                    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)  
                }  
                else  
                {  
                    this.likeImg=this.likeImg_before  

                    uniCloud.database().collection('books_like').where({user_id:this.user_id,books_id:this.books_id}).field("_id,user_id,books_id").remove()  
                }  
            },  
            //收藏  
            collect()  
            {  
                this.count_collect++;  

                if(this.count_collect%2)  
                {  
                    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)  
                }  
                else  
                {  
                    this.collectImg=this.collectImg_before  

                    uniCloud.database().collection('books_collect').where({user_id:this.user_id,books_id:this.books_id}).field("_id,user_id,books_id").remove()  
                }  
            },  
            //开始播放事件  
            startPlay() {   
                // this.status=uni.getStorageSync("back_status")  
                if(this.status) {  
                    console.log("1this.status="+this.status)  
                    this.xpjAudio.pause();  
                    this.status = !this.status;  
                }else {  
                    // this.status=uni.getStorageSync("back_status")  
                    // this.currentTime=uni.getStorageSync("back_currentTime")  
                    // console.log("this.currentTime="+this.currentTime)  

                    console.log("2this.status="+this.status)  
                    uni.$emit('stop')  
                    this.xpjAudio.seek(this.currentTime)  
                    this.xpjAudio.play()  
                }  
            },  
            //进入可播放状态  
            onCanplay() {   
                this.xpjAudio.onCanplay(() => {  
                    this.xpjAudio.duration;  
                    setTimeout(()=>{  
                        this.duration = this.xpjAudio.duration;  
                    })  
                })  
            },  
             //音频播放进度更新  
            onTimeUpdate() {  
                this.xpjAudio.onTimeUpdate(() => {  
                    this.currentTime = this.xpjAudio.currentTime;  
                })  
            },  
            //播放结束事件  
            onEnded() {   
                this.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 = this.xpjAudio.paused;  
                this.xpjAudio.pause();  
                this.xpjAudio.seek(e.detail.value)  
                if(!paused) {  
                    this.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();  
        },  
        watch:{  
            url(newVal){  
                console.log(newVal)  
                if(newVal){  
                    this.xpjAudio.stop();  
                    this.xpjAudio.src = newVal;  
                    this.currentTime = 0;  
                    this.onTimeUpdate();  
                    this.onCanplay();  

                    this.status = false  
                }  
            }  
        }  
    }
2022-08-06 14:50 负责人:无 分享
已邀请:

要回复问题请先登录注册