ios開發——實用技術篇Swift篇&播放MP3

來源:互聯網
上載者:User

標籤:

播放MP3

 

  1     // MARK: - 播放MP3  2     /*----- mp3 ------*/  3     //定時器-  4     func updateTime()  5     {  6         //擷取音頻播放器播放的進度,單位秒  7         var cuTime:Float = Float(audioPlayer.currentTime)  8           9         //更新進度條 10         jinDuSlider.value = cuTime 11          12         //擷取總時間 13         var duTime:Float = Float(audioPlayer.duration) 14          15         //播放時間秒數,換算成:時、分、秒 16         var hour1:Int = Int(cuTime/(60*60)) 17         var minute1:Int = Int(cuTime/60) 18         var second1:Int = Int(cuTime%60) 19          20         //總時間秒數,換算成:時、分、秒 21         var hour2:Int = Int(duTime/(60*60)) 22         var minute2:Int = Int(duTime/60) 23         var second2:Int = Int(duTime%60) 24          25          26         //label顯示 27 //        timeLabel.text = NSString(format: "%.2d:%.2d:%.2d / %.2d:%.2d:%.2d",hour1,minute1,second1,hour2,minute2,second2) 28          29         //2015年5月2後修改 30         timeLabel.text = NSString(format: "%.2d:%.2d:%.2d / %.2d:%.2d:%.2d",hour1,minute1,second1,hour2,minute2,second2) as String 31     } 32    33     //播放按鈕事件 34     @IBAction func audioPlayButton() 35     { 36         if audioPlayer.playing 37         { 38             return;//如果已在播放,跳出 39         } 40          41         //開始播放音頻檔案 42         audioPlayer.play() 43          44         //設定進圖條最小是=0 45         jinDuSlider.minimumValue = 0.0; 46          47         //設定進度條最大值等於聲音的描述 48         jinDuSlider.maximumValue = Float(audioPlayer.duration) 49          50         //啟動定時器 定時更新進度條和時間label 在updateTime方法中實現 51         _timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "updateTime", userInfo: nil, repeats: true) 52     } 53      54     //暫停 55     @IBAction func audioPauseButton(sender:UIButton) 56     { 57         var title = sender.titleForState(UIControlState.Normal) 58         if  title == "Pause" && audioPlayer.playing 59         { 60             audioPlayer.pause() 61             sender.setTitle("Continue", forState: UIControlState.Normal) 62         } 63         else if title == "Continue" 64         { 65             sender.setTitle("Pause", forState: UIControlState.Normal) 66             audioPlayer.play() 67         } 68     } 69      70     //停止 71     @IBAction func audioStopButton(sender:UIButton) 72     { 73         if(audioPlayer.playing) 74         { 75             audioPlayer.stop() 76             audioPlayer.currentTime=0; 77             timeLabel.text = ""; 78         } 79     } 80      81     //調 進度 82     @IBAction func jinDuChange(sender:UISlider) 83     { 84         //擷取jinDuSlider的值來設定音頻播放器進度 85         audioPlayer.currentTime = NSTimeInterval(jinDuSlider.value) 86          87         //播放器播放 88         audioPlayer.play() 89     } 90  91     //控制聲音 92     @IBAction func audioSoundChange(sender:UISlider) 93     { 94         //擷取UISlider對象的值,並設定audioPlayer.volume 95         audioPlayer.volume = sender.value 96          97         aLabel.text = "\(sender.value)" 98     } 99     100     //播放代理AVAudioPlayerDelegate101     func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool)102     {103         //成功播放完畢結束104     }105     106     func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!)107     {108         //音頻播放器的解碼錯誤109     }110     111     //@availability(iOS, introduced=2.2, deprecated=8.0)112     func audioPlayerBeginInterruption(player: AVAudioPlayer!)113     {114         //音頻播放器開始中斷115     }116     117     118     //@availability(iOS, introduced=6.0, deprecated=8.0)119     func audioPlayerEndInterruption(player: AVAudioPlayer!, withOptions flags: Int)120     {121         //音頻播放結束中斷122     }123     124     125     /*-----mp3 end------*/126     127     

 

 

ios開發——實用技術篇Swift篇&播放MP3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.