中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

swift 豆瓣音樂開發案例

發布時間:2020-07-24 14:07:03 來源:網絡 閱讀:317 作者:文啟領航 欄目:移動開發

搞了一天,總有把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

伊春市| 诸城市| 中西区| 嘉义县| 靖宇县| 盘山县| 南和县| 富顺县| 沈阳市| 中西区| 诏安县| 大渡口区| 吉林省| 竹溪县| 砚山县| 临西县| 长丰县| 张家界市| 奉节县| 垫江县| 剑阁县| 留坝县| 三江| 聂荣县| 益阳市| 江门市| 夏邑县| 三门县| 西宁市| 远安县| 和顺县| 莎车县| 揭西县| 武强县| 乐昌市| 大化| 湘潭市| 图木舒克市| 临清市| 石棉县| 论坛|