• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

微信小程序(音乐)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

用微信小程序做个简单的音乐

用微信小程序完成搭建后首先在app.josn的pages里加个 "pages/indexprojrct/indexprojrct"保存。

 

 

 

你会发现它自动跟你生成。

 

 

 在indexprojrct.wxml:

<view  class="songCss"  style="flex-direction:column;">
    
    <input class="kb" placeholder="请输入歌名" name=\'songName\' bindinput="insongname"/>
     
  <button catchtap="looksong">搜索</button>
 
<view>
<audio name="{{playing.name}}" author="{{playing.author}}" src="{{playing.url}}" id="audioCtx" controls loop bindtap="isplaySong"></audio>
</view>

<view wx:for=\'{{songList}}\' data-music="{{item}}" data-author="{{item.artists[0].name}}" bindtap="playSong">
    <text>歌名:{{item.name}}</text>
    <text>歌手:{{item.artists[0].name}}</text>
    <view class="kb"></view>
    </view>
</view>

在indexprojrct.js:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    audioCtx:{},
    playing:{
      name:\'\',
      url:\'\',
      author:\'\'
    },//当前播放的音乐的地址
    songName: "",
    songList:[],//歌曲列表


  },
  insongname(e) {
    this.setData({
      songName: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */ 
  onLoad: function (options) {
    var that = this;
  },
  looksong: function (e) {
    // console.log(this.data.songName);
    var songname = this.data.songName;
    // console.log(songname);
    var that=this;
    wx.request({
      //url: \'http://musicapi.leanapp.cn/search?keywords=\' + songname,
      url:\'http://musicapi.xiecheng.live/search?keywords=\'+songname,
      success: function (res) {
       console.log(res.data.result.songs);
       var list=res.data.result.songs;
       that.setData({
         songList:list
       })
      }
    })


  },
  //播放音乐
  playSong(e){
    let {music} =e.currentTarget.dataset;
    let {author} =e.currentTarget.dataset;

   // console.log(\'https://music.163.com/song/media/outer/url?id=\'+id);
    var that=this;
    
    wx.request({
     // url: \'https://music.163.com/song/media/outer/url?id=\'+id,
     url:\'https://autumnfish.cn/song/url?id=\'+music.id,
      header: { \'content-type\': \'application/x-www-form-urlencoded\' },
      method:\'GET\',
      dataType:\'json\',
      success:function(res){
        console.log(res);
        var musicItem=that.data.playing;

        musicItem.url= res.data.data[0].url;
        musicItem.name=music.name;
        musicItem.author=author;
        that.setData({
          playing:musicItem
        })
        
        that.data.audioCtx.play();
        that.data.isPlay=true;
      }
    })
    
  },

  isplaySong(){    
      var b= !this.data.isPlay;
      // console.log(b);
      this.setData({
        isPlay:b
      })
      if(b){

        this.data.audioCtx.play();
      }else{
        this.data.audioCtx.pause();
      }
      
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.data.audioCtx=wx.createAudioContext(\'audioCtx\')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

 

在indexprojrect.wcss里你可以装饰你的页面了

 

 

  

 

 

 

 这样就完成了!!!

 

 

 本人新手不喜,别喷。

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序后台音乐播放注意事项发布时间:2022-07-18
下一篇:
[小程序]——音乐播放器发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap