<span id="Label3"></p><p><p><span style="font-size: 15px; font-family: 幼圆;">This article mainly shares the landlord in the study of Swift programming process, on GitHub on a open source project, Swift Radio Research Experience.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"></span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Project Address: Https://github.com/swiftcodex/Swift-Radio-Pro</span></p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt; font-family: 幼圆;">first, the interface</span></strong></p></p><p><p></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"></span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">A total of 6 interface controllers are included in this project, including 5 for displaying radio channels (Swift Radio), playing (now Playing view), Radio Information (info View controller), app information (Menu view Controller) and the app features and contact email (about View Controller) View controller and a navigation controller.</span></p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt; font-family: 幼圆;">second, the source of radio songs and the realization of the principle</span></strong></p></p><p><p></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">This project implements the radio function by invoking the API provided by Http://www.last.fm/api. The landlord will make a simple introduction and analysis of the API it Calls.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">1) Http://[yoururl.com]/json/stations.json is used to obtain the station information, the return format is Json.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">In this project, the author does not use this api, but rather the built-in json-formatted radio Data.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">2) http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=[api_key]&artist=[artist]&track=[ Track]&format=json Gets the song information by passing the API_KEY,ARITST and track three Parameters.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Such as:http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=9a267c245324cfa4f887366d497d3dd3& Artist=the%20gossip&track=coal%20to%20diamonds&format=json</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Where name is the song name, artist is the singer information, and album is the album Information.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">In this project, the author obtains the artist and track parameters by obtaining the metadata in the MPMoviePlayerController () being played, and then obtains the complete song information by passing parameters to the Api.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Code specific implementation</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">1) Swiftradio-settings.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">In the Swiftradio-settings.swift file, the author defines debug_log to determine if the debug log needs to be Displayed. The uselocalstatons is then defined to determine if the local radio data is used, and if Uselocalstatons is false, the station information is obtained by stationdataurl. The Apikey and Apisecret are then given two parameters for use when the song information API is called (apisecret is not used).</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">2) Stations.json</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">This document is for local radio information and can be modified to add or delete stations. Where name is the station name, Streamurl is the radio station Url,imageurl is the URL of the picture (because it is a local station, so the picture has been saved in the project file, so ImageUrl directly to the station picture file corresponding name).</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">3) Radiostation.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">A radiostation class is created that contains the property parameters of the station obtained from the radio JSON Data. The same is a constructor and a method for directly transferring the radio JSON data to a radiostation instance.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">4) Track.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Created a track structure that contains 6 track Properties. Where artworkloaded is used to record whether the song picture has been acquired, isplaying record whether the song is already playing.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">5) Datamanager.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">DataManager This class mainly contains methods for acquiring and processing network data or local Data. where Getstationdatawithsuccess (success: ((METADATA:NSDATA!)-VOID)) method to determine whether to use local station data or network radio data by accessing the value of Uselocalstations in swiftradio-settings.swift. Getdatafromfilewithsuccess (success: ((data:nsdata) and Void) and gettrackdatawithsuccess (queryurl:string, success: (( Metadata:nsdata!) (Void)), are two methods for invoking local and network radio data respectively. Loaddatafromurl (url:nsurl, completion: (data:nsdata, error:nserror?), Void) is the way to get radio data from the Network. Through the nsurlsession to obtain the network data, and then the resulting data to the method parameters contained in the equation for later processing and use.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">6) Stationsviewcontroller.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">In the Stationsviewcontroller class, the obtained radio data is analyzed and disassembled and displayed by UITABLEVIEW. Loadstationsfromjson () by calling Datamanager.getstationdatawithsuccess (success: ((METADATA:NSDATA!)-VOID)) Get the station data and save it 0 in stations:[radiostation]. The resulting data is then presented to the UITableView by the TableView method. By rewriting Prepareforsegue (segue:uistoryboardsegue, sender:anyobject?) method to complete passing the selected station to the Nowplayingviewcontroller. At the same time by completing the Nowplayingviewcontrollerdelegate two methods artworkdidupdate (track:track) and Songmetadatadidupdate (track:track) To get and display information about the currently playing Song.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">7) Nowplayingviewcontroller.swift</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Create a radioplayer = MPMoviePlayerController () and set it. When the Stationdidchange () method is called, RadioPlayer plays the media stream of the current Station.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">The metadataupdated (n:nsnotification) method is used to get the metadata that RadioPlayer is playing music, and to get the title and artist information for the music being played through a simple process. If you find Currentsongname does not match the get title, it starts playing a new song, calls Resetalbumartwork () and Queryalbumart () to get new song information, and uses Updatelockscreen ( ) to update the lock screen and use songmetadatadidupdate (self.track) to pass the new song information to Stationsviewcontroller.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Get complete information about the song by using the Api.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Resetalbumartwork () implements an update to the album art by calling Updatealbumartwork (), which is replaced with a radio cover if it fails to get the album Cover.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Use Nsurlsessiondownloadtask to download album images, save downloaded images to track.artworkimage and update cover images, lock screen interface and send new data back to Stationsviewcontroller.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Used to update buttons and labels on the Interface.</span></p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt; font-family: 幼圆;">Analysis of some characteristics of project realization</span></strong></p></p><p><p></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">1) Dynamic playing icon</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Call Animationframes class in the Createframes () method to create an object for Nowplayingimageview.animationimages. The animation effect is achieved through the startanimating () method.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">2) Open Safari in the app</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Implemented by Uiapplication.shareapplication (). OpenURL (url).</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">3) Background play, lock screen play and lock screen display song information</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">The system begins accepting remote EVENTS.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;"> </span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Allow background broadcast</span></p></p><p><p> </p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Lock screen song information settings and the control to play or pause the song when the lock screen is Accepted.</span></p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt; font-family: 幼圆;">Iv. some bugs found</span></strong></p></p><p><p></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Bugs found in the Xcode 7.0.1 iPhone 6s Plus simulator:</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">1) the built-in email function is not available and crashes immediately after you click Email to jump to the mail app.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">2) the volume Bar on the Nowplayingviewcontroller interface does not adjust the Volume.</span></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">3) when the Nowplayingviewcontroller interface is playing music on the way back to Stationviewcontroller after the pause, stationviewcontroller the lower left corner of the dynamic playing icon continues to play the animation, Then if you click right below now Playing button return nowplayingviewcontroller, The interface shows that the music is playing, but in fact the music is in a paused state, you need to click the Pause button and then click the Play button to Recover.</span></p></p><p><p></p></p><p><p><strong><span style="font-size: 18pt; font-family: 幼圆;">five, PostScript</span></strong></p></p><p><p></p></p><p><p><span style="font-size: 15px; font-family: 幼圆;">Landlord once in the study of Swift did a radio app, compared to this app, the landlord before the one that is a little rough made. Through the study of this app, not only I have a better understanding of some new features of Swift2.0, but also let me feel that the author of this project has a strong programmer Quality. The design of the code has a strong global consideration, making the code very simple and easy to understand. There was little difficulty in reading and testing because of the confusion of the Code. This kind of programming thought and the method of the design procedure is worth to learn and study in the Future.</span></p></p><p><p>(IOS) Swift2.0 Radio Program Analysis</p></p></span>
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