赞
踩
根据播放性质可分为背景音频和前台音频,背景音频在小程序最小化以后还可以继续在后台播放。
1.背景音频管理
使用wx.getBackgroundAudioManager()获取全局唯一的背景音频管理器backgroundAudioManager,backgroundAudioManager对象的属性说明如下:
backgroundAudioManager对象的方法如下:
errCode说明如下:
背景音频的简单应用:
- <!--wxml-->
- <view class="title">第6章 媒体API</view>
- <view class="demo-box">
- <view class="title">背景音频管理器</view>
- <button type="primary" size="mini" bindtap="play">播放</button>
- <button type="primary" size="mini" bindtap="pause">暂停</button>
- </view>
-
- //js
- Page({
- //初始化背景音频
- initialAudio:function(){
- let bgAudioManager = this.bgAudioManager
- bgAudioManager.title="世间美好与你环环相扣",
- bgAudioManager.epname='听闻余生',
- bgAudioManager.singer="柏松",
- bgAudioManager.coverImgUrl ='http://p1.music.126.net/DK1_4sP_339o5rowMdPXdw==/109951164071024476.jpg?param=130y130',
- bgAudioManager.src ='https://music.163.com/song/media/outer/url?id=1363948882'
- },
- //开始播放
- play:function(){
- this.bgAudioManager.play()
- },
- //暂停播放
- pause:function(){
- this.bgAudioManager.pause()
- },
- onLoad:function(options){
- this.bgAudioManager = wx.getBackgroundAudioManager()
- this.initialAudio()
- }
- })
-
- //!important app.json文件
- "requiredBackgroundModes":["audio"] //需要在后台使用的能力,如「音乐播放」
2.音频组件控制
使用wx.createInnerAudioContext创建并返回内部audio上下文对象innerAudioContext,其属性说明如下:
innerAudioContext对象的方法如下:
简单应用:
- <!--wxml-->
- <view class="title">第6章 媒体API</view>
- <view class="demo-box">
- <view class="title">音频组件控制</view>
- <button type="primary" size="mini" bindtap="play">播放</button>
- <button type="primary" size="mini" bindtap="stop">停止</button>
- <button type="primary" size="mini" bindtap="pause">暂停</button>
- </view>
-
- //js
- Page({
- //初始化音频
- initialAudio:function(){
- let audioCtx = this.audioCtx
- audioCtx.autoplay=true
- audioCtx.src ='https://music.163.com/song/media/outer/url?id=1363948882'
- audioCtx.onPlay(()=>{
- console.log('开始播放')
- })
- audioCtx.onPause(() => {
- console.log('暂停播放')
- })
- audioCtx.onStop(() => {
- console.log('停止播放')
- })
- },
- //开始播放
- play:function(){
- this.audioCtx.play()
- },
- //暂停播放
- pause:function(){
- this.audioCtx.pause()
- },
- stop:function(){
- this.audioCtx.stop()
- },
- onLoad:function(options){
- this.audioCtx = wx.createInnerAudioContext()
- this.initialAudio()
- }
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。