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 ...
随机推荐
- ssh: scp命令
scp 复制命令 Eg. -r /tmp/q1 root@[::]/home 1.命令格式: scp [参数] [原路径] [目标路径] 2.命令功能: scp是 secure copy的缩写, sc ...
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...
- JavaEE 配置文件 应用首选项存储
JavaEE 配置文件 应用首选项存储 @author ixenos 什么是首选项? 首选项Preferences 指配置信息 首选项存储就是指对配置信息的存储 有什么方式存储? 有两种,一种存于 ...
- Java 序列化 对象序列化和反序列化
Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...
- xcb编译
sed -i "s/pthread-stubs//" configure && ./configure $XORG_CONFIG \ --enable-xinput ...
- ubuntu libtiff-dev
cc@cc:~$ dpkg -L libti libtiff5 libtiffxx5 libtimezonemap1 libtinyxml2- libtiff5-dev libtimedate-per ...
- Openjudge-计算概论(A)-晶晶赴约会
描述 晶晶的朋友贝贝约晶晶下周一起去看展览,但晶晶每周的1.3.5有课必须上课,请帮晶晶判断她能否接受贝贝的邀请,如果能输出YES:如果不能则输出NO. 输入输入有一行,贝贝邀请晶晶去看展览的日期,用 ...
- tomcat源码分析(二)启动过程
在Catalina的load方法中,首先初始化Server组件. // Start the new server if (server instanceof Lifecycle) { try { se ...
- Smarty自定义函数
自定义函数:<{方法名称}> 在html页面是可以直接赋值的:(没啥作用只是知道即可) <{$a = "hello"}><div><{$a ...
- Mac 命令行中进入带有空格的文件夹
http://blog.sina.com.cn/s/blog_5e8392b10100jkvg.html 今天在公司用mac的时候,有个文件夹的名字有空格,怎么都进不去,在网上一查原来不能直接cd 文 ...