播放音效
#import "VJAudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation VJAudioTool
static NSMutableDictionary *soundIDDictionry;
+(void)load{
soundIDDictionry = [NSMutableDictionary dictionary];
}
+(void)playWithSoundName:(NSString*)name
{
SystemSoundID ID = [soundIDDictionry[name] intValue];
if (!ID) {
CFURLRef urlRef = (__bridge_retained CFURLRef)[[NSBundle mainBundle] URLForResource:name withExtension:nil];
AudioServicesCreateSystemSoundID(urlRef, &ID);
[soundIDDictionry setObject:@(ID) forKey:name];
NSLog(@"create id");
}
AudioServicesPlayAlertSoundWithCompletion(ID, ^{
NSLog(@"ok");
});
}
@end
录音
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/* 录音**/
@property (nonatomic,strong) AVAudioRecorder *recorder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
NSError *error ;
// 关闭后台其他程序的声音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
// 设置当前音效为活跃状态
[audioSession setActive:YES error: &error];
}
- (IBAction)start:(id)sender {
// 结束录音
[self.recorder stop];
}
- (IBAction)stop:(id)sender {
// 开启录音
[self.recorder record];
}
- (AVAudioRecorder *)recorder{
if (!_recorder) {
NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *voicePath = [path stringByAppendingPathComponent:@"aa.caf"];
NSURL *voiceUrl =[NSURL fileURLWithPath:voicePath];
NSDictionary *recordSetting = @{
AVEncoderAudioQualityKey : @(AVAudioQualityHigh),
AVEncoderBitRateKey:@(16),
AVSampleRateKey:@(8000),
AVNumberOfChannelsKey:@2
};
_recorder = [[AVAudioRecorder alloc] initWithURL:voiceUrl settings:recordSetting error:nil];
}
return _recorder;
}
@end
异常断点调试
- 在断点栏添加异常断点 默认为全部异常拦截 改为obectivec拦截即可
- 会在代码抛异常的地方进行拦截。
[图片上传失败...(image-7c0b08-1516414056148)]
网友评论