查看: 35130|回复: 0

Swift功能代码《简单百度音乐 ZLMusic》实现完整源码下载

[复制链接]
  • TA的每日心情
    开心
    2014-12-18 00:08
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    40

    主题

    42

    帖子

    516

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    516
    发表于 2015-1-13 00:17:01 | 显示全部楼层 |阅读模式
    源码简介
    用Swift开发的百度音乐电台,很好拿来研究学习不错。

    源码运行截图

    点击下载源码

    源码片段

    1. func stopPlayMusic() {
    2.     if (self.isStop == true) {
    3.         self.player.pause()
    4.         btn .setTitle(“播放”, forState:UIControlState.Normal)
    5.         //var paused = player.currentTime
    6.         self.songImageView.layer.speed = 0.0
    7.     }else{
    8.         self.player.play()
    9.         btn .setTitle(“暂停”, forState:UIControlState.Normal)
    10.         self.songImageView.layer.beginTime = player.currentTime
    11.         self.songImageView.layer.speed = 0.1
    12.     }
    13.     isStop = !isStop
    14.    }
    15.    func downloadData(path: NSString,dataHandler: (NSData)->Void){
    16.         var url = NSURL(string: path)
    17.         var request = NSURLRequest(URL: url)
    18.         var mainQueue = NSOperationQueue.mainQueue()
    19.         NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    20.             (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
    21.             var httpResponse = response as NSHTTPURLResponse
    22.             if httpResponse.statusCode == 200 {
    23.                dataHandler(data)
    24.             }
    25.         }
    26.     }
    27.     func downloadSong(path: NSString) {
    28.         self.downloadData(path, dataHandler: { (data: NSData) -> Void in
    29.             self.playSong(data)
    30.         })
    31.     }
    32.     func downloadSongLrc(path: NSString) {
    33.         var lrcPath = “http://ting.baidu.com\(path)”
    34.         self.downloadData(lrcPath, dataHandler: { (data: NSData) -> Void in
    35.             var lrc = NSString(data: data, encoding:NSUTF8StringEncoding)
    36.             var lrcLists = lrc.componentsSeparatedByString(“\n”) as NSArray
    37.             println(“\(lrc)”)
    38.             println(“\(lrcLists[5])”)
    39.         })
    40.     }
    41.     func loadSongMessage(id: Int) {
    42.         var path = “http://music.baidu.com/data/music/fmlink?type=mp3&rate=1&format=json&songIds=\(id)”
    43.         downloadData(path, dataHandler: { (data: NSData) -> Void in
    44.             var array: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error:nil) as NSDictionary
    45.             var dataDic: NSDictionary = array[“data”] as NSDictionary
    46.             var list:NSArray = dataDic[“songList”] as NSArray
    47.             var songDic:NSDictionary = list[0] as NSDictionary
    48.             self.currentSong.refreshSong(songDic)
    49.             self.title = self.currentSong.songName
    50.             println(“\(songDic)”)
    51.             self.playImage(self.currentSong.songPicRadio)
    52.             self.showSongInfo()
    53.             self.downloadSong(self.currentSong.songLink)
    54.             self.downloadSongLrc(self.currentSong.lrcLink)
    55.         })
    56.     }
    57.     func playImage(path: NSString) {
    58.         if path == “” {
    59.             self.songImageView.backgroundColor = UIColor.cyanColor()
    60.         }else{
    61.             var url = NSURL(string: path)
    62.             var data = NSData(contentsOfURL: url, options: nil, error: nil)
    63.             var image = UIImage(data: data)
    64.             self.songImageView.image = image
    65.         }
    66.         self.songImageView.contentMode = UIViewContentMode.ScaleAspectFit
    67.         self.songImageView.layer.cornerRadius = self.songImageView.frame.width/2
    68.         self.songImageView.layer.masksToBounds = true
    69.         self.songImageView.userInteractionEnabled = true
    70.         var ges = UITapGestureRecognizer(target: self, action:”tapImageView:”)
    71.         self.songImageView.addGestureRecognizer(ges)
    72.     }
    73.     func showSongInfo() {
    74.         self.artistName.text = self.currentSong.artistName
    75.         self.songName.text = self.currentSong.songName
    76.         self.timeLabel.text = “00:00″
    77.     }
    78.     func loadSongList() {
    79.         var path = “http://fm.baidu.com/dev/api/?tn=playlist&special=flash&prepend=&format=json&_=1378945264366&id=“+String(channel.id)
    80.         downloadData(path, dataHandler: { (data: NSData) -> Void in
    81.             var dic: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error:nil) as NSDictionary
    82.             var list: NSArray = dic[“list”] as NSArray
    83.             for dict in list{
    84.                 var song = Song()
    85.                 song.id = dict[“id”] as Int
    86.                 self.songList.append(song)
    87.             }
    88.             if self.songList.count != 0 {
    89.                 self.currentSong = self.songList[0]
    90.                 self.loadSongMessage(self.currentSong.id)
    91.             }
    92.        })
    93.     }
    94.     func doAnimation() {
    95.         var animation = CABasicAnimation(keyPath: “transform.rotation.z”)
    96.         animation.toValue = NSNumber(double: 2.0*M_PI)
    97.         animation.duration = 1.5
    98.         animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    99.         animation.removedOnCompletion = false
    100.         animation.cumulative = false
    101.         animation.repeatCount = FLT_MAX
    102.         self.songImageView.layer.addAnimation(animation, forKey: “AnimatedKey”)
    103.         self.songImageView.layer.speed = 0.1
    104.     }
    105.     func tapImageView(sender:UITapGestureRecognizer){
    106.         self.stopPlayMusic()
    107.     }
    108.         override func viewDidLoad() {
    109.         super.viewDidLoad()
    110.         //self.title = channel.title
    111.         loadSongList()
    112.         self.view.backgroundColor = UIColor(red: 218/255.0, green: 225/255.0, blue: 230/255.0, alpha: 1)
    113.         println(“\(channel.id)”)
    114.     }
    115.     override func viewWillDisappear(animated: Bool) {
    116.         timer.invalidate()
    117.         self.songImageView.stopAnimating()
    118.         player.stop()
    119.     }
    120.     override func didReceiveMemoryWarning() {
    121.         super.didReceiveMemoryWarning()
    122.         // Dispose of any resources that can be recreated.
    123.     }
    124.      func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
    125.         timer.invalidate()
    126.         self.songImageView.layer.speed = 0
    127.         self.songImageView.stopAnimating()
    128.         var index = self.currentIndex + 1
    129.         loadSongWithIndex(index)
    130. }
    复制代码



    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    广播台
    特别关注
    快速回复 返回顶部 返回列表