一、系统声音

ios应用中的提醒声音、游戏背景音乐等。可以播放的格式有CAF、AIF、WAV。

系统声音服务提供了一个API,但是没有操作声音和控制音量的功能,因此如果为多媒体或者游戏创建专门的声音,就不要使用系统声音服务。

其中支持三种类型:声音、提醒、震动。

1、首先导入AudioToolbox.framework框架。

2、系统声音服务不是通过类实现的,而是通过传统的C语言函数调用来触发播放操作。要播放音频需要使用两个函数AudioServicesCreateSystemSoundID和AudioServicePlaySystemSound。

  1. // MARK: - 系统声音
  2. /*----- 系统声音 ------*/
  3. @IBAction func systemSound()
  4. {
  5. //建立的SystemSoundID对象
  6. var soundID: SystemSoundID = 0
  7. //获取声音文件地址
  8. let path = NSBundle.mainBundle().pathForResource("SaoMa", ofType: "wav")
  9. //地址转换
  10. let baseURL = NSURL(fileURLWithPath: path!)
  11. //赋值
  12. AudioServicesCreateSystemSoundID(baseURL , &soundID)
  13. //使用AudioServicesPlaySystemSound播放
  14. AudioServicesPlaySystemSound(soundID)
  15. }
  1. /*----- 系统提醒 ------*/
  2. @IBAction func systemAlert()
  3. {
  4. //建立的SystemSoundID对象
  5. var soundID: SystemSoundID = 0
  6. //获取声音文件地址
  7. let path = NSBundle.mainBundle().pathForResource("SaoMa", ofType: "wav")
  8. //地址转换
  9. let baseURL = NSURL(fileURLWithPath: path!)
  10. //赋值
  11. AudioServicesCreateSystemSoundID(baseURL , &soundID)
  12. //使用AudioServicesPlayAlertSound播放
  13. AudioServicesPlayAlertSound(soundID)
  14. }
  1. /*----- 系统震动 ------*/
  2. @IBAction func systemVibration()
  3. {
  4. //建立的SystemSoundID对象
  5. let soundID = SystemSoundID(kSystemSoundID_Vibrate)
  6. //使用AudioServicesPlaySystemSound播放
  7. AudioServicesPlaySystemSound(soundID)
  8. }

二、AV音频播放器

1、导入AVFoundation.framework。

2、声明一个AVAudioPlayer对象。

  1. @IBOutlet var timeLabel:UILabel!//播放的时间/总时间
  2. @IBOutlet var jinDuSlider:UISlider!//进度条
  3. var _timer:NSTimer!//定时器线程, 刷新进度条
  1. let mp3Path = NSBundle.mainBundle().pathForResource("xiaoPingGuo", ofType: "mp3")
  2. let fileUrl = NSURL.fileURLWithPath(mp3Path!)
  3. do
  4. {
  5. audioPlayer = try AVAudioPlayer(contentsOfURL: fileUrl)
  6. }
  7. catch let error as NSError {
  8. print("初始化播放器失败:\(error)")//如果失败,error 会返回错误信息
  9. }
  10. audioPlayer.prepareToPlay()
  1. //播放按钮事件
  2. @IBAction func audioPlayButton()
  3. {
  4. if audioPlayer.playing
  5. {
  6. return;//如果已在播放,跳出
  7. }
  8. //开始播放音频文件
  9. audioPlayer.play()
  10. //设置进图条最小是=0
  11. jinDuSlider.minimumValue = 0.0;
  12. //设置进度条最大值等于声音的描述
  13. jinDuSlider.maximumValue = Float(audioPlayer.duration)
  14. //启动定时器 定时更新进度条和时间label 在updateTime方法中实现
  15. _timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "updateTime", userInfo: nil, repeats: true)
  16. }
  1. //定时器-
  2. func updateTime()
  3. {
  4. //获取音频播放器播放的进度,单位秒
  5. let cuTime:Float = Float(audioPlayer.currentTime)
  6. //更新进度条
  7. jinDuSlider.value = cuTime
  8. //获取总时间
  9. let duTime:Float = Float(audioPlayer.duration)
  10. //播放时间秒数,换算成:时、分、秒
  11. let hour1:Int = Int(cuTime/(60*60))
  12. let minute1:Int = Int(cuTime/60)
  13. let second1:Int = Int(cuTime%60)
  14. //总时间秒数,换算成:时、分、秒
  15. let hour2:Int = Int(duTime/(60*60))
  16. let minute2:Int = Int(duTime/60)
  17. let second2:Int = Int(duTime%60)
  18. //label显示
  19. timeLabel.text = NSString(format: "%.2d:%.2d:%.2d / %.2d:%.2d:%.2d",hour1,minute1,second1,hour2,minute2,second2) as String
  20. }

  1. //
    播放代理 AVAudioPlayerDelegate
  2. func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool)
  3. {
  4. //成功播放完毕结束
  5. }
  6. func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer, error: NSError?)
  7. {
  8. //音频播放器的解码错误
  9. }
  10. //@availability(iOS, introduced=2.2, deprecated=8.0)
  11. func audioPlayerBeginInterruption(player: AVAudioPlayer)
  12. {
  13. //音频播放器开始中断
  14. }
  15. //@availability(iOS, introduced=6.0, deprecated=8.0)
  16. func audioPlayerEndInterruption(player: AVAudioPlayer, withOptions flags: Int)
  17. {
  18. //音频播放结束中断
  19. }
  1. //暂停
  2. @IBAction func audioPauseButton(sender:UIButton)
  3. {
  4. let title = sender.titleForState(UIControlState.Normal)
  5. if title == "Pause" && audioPlayer.playing
  6. {
  7. audioPlayer.pause()
  8. sender.setTitle("Continue", forState: UIControlState.Normal)
  9. }
  10. else if title == "Continue"
  11. {
  12. sender.setTitle("Pause", forState: UIControlState.Normal)
  13. audioPlayer.play()
  14. }
  15. }
  16. //停止
  17. @IBAction func audioStopButton(sender:UIButton)
  18. {
  19. if(audioPlayer.playing)
  20. {
  21. audioPlayer.stop()
  22. audioPlayer.currentTime=0;
  23. timeLabel.text = "";
  24. }
  25. }
  26. //调 进度
  27. @IBAction func jinDuChange(sender:UISlider)
  28. {
  29. //获取jinDuSlider的值来设置音频播放器进度
  30. print("当前进度:\(jinDuSlider.value)")
  31. audioPlayer.currentTime = NSTimeInterval(jinDuSlider.value)
  32. //播放器播放
  33. audioPlayer.play()
  34. }
  35. //控制声音
  36. @IBAction func audioSoundChange(sender:UISlider)
  37. {
  38. //获取UISlider对象的值,并设置audioPlayer.volume
  39. audioPlayer.volume = sender.value
  40. aLabel.text = "\(sender.value)"
  41. }

 

版权声明:本文为fengmin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/fengmin/p/5714028.html