iOS之音頻

1.錄音

-(AVAudioRecorder *)recoder
{
    if (_recoder == nil) {
        
        //1.建立存放錄音文件的地址
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [path stringByAppendingPathComponent:@"recoder.caf"];
        NSURL *url = [NSURL URLWithString:filePath];
        
       //iOS8必須實例前設置開啓代理
        AVAudioSession *session = [AVAudioSession sharedInstance];
        NSError *setCategoryError = nil;
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];
        
        if(setCategoryError){
            NSLog(@"%@", [setCategoryError description]);
        }

        //2.建立錄音對象
        self.recoder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
        
        //3.準備錄音
        [self.recoder prepareToRecord];
    }
    return _recoder;
}

- (IBAction)start:(UIButton *)sender {
    
    //4.錄音
    [self.recoder record];
    
}
- (IBAction)stop:(UIButton *)sender {
    
    //5.中止錄音
    [self.recoder stop];
}

 

2.播放音效

  /***   AudioServicesPlaySystemSound若是系統靜音, 則用戶提示就沒有什麼效果
         AudioServicesPlayAlertSound若是靜音會震動,不然聲音提示
         AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);單一的震動
     */
    播放聲音能夠用二者,播放提醒只能用,播放震動 
     //振動   
    var soundID = SystemSoundID(kSystemSoundID_Vibrate)
    AudioServicesPlaySystemSound(soundID)
    
    
    ========================播放音效公用代碼=====================
    //1.建立SystemSoundID,根據音效文件來生成
    SystemSoundID soundID = 0;
    
    //2.根據音效文件,來生成SystemSoundID
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"win.aac" withExtension:nil];
    CFURLRef urlRef = (__bridge CFURLRef)(url);
    AudioServicesCreateSystemSoundID(urlRef, &soundID);
    
    //3.播放印象
    AudioServicesPlaySystemSound(soundID);
    //AudioServicesPlayAlertSound(soundID);
    
    
    =================封裝播放音效========================

static NSMutableDictionary *_soundIDs;
+ (NSMutableDictionary *)soundIDs
{
    if (!_soundIDs) {
        _soundIDs = [NSMutableDictionary dictionary];
    }
    return _soundIDs;
}

+ (void)playAudioWithFilename:(NSString *)filename
{
    if (filename == nil) {
        return;
    }
    
    //1.從字典中取出音效ID
    SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
    
    //2.判斷ID是否爲nil
    if (!soundID) {
        NSLog(@"建立新的soundID");
        
        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
        if (!url) {
            return;
        }
        
        //創新音效ID
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
        
        //將音效ID添加到字典中
        [self soundIDs][filename] = @(soundID);
        
    }
    
    //3.播放音效
    AudioServicesPlaySystemSound(soundID);
}

//內存警告的是否釋放播放音效
+ (void)disposeAudioWithFilename:(NSString *)filename
{
    // 0.判斷文件名是否爲nil
    if (filename == nil) {
        return;
    }
    
    // 1.從字典中取出音效ID
    SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
    
    if (soundID) {
        // 2.銷燬音效ID
        AudioServicesDisposeSystemSoundID(soundID);
        
        // 3.從字典中移除已經銷燬的音效ID
        [[self soundIDs] removeObjectForKey:filename];
    }
}

 

3.播放音樂

//若是須要播放多首音樂,能夠參考播放音效的操做,將id做爲value,name做爲key。


-(AVAudioPlayer *)player
{
    if (_player == nil) {
        
        //取出資源的url
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"a.mp3" withExtension:nil];
        
        //建立播放器
        NSError *error = nil;
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        
        //準備播放
        [self.player prepareToPlay];
    }
    return _player;
}

- (IBAction)start:(id)sender {
    
    [self.player play];
}

- (IBAction)stop:(id)sender {
    
    [self.player stop];
}

- (IBAction)pause:(id)sender {
    
    [self.player pause];
    //中止沒法,只能暫停,能夠清空播放器對象
    self.player = nil;
}
相關文章
相關標籤/搜索