iOS 多媒体播放音频、音效
2018-03-17 本文已影响43人
iOS_July
除了AVAudioRecorder录音之外,播放音频也是热门功能。如听歌
、玩游戏
、应用提示音
等。
一、使用系统声音服务播放音效
例如QQ、微信的消息提示音效。iOS系统提供了系统声音服务。
优点:不需要缓冲、播放速度快
导入头文件
#import <AudioToolbox/AudioToolbox.h>
创建声音对象
系统声音服务是一个C语言的函数包
SystemSoundID outSystemSoundID;//= 1
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(inFileURL), &outSystemSoundID);
inFileURL参数:指定播放音效文件的路径,是CFURLRef类型[C语言类型],
所以使用的时候,用OC创建NSURL对象,在使用__bridge CFURLRef进行类型转换。
outSystemSoundID:输出参数,代表创建的音效文件ID,根据ID来播放音效。
播放音效
创建完音效ID之后,可利用ID来播放。
//以通知的方式来播放-手机会震动
AudioServicesPlayAlertSound(inSystemSoundID);
//以系统声音的方式播放-不会震动
AudioServicesPlaySystemSound(inSystemSoundID);
笔者示例
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupStartBtn];
}
- (void)setupStartBtn{
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"播放音效" forState:UIControlStateNormal];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.frame = CGRectMake(100, 200, 200, 30);
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn addTarget:self action:@selector(clickStartBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickStartBtn{
NSLog(@"播放");
//创建一个本地音效文件的URL
NSString *path = [[NSBundle mainBundle]pathForResource:@"test.caf" ofType:nil];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil];
NSURL *inFileURL = [NSURL URLWithString:path];
//创建声音对象
SystemSoundID outSystemSoundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(inFileURL), &outSystemSoundID);
//播放音效
AudioServicesPlaySystemSound(outSystemSoundID);
}
这里的test.caf
和buyao.wav
,都是我拖拽到项目的短音频。
二、使用AVAudioPlayer播放音乐
系统声音服务只能播放少于30s的音频文件,如要播放音乐,则需要更强大的类---AVAudioPlayer。
- 属性
属性声明 | 功能描述 |
---|---|
@property(readonly, getter=isPlaying) BOOL playing; | 获取播放器是否正在播放 |
@property(readonly) NSUInteger numberOfChannels; | 获取播放器的声道数 |
@property(readonly) NSTimeInterval duration; | 获取音乐文件的持续时间 |
@property(assign, nullable) id<AVAudioPlayerDelegate> delegate; | 设置、获取AVAudioPlayer对象的代理 |
@property BOOL enableRate NS_AVAILABLE(10_8, 5_0); | 获取播放器是否可以改变播放速度 |
@property float rate NS_AVAILABLE(10_8, 5_0); | 设置、获取播放器播放速度 |
@property NSInteger numberOfLoops; | 设置、获取循环播放次数。默认为0[播放一次]、正数[播放次数]、负数[无限播放,stop方法才能停止] |
- 方法
方法声明 | 功能描述 |
---|---|
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url fileTypeHint:(NSString * __nullable)utiString error:(NSError **)outError NS_AVAILABLE(10_9, 7_0); | 创建播放器 |
- (BOOL)prepareToPlay; | 缓冲 |
- (BOOL)play; | 播放 |
- (void)pause; | 暂停播放 |
- (void)stop; | 停止播放 |
笔者的示例
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** player*/
@property (nonatomic, strong) AVAudioPlayer * player;
@end
@implementation ViewController
- (AVAudioPlayer *)player{
if (!_player) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"J.Fla - Closer.mp3" withExtension:nil];
//播放器
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url fileTypeHint:@"music" error:nil];
_player = player;
}
return _player;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupPlayerBtn];
[self setupPauseBtn];
[self setupContinueBtn];
[self setupStopBtn];
}
- (void)setupPlayerBtn{
//准备播放
[self.player prepareToPlay];
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"播放音乐" forState:UIControlStateNormal];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.frame = CGRectMake(100, 200, 200, 30);
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn addTarget:self action:@selector(clickStartBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickStartBtn {
//开始播放
[self.player play];
}
- (void)setupPauseBtn{
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"暂停播放" forState:UIControlStateNormal];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.frame = CGRectMake(100, 260, 200, 30);
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn addTarget:self action:@selector(clickPauseBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickPauseBtn{
if (self.player.isPlaying) {
//暂停播放
[self.player pause];
}
}
- (void)setupContinueBtn{
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"继续播放" forState:UIControlStateNormal];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.frame = CGRectMake(100, 320, 200, 30);
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn addTarget:self action:@selector(clickContinueBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickContinueBtn{
//播放暂停或者已经结束时
if (self.player !=nil && !self.player.isPlaying) {
[self.player play];
}
}
- (void)setupStopBtn{
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"结束播放" forState:UIControlStateNormal];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.frame = CGRectMake(100, 380, 200, 30);
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn addTarget:self action:@selector(clickStopBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)clickStopBtn{
//结束播放
[self.player stop];
}
@end
- PS:
代码写的很丑,放过放过......