温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

swift 豆瓣音乐开发案例

发布时间:2020-07-24 14:07:03 来源:网络 阅读:332 作者:文启领航 栏目:移动开发

搞了一天,总有把swift 豆瓣音乐开发完成。

功能:某一频道列表

swift 豆瓣音乐开发案例

所有频道列表,点击返回某一频道

swift 豆瓣音乐开发案例

工程结构图

swift 豆瓣音乐开发案例

代码如下:

ViewController.swift

import UIKit import MediaPlayer import QuartzCore // 文启领航 bjflexedu.com // qq:376610000 class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,HttpProtocol,ChannelProtocol{          @IBOutlet var btnPlay : UIImageView     @IBOutlet var tap : UITapGestureRecognizer = nil     @IBOutlet var playTim : UILabel     @IBOutlet var progressView : UIProgressView     @IBOutlet var iv : UIImageView     @IBOutlet var tv : UITableView     var tableData:NSArray = NSArray()     var channelData:NSArray = NSArray()     var p_w_picpathCache = Dictionary<String,UIImage>()     var audioPlayer:MPMoviePlayerController = MPMoviePlayerController()     var ehttp:HttpController = HttpController()          var timer:NSTimer?          @IBAction func onTap(sender : UITapGestureRecognizer) {           println("tap")                  if sender.view == btnPlay{             btnPlay.hidden = true             audioPlayer.play()             btnPlay.removeGestureRecognizer(tap)             iv.addGestureRecognizer(tap)                               } else if sender.view == iv{             btnPlay.hidden = false             audioPlayer.pause()             btnPlay.addGestureRecognizer(tap)             iv.removeGestureRecognizer(tap)                  }                       }          override func viewDidLoad() {         super.viewDidLoad()         ehttp.delegate = self         ehttp.onSearch("http://www.douban.com/j/app/radio/channels")         ehttp.onSearch("http://douban.fm/j/mine/playlist?channel=0")         progressView.progress = 0.0         iv.addGestureRecognizer(tap)              }     func onChangeChannel(channel_id:String){      let url:String = "http://douban.fm/j/mine/playlist?\(channel_id)"        ehttp.onSearch(url)                   }     override func   prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {         var channelC:ChannelController = segue.destinationViewController as ChannelController         channelC.channelData = self.channelData         println("d")         channelC.delegate = self     }          override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()         println("dd")                       }     func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{               return self.tableData.count     }       func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{      var cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "douban")         let rowData:NSDictionary = self.tableData[indexPath.row] as NSDictionary         cell.text = rowData["title"] as String         cell.detailTextLabel.text = rowData["artist"] as String         cell.p_w_picpath = UIImage(named:"detail.jpg")         let url = rowData["picture"] as String         let p_w_picpath = self.p_w_picpathCache[url] as?  UIImage                  if !p_w_picpath? {             let imgURL:NSURL = NSURL(string: url)             let request:NSURLRequest = NSURLRequest(URL: imgURL)             NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {                      (response, data, error) -> Void  in                 let img = UIImage(data: data)                 cell.p_w_picpath = img                 self.p_w_picpathCache[url] = img                                  })                      }else{                      cell.p_w_picpath = p_w_picpath //self.p_w_picpathCache["url"]         }                          return cell     }     func didRecieveResults(results:NSDictionary){              println("dd")         println(results);         if(results["song"]){             self.tableData = results["song"] as NSArray             self.tv.reloadData()             let firstDict:NSDictionary = self.tableData[0] as NSDictionary             let audioURL:String = firstDict["url"] as String             onSetAudio(audioURL)                          let imgUrl:String = firstDict["picture"] as String             onSetImage(imgUrl)                                                                          }else if(results["channels"]){             self.channelData = results["channels"] as NSArray                  }                            }          func onSetAudio(url:String){         timer?.invalidate()         playTim.text = "00:00"         self.audioPlayer.stop()         self.audioPlayer.contentURL = NSURL(string: url)         self.audioPlayer.play()                  timer = NSTimer.scheduledTimerWithTimeInterval(0.4,  target: self,  selector: "onUpdate", userInfo: nil, repeats: true)                  btnPlay.removeGestureRecognizer(tap)         iv.addGestureRecognizer(tap)         btnPlay.hidden = true              }          func onUpdate(){         println("==")         let currentTimer = audioPlayer.currentPlaybackTime         if currentTimer>0.0 {             let dur = audioPlayer.duration             let pecent:CFloat = CFloat(currentTimer/dur)             progressView.setProgress(pecent, animated: false)             let all:Int = Int(currentTimer)             let m:Int = all%60             let f:Int = Int(all/60)             var time:String = ""             //小时             if f<10{                 time = "0\(f):"             }else{                 time = "\(f):"             }             // 分钟             if m<10{                 time += "0\(m)"             }else{                 time += "\(m)"             }             playTim.text = time                                                }               }               func onSetImage(url:String){              let p_w_picpath = self.p_w_picpathCache[url] as? UIImage         if !p_w_picpath? {             let imgURL:NSURL = NSURL(string: url)             let request:NSURLRequest = NSURLRequest(URL: imgURL)             NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {                 (response, data, error) -> Void  in                 let img = UIImage(data: data)                 self.iv.p_w_picpath = img                 self.p_w_picpathCache[url] = img                                  })                      }else{              self.iv.p_w_picpath = p_w_picpath                     }              }     //选择其中之一     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){                  var rowData:NSDictionary = self.tableData[indexPath.row] as NSDictionary         let url = rowData["url"] as String         onSetAudio(url)         let urlImg = rowData["picture"] as String         onSetImage(urlImg)     }     //动画效果     func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!){               cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)               UIView.animateWithDuration(0.25,animations:{          cell.layer.transform = CATransform3DMakeScale(1,1,1)                      })          }           }


ChannelProtocol.swift

import UIKit import QuartzCore // 文启领航 bjflexedu.com // qq:376610000 protocol ChannelProtocol{     func onChangeChannel(channel_id:String) } class ChannelController: UIViewController,UITableViewDataSource,UITableViewDelegate {               @IBOutlet var tv : UITableView     var channelData:NSArray = NSArray()     var delegate:ChannelProtocol?               override func viewDidLoad() {         super.viewDidLoad()     }          override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()     }          func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{                  return channelData.count     }     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{         let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "channel")         let rowData:NSDictionary = self.channelData[indexPath.row] as NSDictionary         cell.text = rowData["name"] as String         cell.p_w_picpath = UIImage(named:"detail.jpg") //        cell.detailTextLabel.text = rowData["channel_id"] as String     return cell     }     //     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){                  var rowData:NSDictionary = self.channelData[indexPath.row] as NSDictionary         let channel_id:AnyObject = rowData["channel_id"] as AnyObject         let channel:String = "channel=\(channel_id)"                  self.delegate?.onChangeChannel(channel)                  self.dismissViewControllerAnimated(true, completion: nil)       }     //动画效果     func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!){                  cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)                  UIView.animateWithDuration(0.25,animations:{             cell.layer.transform = CATransform3DMakeScale(1,1,1)                          })              }           }

HttpProtocol.swift

import UIKit // 文启领航 bjflexedu.com // qq:376610000 protocol HttpProtocol{     func didRecieveResults(results:NSDictionary) } class HttpController:NSObject{          var delegate:HttpProtocol?          func onSearch(url:String){         var nsUrl:NSURL = NSURL(string: url)         var request:NSURLRequest = NSURLRequest(URL: nsUrl)         NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {              (response, data, error) -> Void in             var jsonResult:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary             self.delegate?.didRecieveResults(jsonResult)                                                                 })                   }                }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI