iOS开发实用技术之音频开发

录制音频

  • 创建一个录音对象
/** 录音对象 */
@property (nonatomic, strong) AVAudioRecorder *recorder;
#pragma mark - 懒加载代码
- (AVAudioRecorder *)recorder
{
    if (_recorder == nil) {
        // 1.获取音频存放的路径
        // 1.1.URL决定的录制的音频的存放的位置
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

        // 1.2.拼接一个音频的文件名称
        NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"];

        // 1.3.将路径转成URL
        NSURL *url = [NSURL fileURLWithPath:filePath];

        // 2.设置音频的相关格式:settings : 决定音频的格式/采样率
        NSDictionary *setttings = @{
                    AVFormatIDKey : @(kAudioFormatLinearPCM),
                    AVSampleRateKey : @(8000)};

        // 3.创建录音对象
        self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setttings error:nil];
    }
    return _recorder;
}

播放音频基本使用

- (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);
}
  • 播放音效工具类抽取

    • 建一个静态变量保存声音(字典)
    static NSMutableDictionary *_soundIDs;
    • 第一使用初始化赋值
    + (void)initialize
    {
    _soundIDs = [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);
    }

音乐播放

  • 建一个播放器
 /** 播放器 */
 @property (nonatomic, strong) AVAudioPlayer *player;
#pragma mark - 懒加载代码
- (AVAudioPlayer *)player
{
    if (_player == nil) {
        // 获取资源的URL
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"1201111234.mp3" withExtension:nil];

        // 创建播放器
        NSError *error = nil;
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    }
    return _player;
}
  • 建一个字典存储播放器
- (void)playMusicWithMusicName:(NSString *)musicName
{
    // 1.从字典中取出之前保存的播放器
    AVAudioPlayer *player = self.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.将播放器存入字典
        [self.players setObject:player forKey:musicName];
    }

    // 3.播放音乐
    [player play];
}

你可能感兴趣的:(音频,录音,音乐播放)