(IOS) Swift Music Program Analysis

Source: Internet
Author: User

This article mainly shares the landlord in the study of swift programming process, on GitHub on a open source app Swift music research experience.

Project Address: Https://github.com/xujiyao123/SwiftMusic

I. Introduction of the Project

This project mainly implements the Song keyword query song, by using

Http://h5.kaolafm.com/v3api/api/search?words=\ (SEARCHSTR) &pagesize=30&pagenum=\ (page) &searchType=0 &_=1419749689828

Http://v3.kaolafm.com/api/play/\ (MUSICID)

These two APIs get the song details and play them. and the specified video content is accessed and played through the built-in URL.

Second, the project structure analysis

The reason why the landlord chose to study this project is because in the learning process found this project is a more typical MVC model, and all the view parts are implemented by code rather than storyboard, with a certain learning value. Next, this article will follow the model, View, Control, three parts detailed analysis of how this project through the MVC model to achieve query songs, get songs, play songs and video functions.

1. Model (MODELS)

  

In the Model section, the author provides a total of 4 files.

  

Videos.plist is a plist file that contains video data that can be played.

  

In Webimagecategory.swift, the author expands the class of Uiimageview, adding a method Setwebimagewithurlstr (urlstr:string?) that takes a picture and loads it through a URL. > Void.

  

  

In Musicmodel.swift, the author defines two classes, namely: Musicmodel and Musicplaymodel.

The Musicmodel class is the object used to build the songs that the keyword searches to. It contains 5 properties of the song and defines a class method Loadmusiclistdatawithclouse (Searchstr:string, Page:int, Clouse: (Data:nsmutablearray, Havenext:int), void by passing in the song keyword and searching for the number of pages to get the details of the song, create the Musicmodel object separately and load it into a nsmutablearray. Pass the resulting Nsmutablearray to the method parameter Clouse for next steps. By introducing the method parameter Clouse (clousure), the author skillfully reads and encapsulates the data and separates the operation after the data.

The Musicplaymodel class is the object that is used to build the songs obtained through MusicID. It contains the Title,pic,mp3url three properties and defines the class method Loadmusicdatawithclouse (Musicid:string, Clouse: (Data:nsmutablearray), Void)- >void through the incoming MusicID, gets the song information and establishes the Musicplaymodel object and passes the loading Nsmutablearray to the method parameter Clouse (clousure) for the next step.

  

In Videomodel.swift, the author defines a Videomodel class, and also uses the pain Musicmodel similar means to process the video information data and pass it to Clouse. Unlike Musicmodel, in void Loadvideodatawithclouse (Clouse: (Dataarr:nsmutablearray), void), the author passes Dispatch_ Async adds an asynchronous task to process the video data in the videos.plist.

By analyzing the 4 files in the model, it is not difficult to find out that the author defines the model of the data and accepts the subsequent operation of the data by means of closure (clousure) to achieve the purpose of separating the model from the rest. Thus, the use of method parameters (closure clousure) is a good way to implement MVC.

2. View (views)

   

The View section contains a total of 2 files.

  

In the Xutableviewcell.swift file, the author creates a UITableViewCell and defines its display and populates the Videomodel and Musicmodel objects into the cell.

  

  

  

  

The protocol musicplaydelegate is first defined in Musicplayermanager.swift, which contains 4 methods. In the Musicplaymanager class, the protocol proxy is defined delegate,player:avplayer! and Var timer:nstimer!. A Musicplayermanager () is obtained through the class variable shared. Method Playwithmodel (Model:musicplaymodel) is used to play music and use a timer every 1s to start the playing method. The playing () method is used to determine whether the playback ends and uses the proxy call protocol method Playmanager (Manager:musicplayermanager, begintoplayprogress progress:double) or Playmanagerplayisend (Manager:musicplayermanager) and passes itself as a parameter to the Protocol method. Pause () and Goon () control the player pause and play, respectively, and invoke the associated protocol method with the proxy as a parameter. Changevaluewithprogress (Value:float64)->void controls the player's playback progress by passing in the parameters.

The author mainly defines UITableViewCell and Musicplayermanager in the View section and separates the player itself from the rest of the program by using the Proxy protocol method. In the MVC model, it is also a good idea to use proxies to pass values and invoke protocol methods.

3. Controller

  

In the controller, the author gives 4 files that correspond to the 4 interfaces in this project.

  

  

The rootviewcontroller.swift corresponds to the main menu of the project. Rootviewcontroller.swift, set the background color of the page, define and instantiate the Uinavigationcontroller () and two UIButton (), The trigger method is defined for two UIButton to jump to the music and film faces of the page to the button respectively.

  

  

The rootviewcontroller.swift corresponds to a video interface. This defines and instantiates the Uitableviewcontroller, by calling Videomodel.loadvideodatawithclouse (Clouse: (dataarr:nsmutablearray)-Void The void method gets the video data and loads the data into the cell via method Drawcellwithmodel (Model:videomodel)->void and populates it with the Uitableviewcontroller. The function of TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) is also defined to realize video playback.

  

  

  

  

The musiclistviewcontroller.swift corresponds to the music search interface, which is similar to the video interface and defines the Uitableviewcontroller and calls the Cell.drawmusicwithmodel (model: Musicmodel)->void to be populated. Unlike the video interface, Textfild () is defined and instantiated by the author in the music Search interface to enter search keywords and define LoadData (Page:int?)->void Method call Musicmodel.loadmusiclistdatawithclouse (searchstr:string, Page:int, Clouse: (Data:nsmutablearray, HaveNext:Int) , void to get the music list. The author also defines the Select cell method for jumping to the music playback interface. Call the Zlswifthrefresh method to implement the list drop-down refresh feature.

  

  

  

  

  

In Musicviewcontroller.swift, the author instances two UIButton for play and pause, one uiimage to display the song picture and rounded and rotated uiimage (rotate animation +nstimer trigger), a UISlider () to display and control playback progress. By using Musicplaymodel.loadmusicdatawithclouse (Musicid:string, Clouse: (Data:nsmutablearray), Void)->Void method to get the data and information to play the song. Use the Musicplayermanager.shared property to get Musicplayermanager (), call the method in Musicplayermanager () to control the player play pause effect, and obtain the Protocol agent, Implement 4 methods in the agent at the same time.

The author defines and instances each controller contained in 4 pages in 4 files of the controller. The methods in the model are called by different instructions to get the data, and the data is passed to the cell and player in the view for display and playback.

Third, the project music part of the running flow chart

  

Iv. improvements to the project section

1, the project in the URL Communication section is still using nsurlconnection and does not use a more sophisticated nsurlsession.

2, the project UI and function is relatively simple.

3, although the Musicplayerdelegate protocol is defined, it is not given its practical function in the actual implementation process. The interaction between view and controller is also accomplished by passing Musicplayermanager ().

V. Summary

Although this project is relatively simple in function, it follows the MVC model well in the structure of program design, and through Clousure, proxy protocol, object transfer, etc., to achieve the interaction between model, view and controller. It's worth learning. The author completely uses the code to design the interface, is also worthy of reference, after all, sometimes using code design interface than storyboard will come more convenient.

(IOS) Swift Music Program Analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.