## 音乐播放
![](https://box.kancloud.cn/09b27c0482875e87c1c77d1a3886720f_862x716.gif)
[TOC]
### index.wxml
```
<music title="{{music.title}}" url="{{music.url}}"></music>
```
>传递音乐的标题和音源地址
### index.json
```
{
"usingComponents": {
"music":"/components/music/music"
}
}
```
>注册music组件
### index.js
```
import {local_data} from "../../data/local";
//引入外部数据js文件
const app = getApp()
Page({
data: {
music:local_data.music
},
onLoad(){
console.log(this.data.music)
}
})
```
### music.js
```
// components/music/music.js
const audio = wx.getBackgroundAudioManager();
Component({
properties: {
title:String,
url:String
},
//注册index页面传递进来的数据title、url
data: {
isPlay:false
},
methods: {
onMusic(){
if(this.data.isPlay){
this.setData({
isPlay:false
})
audio.pause();
}
else{
audio.title = this.properties.title;
audio.src = this.properties.url;
//设置title、src属性之后可自动播放,不需要写play
this.setData({
isPlay:true
})
}
}
},
//attached相当于onload,该组件加载时进行音乐监听
attached(){
audio.onPlay(()=>{
this.setData({
isPlay:true
})
})
audio.onPause(()=>{
this.setData({
isPlay:false
})
})
audio.onStop(()=>{
this.setData({
isPlay:false
})
})
audio.onEnded(()=>{
this.setData({
isPlay:false
})
})
}
})
```
### music.wxml
```
<!--components/music/music.wxml-->
<view class="container">
<image class="banner {{isPlay?'rotate':''}}" src="/images/banner.png"></image>
<image bindtap="onMusic" class="play" src="{{isPlay?'/images/play.png':'/images/pause.png'}}"></image>
<text class="text" decode="{{true}}">谁念过 千字文章\n秋收冬已藏</text>
</view>
```
>为音乐封面添加旋转样式,当判断音乐在播放时封面旋转
### music.wxss
```
.container{
position: relative;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 100rpx;
}
.banner{
width: 500rpx;
height: 500rpx;
border-radius: 50%;
}
.play{
width: 100rpx;
height: 100rpx;
position: absolute;
top:27%;
}
.text{
margin-top: 100rpx;
line-height: 80rpx;
color:gray;
font-size: 35rpx;
text-align: center;
}
.rotate{
animation: rotate 12s linear infinite
}
@keyframes rotate {
from{
transform: rotate(0deg)
}to{
transform: rotate(360deg)
}
}
```
### data>local.js
```
var local_data ={
music: {
url: "http://music.163.com/song/media/outer/url?id=571338279",
title: "往后余生"
}
};
export {local_data}
```
- 项目启动配置
- 01.欢迎页
- 03.点击图片切换——事件绑定bindtap、catchtap
- 04.底方分页切换tabBar
- 20.音乐播放
- 06.豆瓣电影列表
- 1.App
- 2.template
- 3.movies-detail 页面详情
- 组件
- 02.地图
- 05.轮播图swiper
- 07.点击预览图片previewImage结合scroll-view
- 11.web-page
- 14.form表单
- 08.头像图片更换chooseImage
- 09.上拉刷新OnReachBottom
- 10.使用缓存(收藏、分享状态)
- 12.导航条加载、页面加载、导航标题设置
- 13.弹出框
- 15.demo
- data
- local.js
- pages
- welcome(欢迎页)
- index
- index.wxml
- index.wxss
- index.js
- index-detail
- index-detail.wxml
- index-detail.wxss
- index-detail.js
- template
- yuedu-list.wxml
- yuedu-list.wxss
- map
- movies
- movies.wxml
- movies.wxss
- movies.js
- movies-grid
- movies-grid-template.wxml
- movies-grid-template.wxss
- movies-item
- movies-item-template.wxml
- movies-item-template.wxss
- movies-more
- movies-more.wxml
- movies-more.wxss
- movies-more.js
- movie-detail
- movie-detail.wxss
- movie-detail.wxml
- movie-detail.js
- star
- star-template.wxml
- star-template.wxss
- web-page
- web-page.wxml
- web-page.js
- post-img
- post-img.wxml
- post-img.js
- post-img.wxss
- 演员列表图
- 列表图.wxml
- 列表图.wxss
- 列表图.js
- utils
- utils.js
- app.js
- app.json
- app.wxss
- 16.事件冒泡
- 18.Tip
- 框架
- 17.模板
- 19.组件模板和样式(slot)
- component构造器
- 组件事件
- behaviors
- wxs
- HTTP封装
- models使用封装HTTP
- 更多有关promise封装的例子
- 生命周期
- 注意: