标签:
一、录音
1 1 #import "ViewController.h" 2 2 #import <AVFoundation/AVFoundation.h> 3 3 4 4 @interface ViewController () 5 5 6 6 /** 录音对象 */ 7 7 @property (nonatomic, strong) AVAudioRecorder *recorder; 8 8 9 9 @end 10 10 11 11 12 12 #pragma mark - 录制的控制 13 13 - (IBAction)startRecorder { 14 14 [self.recorder record]; 15 15 } 16 16 17 17 - (IBAction)stopRecorder { 18 18 [self.recorder stop]; 19 19 } 20 20 21 21 #pragma mark - 懒加载代码 22 22 - (AVAudioRecorder *)recorder 23 23 { 24 24 if (_recorder == nil) { 25 25 // 1.获取音频存放的路径 26 26 // 1.1.URL决定的录制的音频的存放的位置 27 27 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 28 28 29 29 // 1.2.拼接一个音频的文件名称 30 30 NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"]; 31 31 32 32 // 1.3.将路径转成URL 33 33 NSURL *url = [NSURL fileURLWithPath:filePath]; 34 34 35 35 // 2.设置音频的相关格式:settings : 决定音频的格式/采样率 36 36 NSDictionary *setttings = @{ 37 37 AVFormatIDKey : @(kAudioFormatLinearPCM), 38 38 AVSampleRateKey : @(8000)}; 39 39 40 40 // 3.创建录音对象 41 41 self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setttings error:nil]; 42 42 } 43 43 return _recorder; 44 44 }
二、播放音效
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1.定义一个SystemSoundID SystemSoundID soundID = 0; // 2.根据音频文件资源创建SystemSoundID(赋值) NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil]; CFURLRef urlRef = (__bridge CFURLRef)(url); AudioServicesCreateSystemSoundID(urlRef, &soundID); // 3.播放音频 // 播放音效的同时有震动效果 AudioServicesPlayAlertSound(soundID); // 仅仅是播放音效 // AudioServicesPlaySystemSound(soundID); }
三、音乐播放工具类
.h
/** * 播放音效 * * @param soundName 音效名称 */ + (void)playSoundWithSoundName:(NSString *)soundName; /** * 播放音乐 * * @param musicName 音乐名称 */ + (void)playMusicWithMusicName:(NSString *)musicName; /** * 播放音乐 * * @param musicName 音乐名称 */ + (void)pauseMusicWithMusicName:(NSString *)musicName; /** * 播放音乐 * * @param musicName 音乐名称 */ + (void)stopMusicWithMusicName:(NSString *)musicName;
.m
static NSMutableDictionary *_soundIDs; static NSMutableDictionary *_players; + (void)initialize { _soundIDs = [NSMutableDictionary dictionary]; _players = [NSMutableDictionary dictionary]; } + (void)playSoundWithSoundName:(NSString *)soundName { // 1.从字典中取出之前保存的soundID SystemSoundID soundID = [[_soundIDs objectForKey:soundName] unsignedIntValue]; // 2.如果取出为0,表示之前没有加载当前声音 if (soundID == 0) { // 2.1.根据资源文件加载soundID CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundName withExtension:nil]; AudioServicesCreateSystemSoundID(url, &soundID); // 2.2.存入字典 [_soundIDs setObject:@(soundID) forKey:soundName]; } // 3.播放声音 AudioServicesPlaySystemSound(soundID); } + (void)playMusicWithMusicName:(NSString *)musicName { // 1.从字典中取出之前保存的播放器 AVAudioPlayer *player = _players[musicName]; // 2.判断播放器是否为nil,如果为空,则创建播放器 if (player == nil) { // 2.1.加载对应的资源 NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil]; // 2.2.创建播放器 player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; // 2.3.将播放器存入字典 [_players setObject:player forKey:musicName]; } // 3.播放音乐 [player play]; } + (void)pauseMusicWithMusicName:(NSString *)musicName { // 1.从字典中取出之前保存的播放器 AVAudioPlayer *player = _players[musicName]; // 2.判断播放器是否为空,如果不为空,则暂停 if (player) { [player pause]; } } + (void)stopMusicWithMusicName:(NSString *)musicName { // 1.从字典中取出之前保存的播放器 AVAudioPlayer *player = _players[musicName]; // 2.判断播放器是否为空,如果不为空,则停止音乐 if (player) { [player stop]; [_players removeObjectForKey:musicName]; } }
标签:
原文地址:http://www.cnblogs.com/dmcode/p/4865017.html