Swift實(shí)現(xiàn)plist文件讀取、音頻播放

舉一個(gè)之前我練習(xí)過的一個(gè)例子


題干

實(shí)現(xiàn)plist文件 首先得先創(chuàng)建plist文件


創(chuàng)建方法

然后根據(jù)我們給出的題干設(shè)計(jì)plist文件
plist文件

之后我們需要把這個(gè)plist傳到表格上码邻,我采用的是直接放在表格上數(shù)據(jù)折剃,如果有大神可以教教我怎么直接傳數(shù)據(jù)哦!O裎荨E吕纭!

下面是代碼

根據(jù)題干 一共跳轉(zhuǎn)界面跳轉(zhuǎn)了四次
所以我們可以創(chuàng)建四個(gè)ViewController


控制器

可以把viewcontroller當(dāng)成我們的主界面

// 存放數(shù)據(jù)的字典
    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!
    }
    
    // 點(diǎn)擊按鈕跳轉(zhuǎn)
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let sec = SecendViewController.init()
            // 將值傳到下一個(gè)界面
            sec.data = cells?["國內(nèi)"] as? NSDictionary
            self.navigationController?.pushViewController(sec, animated: true)
        }else{
            let sec = SecendViewController.init()
            // 將值傳到下一個(gè)界面
            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)
        // 表格的協(xié)議跟代理
        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)
    }

    

別忘了遵守表格協(xié)議哦

UITableViewDataSource,UITableViewDelegate

然后我們判斷跳轉(zhuǎn)后的下一個(gè)界面

// 存放數(shù)據(jù)的字典
    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?["國內(nèi)"] 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)
        // 表格的協(xié)議跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到視圖上
        self.view.addSubview(tableView!)
        // Do any additional setup after loading the view.
    }

也是添加個(gè)表格己莺,把數(shù)據(jù)放在我們的表格上

然后就是我們的下一級(jí)

// 存放數(shù)據(jù)的字典
    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)
        // 表格的協(xié)議跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到視圖上
        self.view.addSubview(tableView!)
    }

然后是我們最后一級(jí)

 // 存放數(shù)據(jù)的字典
    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 = ["因?yàn)閻鬯詯?,"只要為你活一天"];
        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)
        // 表格的協(xié)議跟代理
        tableView?.delegate = self
        tableView?.dataSource = self
        // 添加到視圖上
        self.view.addSubview(tableView!)
        

最后就是我們的最后一個(gè)界面實(shí)現(xiàn)的音頻播放
首先得先下載我們MP4歌曲下載 實(shí)現(xiàn)我們音頻的播放

導(dǎo)入 頭文件 import AVFoundation

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



        self.view.backgroundColor = .white
        // 設(shè)置音樂名稱的標(biāo)簽
        let lab = UILabel.init(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 30))
        lab.text = "因?yàn)閻鬯詯?
        lab.backgroundColor = .red
        lab.textColor = .black;
        lab.textAlignment = .center
        self.view.addSubview(lab)
        // 設(shè)置音樂
        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()
    }

這樣就完成了 是不是看著很簡單
動(dòng)手操作一下吧 實(shí)力很強(qiáng)的人可以試一試在表格上直接使用plist文件上請(qǐng)求下來的數(shù)據(jù)哦奏甫!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市凌受,隨后出現(xiàn)的幾起案子阵子,更是在濱河造成了極大的恐慌,老刑警劉巖胜蛉,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挠进,死亡現(xiàn)場(chǎng)離奇詭異色乾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)领突,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門暖璧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人攘须,你說我怎么就攤上這事∨固” “怎么了于宙?”我有些...
    開封第一講書人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長悍汛。 經(jīng)常有香客問我捞魁,道長,這世上最難降的妖魔是什么离咐? 我笑而不...
    開封第一講書人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任谱俭,我火速辦了婚禮,結(jié)果婚禮上宵蛀,老公的妹妹穿的比我還像新娘昆著。我一直安慰自己,他們只是感情好术陶,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開白布凑懂。 她就那樣靜靜地躺著,像睡著了一般梧宫。 火紅的嫁衣襯著肌膚如雪接谨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評(píng)論 1 312
  • 那天塘匣,我揣著相機(jī)與錄音脓豪,去河邊找鬼。 笑死忌卤,一個(gè)胖子當(dāng)著我的面吹牛扫夜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播驰徊,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼历谍,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了辣垒?” 一聲冷哼從身側(cè)響起望侈,我...
    開封第一講書人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎勋桶,沒想到半個(gè)月后脱衙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體侥猬,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年捐韩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了退唠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡荤胁,死狀恐怖瞧预,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情仅政,我是刑警寧澤垢油,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站圆丹,受9級(jí)特大地震影響滩愁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辫封,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一硝枉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧倦微,春花似錦妻味、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至劣欢,卻和暖如春棕诵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背凿将。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來泰國打工校套, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人牧抵。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓笛匙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親犀变。 傳聞我的和親對(duì)象是個(gè)殘疾皇子妹孙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 薩提亞提到的自我曼陀羅八個(gè)部分,上周六上完課后获枝,我決定來八篇文章分別寫寫我的每一部分蠢正,看看我有多了解她們,有多少已...
    JC賈閱讀 236評(píng)論 4 1
  • 花開花落省店, 靜待花開嚣崭。 又一次花開笨触, 又一次期待。 花是春天的賜賞雹舀, 是上天的寄語芦劣, 是孩子的期望。 花開了说榆, 它...
    遠(yuǎn)山近水_c11b閱讀 152評(píng)論 0 5
  • 個(gè)人成長修煉手冊(cè):輸入→輸出→復(fù)盤→系統(tǒng)→體系修煉原則:被動(dòng)→主動(dòng) 去年9月份之前虚吟,我還是一個(gè)庸庸碌碌的上班族。在...
    鄧男神Sweety閱讀 604評(píng)論 0 0