1. Open Project Qqmusic, then click menu: "File-new-target" to add applewatch extension
2. Select the swift language to remove the Notification before the include scene (the project does not need to be notified on iOS)
3. Add the required class file for the Qqmusic project in the compile Sources of WatchKit extension
4. Add a resource image to the images.xcassets of the Qqmusic WatchKit extension package
and add images to the Qqmusic WatchKit app Images.xcassets.
(The difference between the two is that WatchKit extension's picture is called in the code, and the WatchKit app's picture is storyboard call)
5. Open Interface.storyboard for layout
6. Associating individual controls 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. Load the network MP3 list
Overridefunc willactivate () {super.willactivate ()iftabledata.count==0 { //Request list DataProvider.getsonglist {(results) ()inchLet Errorcode:nsinteger=results["Error_code"] asNsinteger let Result:nsdictionary=results["result"] asnsdictionaryif(errorcode==22000) {Let Resultdata:nsarray= result["songlist"] asnsarray var list=[Song] () for(Var index:int=0; index<resultdata.count;index++) {var dic:nsdictionary= Resultdata[index] asnsdictionary var song:song=Song () song.setvaluesforkeyswithdictionary (DIC asnsdictionary) List.append (song)} Self.tabledata=list Self.setcurrentsong (list[0] asSong)} } } }
Description: The Willactivate method is called before the page is displayed.
8. Load the current MP3 file, and the lyrics data
func Setcurrentsong (song:song) {Titlelabel.settext ("\ (song.title)-\ (song.author)") Iconimage.setimage (Getimagewithurl (Song.pic_premium)) self.audioPlayer.stop () isplaying=falseSelf.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) } //get current MP3func getCurrentMp3 (song:song) {Provider.getsongmp3 (song, Reciveblock: {(results)()inchSelf.audioPlayer.contentURL=nsurl (string: Results)}) } //Get lyricsfunc GETCURRENTLRC (lrclick:nsstring) {Currentlrcdata=parselyricwithurl (Lrclick)? }
//Update Play Timefunc updatetime () {//Show Lyrics ifCurrentlrcdata! =Nil {let C=Audioplayer.currentplaybacktimeifC>0.0{Let All:int=Int (c)//find the first bar larger than the current number of secondsvar predicate:nspredicate = nspredicate (format:"Total <%d", all)!var lrclist:nsarray= currentlrcdata!. Filteredarrayusingpredicate (predicate)ifLrclist.count >0{var LRCLINE:SONGLRC= Lrclist.lastobject asSONGLRC Lrclabel.settext (Lrcline.text)}} } }
9. Play and pause the current song
@IBAction func Playsong () {if(isplaying==true){ //Stop playingSelf.audioPlayer.pause () self.playButton.setBackgroundImage (UIImage (named:"Player_btn_play_normal.png")) IsPlaying=false }Else{ //Start/Resume playbackSelf.audioPlayer.play () self.playButton.setBackgroundImage (UIImage (named:"Player_btn_pause_normal.png")) IsPlaying=true } }
10. Enable playback of the next song
// previous if (Currentindex>0 ) {currentindex --} C Urrentsong =tabledata[currentindex] Setcurrentsong (currentsong)} // next @IBAction func Nextsong () { if (Curren Tindex < Tabledata.count) {currentindex ++} currentsong =tabledata[currentindex] Setcurrentsong (Currentso NG)}
11. Create a new Interfacecontroller in storyboard and drag a Table control (used to display a list of all songs) to associate it Songlistcontroller
Import Foundationimport WatchKitclassSonglistcontroller:wkinterfacecontroller {var dataSource=[Song] ()//List Control@IBOutlet weak var table:wkinterfacetable!//to receive the data passed over OverrideFunc Awakewithcontext (context:anyobject?) {super.awakewithcontext (context)ifLet data = Context as?[song]{Self.datasource=Data}} //Display list Data Overridefunc willactivate () {super.willactivate () table.setnumberofrows (Datasource.count, Withrowtype:"Songrowtype") for(Index, song)inchEnumerate (dataSource) {ifLet row = Table.rowcontrolleratindex (index) as?Songrowcontroller {row.titleLabel.setText (song.title) Row.subTitleLabel.setText (song.a uthor) }}}//Click on a row to return Overridefunc table (table:wkinterfacetable, Didselectrowatindex rowindex:int) {Let song=Datasource[rowindex] Self.dismisscontroller ()}}
12. Associate the Rowcontroller in the table control Songrowcontroller.swift
Import Foundationimport WatchKit class Songrowcontroller:nsobject { @IBOutlet weak var titlelabel:wkinterfacelabel! @IBOutlet weak var Subtitlelabel:wkinterfacelabel! }
13. Hold down the control key, drag from each controller to the second, page jump
14. In Interfacecontroller.swift in the Contextforseguewithidentifier method to set the jump page of the pass parameter
//page Use storyboard jump Shishun OverrideFunc Contextforseguewithidentifier (segueidentifier:string), Anyobject?{NSLog ("segueidentifier%@", Segueidentifier)ifSegueidentifier = ="Songlistsegue"{ returnSelf.tabledata}Else{ returnNil}}
15. The Awakewithcontext method in the Songlistcontroller.swift, receive the passed parameters.
// to receive the data passed over override func awakewithcontext (context:anyobject?) { Super.awakewithcontext (context) if? [song]{ self.datasource=data } }
16. Data binding for the song list
// Display list Data Override func willactivate () { super.willactivate () "songrowtype") for in enumerate (dataSource) { if? Songrowcontroller { row.titleLabel.setText (song.title) row.subTitleLabel.setText (song.author) } } }
The final effect is as follows:
Source: Http://download.csdn.net
Reprint please indicate source: http://www.cnblogs.com/wuxian/p/4418116.html
Swift Combat-QQ Online music (AppleWatch version)