播放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. ListView真的蛮好用

    老规矩,今晚学过的,明天,依靠回忆写出来. 打个卡,占个版面.

  2. bzoj 2190 [SDOI2008]仪仗队(欧拉函数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2190 [题意] n*n的正方形,在(0,0)格点可以看到的格子数目. [思路] 预处理 ...

  3. (转载)OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)

    前一篇说到了Foundation框架中的NSDirctionary类,这一一篇来看一下Foundation的其他常用的类:NSNumber,NSDate,NSException. 注:其实按照Java ...

  4. pip install 下载慢的问题

    建个文件 ~/.pip/pip.conf, 内容如下 [global] timeout = 6000 index-url = https://pypi.doubanio.com/simple [ins ...

  5. 第一百九十六天 how can I 坚持

    老妈邮的咸菜到了,美味啊,买不到,哈哈. 以后要勤给鱼换水啊,10天不换,水都臭了,拒绝懒惰. 明天要回济南了,刘松结婚,估计又没法发博客了. 两条鱼,一条罗娜,一条我,哈哈. 睡觉.

  6. 时隔3年,再次折腾BlackBerry 8830!

    2010年手头换得8830,之后就是好几番刷机.解SPC.倒腾各种软件..算软件注册码..那个时候记得最难弄的注册码就是crunchSMS.需要运行虚拟机来从内存地址读取注册码..不过黑莓真的很经得起 ...

  7. 用PyInstaller把Python代码打包成单个独立的exe可执行文件

    之前就想要把自己的BlogsToWordpress打开成exe了.一直没去弄. 又看到有人提到python打开成exe的问题. 所以打算现在就去试试. 注:此处之所有选用BlogsToWordpres ...

  8. Python基础-函数(function)

    这里我们看看Python中函数定义的语法,函数的局部变量,函数的参数,Python中函数的形参可以有默认值,参数的传递是赋值操作,在函数调用时,可以对实参进行打包和解包  1,函数定义 关键字def引 ...

  9. csu oj 1811: Tree Intersection (启发式合并)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 给你一棵树,每个节点有一个颜色.问删除一条边形成两棵子树,两棵子树有多少种颜色是有 ...

  10. DP练习(概率,树状,状压)

    http://vjudge.net/contest/view.action?cid=51211#overview 花了好长时间了,终于把这个专题做了绝大部分了 A:HDU 3853 最简单的概率DP求 ...