iOS開發實用技術之音頻開發

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/ab20514/article/details/50386800

錄製音頻

  • 創建一個錄音對象
/** 錄音對象 */
@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];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章