播放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. scrollTop

    scrollTop 表示滚动的高度,默认从position:0;开始向下滚,scrollTop(offset)的offset表示相对顶部的偏移,以像素计,<br/> scrollTop() ...

  2. TopFreeTheme精选免费模板【20130827】

    今天我们整理了一些关于WordPress, Joomla, Drupal, Magento, OpenCart的最新免费模板主题,绝大多数都是来自ThemeForest的响应式风格模板主题.题材多样化 ...

  3. 第三百四十五天 how can I 坚持

    最烦这个阶段了,飘忽不定,或许这种感觉未来会很值得回味. 我为什么会那么烦,是因为错过而悔恨,还是..其实我还是很在乎的,好想一切都随缘. 让我讲struts.springmvc,可是什么都不会. 我 ...

  4. 用c++库函数轻松解决回文问题

    在<Accelerated C++>6.1学到的,这个解决回文问题的方案很简单,有必要把它记录下来. begin返回一个迭代器,它会从容器的最后一个元素开始,并且从后向前地逆序访问容器.e ...

  5. 网易及新浪IP查询接口

    通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ...

  6. http://acm.hdu.edu.cn/showproblem.php?pid=2579

    #include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1 ...

  7. POJ2947Widget Factory(高斯消元解同模方程)

    http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...

  8. HDU1002大数加法

    大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...

  9. C# Control 控件DrapDrop不触发的问题

    今天在做一个鼠标拖拽功能时,需要用到PictureBox的拖拽,即拖拽一个图标到PictureBox上实现加载绘制,可是怎么整也触发不了DrapDrop事件,最后终于找到了解决方法:原来需要在Drog ...

  10. String.valueOf(null) 报空指针

    String.valueOf 默认的方法 argument 可以为null 的 boolean b = null; char c = null; char[] data = null; double ...