播放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------*/
 

ios开发——实用技术篇Swift篇&播放MP3的更多相关文章

  1. ios开发——实用技术篇Swift篇&视频

    视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...

  2. ios开发——实用技术篇Swift篇&录音

    录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...

  3. ios开发——实用技术篇Swift篇&系统声音

    系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...

  4. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  5. ios开发——实用技术篇Swift篇&拍照

    拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...

  6. ios开发——实用技术篇Swift篇&照片选择

    照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...

  7. ios开发——实用技术篇Swift篇&加速计和陀螺仪

    加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...

  8. ios开发——实用技术篇Swift篇&多点触摸与手势识别

    多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...

  9. ios开发——实用技术篇OC篇&iOS的主要框架

    iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...

随机推荐

  1. 转 AI教程 logo

    版权申明:本文原创作者飞屋工作室,感谢飞屋工作室的原创分享! 这篇AI制作标志教程是一个非常实用的教程.通过这个教程飞特的朋友们将会学习到AI制作标志的流程和标志的创作思路.非常实用.推荐过来和飞特的 ...

  2. android读取data下得数据

    拥有Root权限的情况 adb shell su cd  /data/data/com.package 然后就可以直接读取 没有Root的情况 adb shell run-as com.package ...

  3. mysql innobackupex xtrabackup 大数据量 备份 还原(转)

    原文:http://blog.51yip.com/mysql/1650.html 作者:海底苍鹰 大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推 ...

  4. Altium Designer 6三维元件库建模教程

    一.AD6.9 PCB编辑环境下如何使用STEP模型的方法. 在PCB 封装库中添加对应的3D模型,然后选择update pcb 即可 方法:在PCB library 页面,点击place -- 3d ...

  5. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  6. Data Binding in WPF

    http://msdn.microsoft.com/en-us/magazine/cc163299.aspx#S1   Data Binding in WPF John Papa Code downl ...

  7. Apache Kafka:下一代分布式消息系统

    [http://www.infoq.com/cn/articles/apache-kafka/]分布式发布-订阅消息系统. Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和可复制的提交日 ...

  8. Jupyter增加内核

    本例的Jupyter安装在Python3下,以增加Python2内核为例. 首先确认在Python3下已安装了内核: ipython kernel install --user #or python3 ...

  9. 本地存储(cookie&sessionStorage&localStorage)

    好文章,最全面.就查它吧:https://segmentfault.com/a/1190000004556040 1.DOM存储:https://developer.mozilla.org/zh-CN ...

  10. 【转】 解决IllegalStateException: Can not perform this action after onSaveInstanceState

    今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState ...