AVFoundation(一)---AVAudioPlayer
AVAudioPlayer相当于一个播放器,它支持多种音频格式,而且能够进行进度、音量、播放速度等控制。
下边通过代码来看一下,它的属性和常用方法(具体说明都写在了注释中):
//AVAudioPlayer使用比较简单
/*
1.初始化AVAudioPlayer对象
这里有两种init方法可以初始化AVAudioPlayer对象
1)- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError
使用文件URL初始化播放器,注意这个URL不能是HTTP URL,AVAudioPlayer不支持加载网络媒体流,只能播放本地文件
2)- (instancetype)initWithData:(NSData *)data error:(NSError **)outError
使用NSData初始化播放器,注意使用此方法时必须文件格式和文件后缀一致,否则出错,所以相比此方法更推荐使用上述方法或- (instancetype)initWithData:(NSData *)data fileTypeHint:(NSString *)utiString error:(NSError **)outError方法进行初始化
*/
NSString *path = [[NSBundle mainBundle] pathForResource:@"" ofType:@"mp3"];
NSError *error;
AVAudioPlayer *audioplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:path] error:&error]; /**
AVAudioPlayer对象的常见属性
*/
//.playing 是否正在播放,readonly
BOOL isPlaying = audioplayer.playing;
//.numberOfChannels 音频声道数,readonly
BOOL numberofChannels = audioplayer.numberOfChannels;
//.duration 音频时长,readonly
NSTimeInterval MusicDuration = audioplayer.duration;
//.url 音频文件的路径 readonly
NSURL *MusicUrl = audioplayer.url;
//.data 音频数据 readonly
NSData *MusicData = audioplayer.data;
//.pan 立体声平衡 如果为-1.0则完全左声道,如果0.0则左右声道平衡,如果为1.0则完全为右声道
audioplayer.pan = 0.0;
//.volume 音量大小,范围0-1.0
audioplayer.volume = 0.5;
//.enableRate 是否允许改变播放速率
//播放速率,范围0.5-2.0,如果为1.0则正常播放,如果要修改播放速率则必须设置enableRate为YES
audioplayer.enableRate = YES;
audioplayer.rate = 1.0;
//.currentTime 当前播放时长
NSTimeInterval MusicCurrentTime = audioplayer.currentTime;
//.deviceCurrentTime 输出设备播放音频的时间
NSTimeInterval deviceCurrentTime = audioplayer.deviceCurrentTime;
//.numberOfLoops 循环播放次数,如果为0则不循环,如果小于0则无限循环,大于0则表示循环次数
audioplayer.numberOfLoops = ;
//音频播放设置信息,只读
NSDictionary *settingDic = audioplayer.settings; /**
* 对象方法
*/
//- (BOOL)prepareToPlay; 加载音频文件到缓冲区,注意即使在播放之前音频文件没有加载到缓冲区程序也会隐式调用此方法。
[audioplayer prepareToPlay];
//- (BOOL)play; 播放音频文件
[audioplayer play];
//- (BOOL)playAtTime:(NSTimeInterval)time 在指定的时间开始播放音频
[audioplayer playAtTime:];
//- (void)pause; 暂停播放
[audioplayer pause];
//- (void)stop; 停止播放
[audioplayer stop];
//@property(nonatomic, copy) NSArray *channelAssignments 获得或设置播放声道
NSArray *channelArr = audioplayer.channelAssignments; /**
代理方法 * - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
//音频播放完成 - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
//音频解码发生错误
*/
上边代码就是AVAudioPlayer的简单介绍,接下来,直接上代码,看一下一个简单的音乐播放的实现,可播放、暂停。
@interface ViewController ()<AVAudioPlayerDelegate>
- (IBAction)clickplay:(id)sender;
- (IBAction)clickpause:(id)sender;
@property (weak, nonatomic) IBOutlet UIProgressView *progress; @property (strong,nonatomic) AVAudioPlayer *audioPlayer;//播放器 @property (weak,nonatomic) NSTimer *timer;//进度更新定时器 @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} /**
* 设置定时器,每0.5秒,更新一次播放进度条
*
* @return timer
*/
-(NSTimer *)timer{
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
}
return _timer;
} -(void)updateProgress{
NSLog(@"updateProgress");
float progress = self.audioPlayer.currentTime / self.audioPlayer.duration;
self.progress.progress = progress;
} /**
* 创建播放器
*
* @return 音频播放器
*/
-(AVAudioPlayer *)audioPlayer{
if (!_audioPlayer) {
NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"" ofType:@"mp3"];
NSURL *url = [NSURL URLWithString:urlStr];
NSError *error = nil;
//初始化播放器,这里的url只支持文件路径,不支持HTTP url
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
//设置播放器属性
_audioPlayer.numberOfLoops = ;//设置为0表示不循环
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
if (error) {
NSLog(@"初始化播放器过程发生错误,错误信息:%@",error);
return nil;
}
}
return _audioPlayer;
} /**
* 播放音频
*/
-(void)play{
if (!self.audioPlayer.isPlaying) {
[self.audioPlayer play];
self.timer.fireDate = [NSDate distantPast];
}
} /**
* 暂停播放
*/
-(void)pause{
if (self.audioPlayer.isPlaying) {
[self.audioPlayer pause];
self.timer.fireDate = [NSDate distantFuture];
}
} //play
- (IBAction)clickplay:(id)sender {
[self play];
}
//pause
- (IBAction)clickpause:(id)sender {
[self pause];
} #pragma mark - delegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"播放完毕");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.timer.fireDate = [NSDate distantFuture];
});
} -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
NSLog(@"解码失败");
}
AVFoundation(一)---AVAudioPlayer的更多相关文章
- IOS开发中AVFoundation中AVAudioPlayer的使用
IOS开发中如何调用音频播放组件 1.与音频相关的头文件等都在AVFoundation.h中,所以第一步是添加音频库文件: #import <AVFoundation/AVFoundation. ...
- AVAudioSesion和AVAudioPlayer的基本使用
iOS基础篇-AVPLayer和AVAudioSession 2018.02.27 16:17 字数 215 阅读 1516评论 0喜欢 4 作用 AVPLayer:可以用来播放在线及本地音视频 AV ...
- 《转》iOS音频视频初级开发
代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS ...
- AVAudioPlayer
AVAudioPlayer在AVFoundation框架下,所以我们要导入AVFoundation.framework. AVAudioPlayer类封装了播放单个声音的能力.播放器可以用NSURL或 ...
- iOS播放器 - AVAudioPlayer
今天记录一下AVAudioPlayer,这个播放器类苹果提供了一些代理方法,主要用来播放本地音频. 其实也可以用来播放网络音频,只不过是将整个网络文件下载下来而已,在实际开发中会比较耗费流量不做推荐. ...
- AVAudioPlayer播放本地音频
AVAudioPlayer苹果官方上说一般用于播放本地音频,不能用于播放网络上的音频. 具体的代码:先导入 #import <AVFoundation/AVFoundation.h> // ...
- AVAudioPlayer播放并实现了后台播放和远程控制
// ViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @class ...
- iOS - AVAudioPlayer 音频播放
前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...
- 音乐播放器 AVAudioPlayer、定时器、UISlider
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ...
随机推荐
- JQ中的clone()方法与DOM中的cloneNode()方法
JQ中的clone()方法与DOM中的cloneNode()方法 cloneNode()定义和用法 cloneNode()方法创建节点的拷贝,并返回该副本. 语法: node.cloneNode(de ...
- Oberon程序设计—目录
内 容前 言1, 什么是Oberon? 1.1 ALGOL家族 1.2 该系统2, 第一:程序 2.1 一个符号来描述的语法: 2.2练习 第一部分,符号和基本类型,分配,控制结构, ...
- digitalocean网站打不开,大陆无法正常访问怎么办?
在中国大陆,由于某些恶心的原因,digitalocean官方网站经常出现无法打开,或者打开后网页异常的情况,如果你是一个新注册用户,你甚至会被吓到,一个全球知名的vps主机商网站可能是这样的: 我的天 ...
- 【简单学习shell】iptables命令实用
构造设备离线iptables命令iptables -I INPUT -p all -s 10.71.115.159 -j DROP 断链iptables -I INPUT -p all -s 10.7 ...
- linux重新部署mysql和tomcat时乱码问题
mysql解决方法 vim /etc/my.cnf [client]default-character-set=utf8 [mysqld]default-storage-engine=INNODBch ...
- APP测试--功能测试
1.1 了解需求 这一点,不但是功能测试,是所有测试都需要的第1步.通过需求文档,与产品经理的沟通,与开发的沟通,用户的使用习惯等各方法,了解APP的需求. 1.2 编写测试用例 当然之前可能是测试计 ...
- stm32按键识别
刚写了一个关于stm32单片机的按键识别的程序.目的,同时识别多个按键,并且不浪费cpu的时间. 关于去抖动,以前以为是在按键的时候,手会抖动.通过程序验证,这个确实是误解.这个应该是防止意外干扰.以 ...
- SELinux Policy Macros
参考:http://selinuxproject.org/page/NB_RefPolicy Directory Macros macro expansion getattr_dir_perms ge ...
- windows下如何快速搭建web.py开发框架
在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方 ...
- [转载]Linux 环境下编译 0.11版本内核 kernel
最近在看<.如果Clobber/Modify 为空,则其前面的冒号(:)必须省略. 2.如果Output,Input,Clobber/Modify都为空,Output,Input之前的冒号(:) ...