调用系统功能 录音/播放音效/音频
框架< AVFoundation/AVFoundation.h >
1. 录音
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryRecord error:&error];
if (session == nil) {
NSLog(@"创建失败:%@",[error description]);
} else {
[session setActive:YES error:nil];
}
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [path stringByAppendingString:@"/record.caf"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 8000.0],AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey,nil];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSetting error:nil];
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
[recorder record];
if ([recorder isRecording]) {
[recorder stop];
}
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[player play];
- 实现AVAudioRecorderDelegate代理
// 执行stop时调用
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
NSLog(@"录音完成");
}
- 获取录音过程中的分贝(要在录音过程中获取分贝数,要在录音前先把AVAudioRecorder的属性meteringEnabled设置成YES.)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(getPeakPower) userInfo:nil repeats:YES];
-(void)getPeakPower {
if (self.audioRecorder.isRecording) {
[self.audioRecorder updateMeters];
float peakPower = [self.audioRecorder peakPowerForChannel:0];
NSLog(@"%f", peakPower);
}
}
2. 播放音效
SystemSoundID soundId = 0;
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
CFURLRef urlref = (__bridge CFURLRef)(url);
AudioServicesCreateSystemSoundID(urlref, &soundId);
AudioServicesPlayAlertSound(soundId);
AudioServicesPlaySystemSound(soundId);
AudioServicesPlayAlertSoundWithCompletion(soundId, nil);
AudioServicesDisposeSystemSoundID(soundId);
@property (nonatomic,assign) SystemSoundID soundId;
@property (nonatomic,strong) NSMutableDictionary *soundIDs;
- (NSMutableDictionary *)soundIDs {
if (!_soundIDs) {
_soundIDs = [NSMutableDictionary dictionary];
}
return _soundIDs;
}
- (SystemSoundID)soundId {
if (_soundId == 0) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
CFURLRef urlref = (__bridge CFURLRef)(url);
AudioServicesCreateSystemSoundID(urlref, &_soundId);
}
return _soundId;
}
- (void)play {
SystemSoundID soundId = 0;
soundId = [self.soundIDs[@""] unsignedIntValue];
if (soundId == 0) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
CFURLRef urlref = (__bridge CFURLRef)(url);
AudioServicesCreateSystemSoundID(urlref, &soundId);
[self.soundIDs setObject:@(soundId) forKey:@""];
}
AudioServicesPlayAlertSound(soundId);
}
3. 播放音乐
- (AVAudioPlayer *)player {
if (_player == nil) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.player prepareToPlay];
}
return _player;
}
- (BOOL)prepareToPlay; // 准备播放
- (BOOL)play; // 播放
- (BOOL)playAtTime:(NSTimeInterval)time;// 在指定时间播放音频
- (void)pause; // 暂停播放
- (void)stop; // 停止播放
@property NSTimeInterval currentTime;
@property(readonly) NSTimeInterval duration;
// 播放完成调用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
// 播放解析错误调用
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error;
4. 播放视频
方法一:
- (AVPlayer *)player {
if (_player == nil) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
_player = [AVPlayer playerWithPlayerItem:item];
}
return _player;
}
方法二:(ios 9过期)
- (MPMoviePlayerController *)playerController {
if (_playerController == nil) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"" withExtension:nil];
_playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
_playerController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 9 / 16);
[self.view addSubview:_playerController.view];
}
return _playerController;
}
- (void)play; // 播放
- (void)pause; // 暂停