音頻播放AVAudioPlayer,AVAudioRecorder,AVAudioSession

AVAudioPlayer

  • 播放對象
  self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:self.fileUrl error:nil];
    
    // 3.音頻信息
    NSString *msg = [NSString stringWithFormat:@"音頻文件聲道數(shù):%ld\n 音頻文件持續(xù)時間:%g",self.player.numberOfChannels,self.player.duration];

    
    NSLog(@"%@",msg);
    
    if (self.player) {
        
        [self.player prepareToPlay];
        
        [self.player play];
        
    }

AVAudioRecorder

  • 錄音對象
//(1)url
    NSString *urlStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/recoder.aac"];
    
    //刪除之前的錄音
    [[NSFileManager defaultManager]removeItemAtPath:urlStr error:nil];
    
    
    self.fileUrl = [NSURL fileURLWithPath:urlStr];
    
    
    
    //(2)設(shè)置錄音的音頻參數(shù)
    /*
     1 ID號:acc
     2 采樣率(HZ):每秒從連續(xù)的信號中提取并組成離散信號的采樣個數(shù)
     3 通道的個數(shù):(1 單聲道 2 立體聲)
     4 采樣位數(shù)(8 16 24 32) 衡量聲音波動變化的參數(shù)
     5 大端或者小端 (內(nèi)存的組織方式)
     6 采集信號是整數(shù)還是浮點數(shù)
     7 音頻編碼質(zhì)量
     
     
     */
    NSDictionary *info = @{
                           AVFormatIDKey:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],//音頻格式
                           AVSampleRateKey:@8000,//采樣率
                           AVNumberOfChannelsKey:@1,//聲道數(shù)
                           AVLinearPCMBitDepthKey:@8,//采樣位數(shù)
                           AVLinearPCMIsBigEndianKey:@NO,
                           AVLinearPCMIsFloatKey:@NO,
                           AVEncoderAudioQualityKey:[NSNumber numberWithInt:AVAudioQualityHigh],
                           
                           };
    
    
    /*
     url:錄音文件保存的路徑
     settings: 錄音的設(shè)置
     error:錯誤
     */
    
    NSError * error;
    
    self.recorder = [[AVAudioRecorder alloc]initWithURL:self.fileUrl settings:info error:&error];
    
    self.recorder.delegate = self;
    
    if(error) {
        NSLog(@"錯誤%@", error); // 在這里我們簡單打印錯誤, 在實際項目中我們要做容錯判斷
    }
    
   BOOL isno = [self.recorder prepareToRecord];
    
    if (!isno) {
        NSLog(@"失敗");
    }
    
    [self.recorder record];

AVAudioSession

  • 硬件設(shè)置單例
//開啟擴(kuò)音
   self.section = [AVAudioSession sharedInstance];
    
    NSError *sessionError;
    
    [self.section setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
    
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (
                             kAudioSessionProperty_OverrideAudioRoute,
                             sizeof (audioRouteOverride),
                             &audioRouteOverride
                             );
    
    if(self.section == nil)
        
        NSLog(@"Error creating session: %@", [sessionError description]);
    
    else
        [self.section setActive:YES error:nil];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容