ios开发——实用技术篇Swift篇&播放MP3

播放MP3

     // MARK: - 播放MP3
     /*----- mp3 ------*/
     //定时器-
     func updateTime()
     {
         //获取音频播放器播放的进度,单位秒
         var cuTime:Float = Float(audioPlayer.currentTime)

         //更新进度条
         jinDuSlider.value = cuTime

         //获取总时间
         var duTime:Float = Float(audioPlayer.duration)

         //播放时间秒数,换算成:时、分、秒
         var hour1:Int = Int(cuTime/(*))
         var minute1:Int = Int(cuTime/)
         var second1:Int = Int(cuTime%)

         //总时间秒数,换算成:时、分、秒
         var hour2:Int = Int(duTime/(*))
         var minute2:Int = Int(duTime/)
         var second2:Int = Int(duTime%)

         //label显示
 //        timeLabel.text = NSString(format: "%.2d:%.2d:%.2d / %.2d:%.2d:%.2d",hour1,minute1,second1,hour2,minute2,second2)

         //2015年5月2后修改
         timeLabel.text = NSString(format: "%.2d:%.2d:%.2d / %.2d:%.2d:%.2d",hour1,minute1,second1,hour2,minute2,second2) as String
     }

     //播放按钮事件
     @IBAction func audioPlayButton()
     {
         if audioPlayer.playing
         {
             return;//如果已在播放,跳出
         }

         //开始播放音频文件
         audioPlayer.play()

         //设置进图条最小是=0
         jinDuSlider.minimumValue = 0.0;

         //设置进度条最大值等于声音的描述
         jinDuSlider.maximumValue = Float(audioPlayer.duration)

         //启动定时器 定时更新进度条和时间label 在updateTime方法中实现
         _timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "updateTime", userInfo: nil, repeats: true)
     }

     //暂停
     @IBAction func audioPauseButton(sender:UIButton)
     {
         var title = sender.titleForState(UIControlState.Normal)
         if  title == "Pause" && audioPlayer.playing
         {
             audioPlayer.pause()
             sender.setTitle("Continue", forState: UIControlState.Normal)
         }
         else if title == "Continue"
         {
             sender.setTitle("Pause", forState: UIControlState.Normal)
             audioPlayer.play()
         }
     }

     //停止
     @IBAction func audioStopButton(sender:UIButton)
     {
         if(audioPlayer.playing)
         {
             audioPlayer.stop()
             audioPlayer.currentTime=;
             timeLabel.text = "";
         }
     }

     //调 进度
     @IBAction func jinDuChange(sender:UISlider)
     {
         //获取jinDuSlider的值来设置音频播放器进度
         audioPlayer.currentTime = NSTimeInterval(jinDuSlider.value)

         //播放器播放
         audioPlayer.play()
     }

     //控制声音
     @IBAction func audioSoundChange(sender:UISlider)
     {
         //获取UISlider对象的值,并设置audioPlayer.volume
         audioPlayer.volume = sender.value

         aLabel.text = "\(sender.value)"
     }

     //播放代理AVAudioPlayerDelegate
     func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool)
     {
         //成功播放完毕结束
     }

     func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!)
     {
         //音频播放器的解码错误
     }

     //@availability(iOS, introduced=2.2, deprecated=8.0)
     func audioPlayerBeginInterruption(player: AVAudioPlayer!)
     {
         //音频播放器开始中断
     }

     //@availability(iOS, introduced=6.0, deprecated=8.0)
     func audioPlayerEndInterruption(player: AVAudioPlayer!, withOptions flags: Int)
     {
         //音频播放结束中断
     }

     /*-----mp3 end------*/
 
上一篇:新手学习Python时常见的错误


下一篇:第三百五十六天 how can I 坚持