码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-06-07 23:23:08      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

播放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     //播放代理AVAudioPlayerDelegate
101     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

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4559313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!