1.打开项目QQMusic,然后点菜单:“File-New-Target”添加appleWatch扩展项

2.选择Swift语言,把Include Notification Scene前的勾去掉 (项目暂时不需要做ios端的通知)

3.在 WatchKit Extension的Compile Sources 中添加QQMusic项目的需要使用的类文件

4.在QQMusic WatchKit Extension包的Images.xcassets里添加资源图片

并且在QQMusic WatchKit App的Images.xcassets里资源添加图片

(这两个的区别是,WatchKit Extension的图片是在代码里调用,而WatchKit App的图片是storyboard调用)

5.打开Interface.storyboard进行布局


6.关联各个控件IBOutLet

class InterfaceController: WKInterfaceController {

    @IBOutlet weak var iconImage: WKInterfaceImage!
@IBOutlet weak var titleLabel: WKInterfaceLabel!
@IBOutlet weak var lrcLabel:WKInterfaceLabel!
@IBOutlet weak var playButton: WKInterfaceButton!
}

7.加载网络mp3列表

  override func willActivate() {
super.willActivate()
if tableData.count== {
//请求列表数据
provider.getSongList { (results) -> () in
let errorCode:NSInteger=results["error_code"] as NSInteger
let result:NSDictionary=results["result"] as NSDictionary
if (errorCode==) {
let resultData:NSArray = result["songlist"] as NSArray
var list=[Song]()
for(var index:Int=;index<resultData.count;index++){
var dic:NSDictionary = resultData[index] as NSDictionary
var song:Song=Song()
song.setValuesForKeysWithDictionary(dic as NSDictionary)
list.append(song)
}
self.tableData=list
self.setCurrentSong(list[] as Song)
}
}
}
}

说明:willActivate方法是在页面显示前调用。

8.加载当前mp3文件,和歌词数据

 func setCurrentSong(song:Song){
titleLabel.setText("\(song.title)-\(song.author)")
iconImage.setImage(getImageWithUrl(song.pic_premium))
self.audioPlayer.stop()
isPlaying=false
self.playButton.setBackgroundImage(UIImage(named: "player_btn_play_normal.png"))
getCurrentMp3(song)
getCurrentLrc(song.lrclink)
timer?.invalidate()
timer=NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "updateTime", userInfo: nil, repeats: true)
} //获取当前mp3
func getCurrentMp3(song:Song){
provider.getSongMp3(song, reciveBlock: { (results) -> () in
self.audioPlayer.contentURL=NSURL(string: results)
})
} //获取歌词
func getCurrentLrc(lrclick:NSString){
currentLrcData=parseLyricWithUrl(lrclick)?
}
 //更新播放时间
func updateTime(){
//显示歌词
if currentLrcData != nil {
let c=audioPlayer.currentPlaybackTime
if c>0.0{
let all:Int=Int(c)
// 查找比当前秒数大的第一条
var predicate:NSPredicate = NSPredicate(format: "total < %d", all)!
var lrcList:NSArray = currentLrcData!.filteredArrayUsingPredicate(predicate)
if lrcList.count > {
var lrcLine:SongLrc = lrcList.lastObject as SongLrc
lrcLabel.setText(lrcLine.text)
}
}
}
}

9.播放、暂停当前歌曲

@IBAction func playSong() {
if(isPlaying==true){
//停止播放
self.audioPlayer.pause()
self.playButton.setBackgroundImage(UIImage(named: "player_btn_play_normal.png"))
isPlaying=false
}else{
//开始/继续播放
self.audioPlayer.play()
self.playButton.setBackgroundImage(UIImage(named: "player_btn_pause_normal.png"))
isPlaying=true
}
}

10.实现上一首下一首歌曲的播放

 //上一首
@IBAction func preSong() {
if(currentIndex>){
currentIndex--
}
currentSong=tableData[currentIndex]
setCurrentSong(currentSong)
} //下一首
@IBAction func nextSong() {
if(currentIndex < tableData.count){
currentIndex++
}
currentSong=tableData[currentIndex]
setCurrentSong(currentSong)
}

11.在storyBoard新建一个InterfaceController,并拖一个table控件(用于显示所有歌曲列表),将其关联SongListController

import Foundation
import WatchKit class SongListController:WKInterfaceController {
var dataSource=[Song]()
//列表控件
@IBOutlet weak var table: WKInterfaceTable! //接收传过来的数据
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let data = context as? [Song]{
self.dataSource=data
}
} //显示列表数据
override func willActivate() {
super.willActivate()
table.setNumberOfRows(dataSource.count, withRowType: "SongRowType")
for (index, song) in enumerate(dataSource) {
if let row = table.rowControllerAtIndex(index) as? SongRowController {
row.titleLabel.setText(song.title)
row.subTitleLabel.setText(song.author)
}
}
} //点击某一行返回
override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
let song = dataSource[rowIndex]
self.dismissController()
}
}
12.将table控件里的rowcontroller关联SongRowController.swift
import Foundation
import WatchKit class SongRowController:NSObject {
@IBOutlet weak var titleLabel: WKInterfaceLabel!
@IBOutlet weak var subTitleLabel: WKInterfaceLabel!
}

13.按住control键,从每一个controller拖到第二个,进行页面跳转

14.在InterfaceController.swift的contextForSegueWithIdentifier方法中设置跳转页面时的传参
   //页面使用storyBoard跳转时传参
override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
NSLog("segueIdentifier%@", segueIdentifier)
if segueIdentifier == "songListSegue"{
return self.tableData
}else{
return nil
}
}

15.在SongListController.swift中的awakeWithContext方法,接收传过来的参数

 //接收传过来的数据
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let data = context as? [Song]{
self.dataSource=data
}
}

16.进行歌曲列表的数据绑定

 //显示列表数据
override func willActivate() {
super.willActivate()
table.setNumberOfRows(dataSource.count, withRowType: "SongRowType")
for (index, song) in enumerate(dataSource) {
if let row = table.rowControllerAtIndex(index) as? SongRowController {
row.titleLabel.setText(song.title)
row.subTitleLabel.setText(song.author)
}
}
}

最终效果如下:

  

源码下载地址:http://download.csdn.net/detail/fangwulongtian/8584863

转载请注明来源:http://www.cnblogs.com/wuxian/p/4418116.html

Swift实战-QQ在线音乐(AppleWatch版)的更多相关文章

  1. Swift实战-QQ在线音乐(第二版)

    此版本使用百度音乐接口,原因是豆瓣接口很多歌曲没办法找到歌词. 此版本添加了歌词的显示.上一曲.下一曲的实现.歌曲列表指明当前歌曲. 下面来看一下实现过程>>> 一.项目准备: 百度 ...

  2. Swift实战-QQ在线音乐(第一版)

    //一.*项目准备 1.QQ音乐App 界面素材:(我使用PP助手,将QQ音乐App备份,解压ipa文件 即可得到里面的图片素材) 2.豆瓣电台接口:"http://douban.fm/j/ ...

  3. javascript版QQ在线聊天挂件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Swift实战-豆瓣电台(五)播放音乐

    观看地址 http://v.youku.com/v_show/id_XNzMwODM0MzI0.html 在这节里面,我们简单学习了一下MediaPlayer的使用 引入媒体框架 import Med ...

  5. python 开发在线音乐播放器-简易版

    在线音乐播放器,使用python的Tkinter库做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过urllib.urlopen模块打开网址,使用Json模块进行数 ...

  6. iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码

    iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...

  7. Andriod小项目——在线音乐播放器

    转载自: http://blog.csdn.net/sunkes/article/details/51189189 Andriod小项目——在线音乐播放器 Android在线音乐播放器 从大一开始就已 ...

  8. 微信公众平台中添加qq在线聊天代码

    微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...

  9. Swift实战-豆瓣电台(九)简单手势控制暂停播放(全文完)

    Swift实战-豆瓣电台(九)简单手势控制暂停播放 全屏清晰观看地址:http://www.tudou.com/programs/view/tANnovvxR8U/ 这节我们主要讲UITapGestu ...

随机推荐

  1. HOSTS文件详解【win|mac】

    hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址. hosts文件通常用于补充或取代网络中DNS的功能.和DNS不同的是,计算机的使用者可以直接 ...

  2. the computer spends over 96% of its time waiting for I/O devices to finish transferring data

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION

  3. Fingerprinting

    https://wiki.mozilla.org/Fingerprinting Fingerprinting   Contents 1 Overview 2 Data 2.1 Plugins 2.2 ...

  4. P1018 乘积最大

    开始定义状态f[i][j][k]为[i,j)区间插入k个括号,使用记忆化搜索,但是成功爆栈,得到4个mle #include <bits/stdc++.h> using namespace ...

  5. pdo封装类

    <?php //http://www.imavex.com/php-pdo-wrapper-class/ class db extends PDO { private $error; priva ...

  6. css中textarea去掉边框和选中后的蓝色边框问题的解决方法

    我们在设计网页的输入框时,有时会遇到需要把textarea的边框去掉的问题,经过测试,下面的代码是可以的. textarea{ border: solid 0px; outline:none; }

  7. $watch、$digest、$apply

    $watch.$digest.$apply $watch 代表的就是对数据源的监听,当数据源发生变化,就会触发第二个参数的回调函数 $digest 代表触发一个数据源变化的事件 $apply 代表对于 ...

  8. 记录下 QT Linux 静态编译遇到的坑

    Qt下静态编译Qt,根据我的经验,如果按照Windows下那种直接拿官方sdk安装之后的文件来编译是行不通的,需要直接下载Qt的source包,目前诺基亚的源码叫做qt-everywhere-open ...

  9. c# ToString() 用法

    string tempa = Convert.ToString(31, 2);//将10进制数31转换为2进制字符串. string strNums = int.Parse(tempa).ToStri ...

  10. Provisioning Profile

    什么是Provisioning Profile? 从字面翻译,Provisioning Profile就是配置文件的意思,它在开发者账号体系中所扮演的角色也是配置和验证的作用.如果你有开发者账号,可以 ...