ios开发——实用技术篇Swift篇&播放MP3
播放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的更多相关文章
- ios开发——实用技术篇Swift篇&视频
视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...
- ios开发——实用技术篇Swift篇&录音
录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...
- ios开发——实用技术篇Swift篇&系统声音
系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...
- ios开发——实用技术篇Swift篇&拍照
拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...
- ios开发——实用技术篇Swift篇&照片选择
照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...
- ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...
- ios开发——实用技术篇Swift篇&多点触摸与手势识别
多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...
- ios开发——实用技术篇OC篇&iOS的主要框架
iOS的主要框架 阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...
随机推荐
- Epic - Coin Change
Something cost $10.25 and the customer pays with a $20 bill, the program will print out the most eff ...
- 两款较好的Web前端性能测试工具
前段时间接手了一个 web 前端性能优化的任务,一时间不知道从什么地方入手,查了不少资料,发现其实还是蛮简单的,简单来说说. 一.前端性能测试是什么 前端性能测试对象主要包括: HTML.CSS.JS ...
- Delphi 调用外部程序并等待其运行结束
转自:http://blog.csdn.net/xieyunc/article/details/4140620 如何让Delphi调用外部程序并等待其运行结束 1. uses Window ...
- AutoCompleteTextView使用 监听
AutoCompleteTextView使用 An editable text view that shows completion suggestions automatically while t ...
- perf
- 删除Ngnix 日志
删除Ngnix日志的脚本 #!/bin/bash #初始化 LOGS_PATH=$(pwd)/logs YESTERDAY=$(date -d "yesterday" +%Y-%m ...
- MSSQL 2005数据库可疑状态
今天早上打开进销存,提示链接失败,经过检查参数,网络.端口等各种情况,均没有发现问题,最后检查数据库本事的问题. 通过studio进去发现我的进销存数据变成了(可疑)状态,随机百度修复方法,修复方法还 ...
- nginx配置 首页不显示 index.html首页是显示域名
原状况如下: 访问:www.test.com 敲回车后浏览器中自动跳转致: www.test.com/index.html 公司新需求如下: 访问:www.test.com 敲回车后浏览器中url不变 ...
- AJAX小练习,防止以后忘记
<div id="content"> <input id="btnShow" type="button" value=&q ...
- android 自定义用相机拍照后的照片存储位置
1.imageUri = Uri.fromFile(new File(Environment .getExternalStorageDirectory()+ File.separator + getP ...