Swift实现plist文件读取、音频播放

举一个之前我练习过的一个例子


题干

实现plist文件 首先得先创建plist文件


创建方法

然后根据我们给出的题干设计plist文件
plist文件

之后我们需要把这个plist传到表格上,我采用的是直接放在表格上数据,如果有大神可以教教我怎么直接传数据哦!!!!

下面是代码

根据题干 一共跳转界面跳转了四次
所以我们可以创建四个ViewController


控制器

可以把viewcontroller当成我们的主界面

// 存放数据的字典
    var cells : NSDictionary?
    // 表格
    var tableView:UITableView?
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (cells?.allKeys.count)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let str = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: str)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
        }
        cell?.textLabel?.text = cells?.allKeys[indexPath.row] as? String;
        return cell!
    }
    
    // 点击按钮跳转
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let sec = SecendViewController.init()
            // 将值传到下一个界面
            sec.data = cells?["国内"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        }else{
            let sec = SecendViewController.init()
            // 将值传到下一个界面
            sec.data = cells?["国外"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化表格
        tableView = UITableView.init(frame: self.view.frame, style: .plain)
        // 表格的协议跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到视图上
        self.view.addSubview(tableView!)
        
        // 获得plist文件到地址
        let path = Bundle.main.bundlePath
        let plistName:NSString = "Property List.plist"
        let finalPath:NSString = (path as NSString).appendingPathComponent(plistName as String) as NSString
        cells = NSDictionary(contentsOfFile:finalPath as String)
    }

    

别忘了遵守表格协议哦

UITableViewDataSource,UITableViewDelegate

然后我们判断跳转后的下一个界面

// 存放数据的字典
    var data : NSDictionary?
    // 表格
    var tableView:UITableView?
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (data?.allKeys.count)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let str = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: str)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
        }
        cell?.textLabel?.text = data?.allKeys[indexPath.row] as? String;
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let sec = ThirdViewController.init()
            sec.data = data?["国内"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        }else{
            let sec = ThirdViewController.init()
            sec.data = data?["国外"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        }

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化表格
        tableView = UITableView.init(frame: self.view.frame, style: .plain)
        // 表格的协议跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到视图上
        self.view.addSubview(tableView!)
        // Do any additional setup after loading the view.
    }

也是添加个表格,把数据放在我们的表格上

然后就是我们的下一级

// 存放数据的字典
    var data : NSDictionary?
    // 表格
    var tableView:UITableView?
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let str = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: str)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
        }
        let data = ["谢霆锋","周杰伦","刘德华"];
        cell?.textLabel?.text = data[indexPath.row];
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let sec = FoutViewController.init()
//            sec.data = data?["国外"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化表格
        tableView = UITableView.init(frame: self.view.frame, style: .plain)
        // 表格的协议跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到视图上
        self.view.addSubview(tableView!)
    }

然后是我们最后一级

 // 存放数据的字典
    var data : NSDictionary?
    // 表格
    var tableView:UITableView?
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let str = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: str)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
        }
        let data = ["因为爱所以爱","只要为你活一天"];
        cell?.textLabel?.text = data[indexPath.row];
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let sec = MusicViewController.init()
        //            sec.data = data?["国外"] as? NSDictionary
        self.navigationController?.pushViewController(sec, animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化表格
        tableView = UITableView.init(frame: self.view.frame, style: .plain)
        // 表格的协议跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到视图上
        self.view.addSubview(tableView!)
        

最后就是我们的最后一个界面实现的音频播放
首先得先下载我们MP4歌曲下载 实现我们音频的播放

导入 头文件 import AVFoundation

然后在viewDidLoad外面初始化一下音频
var audioPlayer: AVAudioPlayer? 



        self.view.backgroundColor = .white
        // 设置音乐名称的标签
        let lab = UILabel.init(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 30))
        lab.text = "因为爱所以爱"
        lab.backgroundColor = .red
        lab.textColor = .black;
        lab.textAlignment = .center
        self.view.addSubview(lab)
        // 设置音乐
        let path = Bundle.main.path(forResource: "%E5%BC%A0%E7%B4%AB%E8%B1%AA+-+%E5%8F%AF%E4%B8%8D%E5%8F%AF%E4%BB%A5", ofType: "flac")
        let pathURL=NSURL(fileURLWithPath: path!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
        } catch {
            audioPlayer = nil
        }
        
        audioPlayer?.prepareToPlay()
        // 播放按钮
        let playbtn = UIButton(frame: CGRect(x: 60, y: 200, width: 100, height: 40))
        playbtn.backgroundColor = UIColor.cyan
        playbtn.setTitle("play", for: .normal)
        playbtn.setTitleColor(UIColor.white, for: .normal)
        
        // 暂停按钮
        let pausebtn = UIButton(frame: CGRect(x: 180, y: 200, width: 100, height: 40))
        pausebtn.setTitle("pause", for: .normal)
        pausebtn.setTitleColor(UIColor.white, for: .normal)
        pausebtn.backgroundColor = UIColor.cyan
        
        // 添加到视图上
        self.view.addSubview(playbtn)
        self.view.addSubview(pausebtn)
        
        // 按钮方法
        playbtn.addTarget(self, action: #selector(play), for: .touchUpInside)
        pausebtn.addTarget(self, action: #selector(pause), for: .touchUpInside)
        
        
        
        self.view.addSubview(playbtn)
        self.view.addSubview(pausebtn)
        
        // Do any additional setup after loading the view.
    }
    @objc func play(){
        audioPlayer?.play()
    }

    @objc func pause(){
        audioPlayer?.pause()
    }

这样就完成了 是不是看着很简单
动手操作一下吧 实力很强的人可以试一试在表格上直接使用plist文件上请求下来的数据哦!

你可能感兴趣的:(Swift实现plist文件读取、音频播放)