iOS音頻開發(fā)(錄音/播放/剪輯/合成/壓縮轉(zhuǎn)碼)

錄音
//音頻會話
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
/*
AVAudioSessionCategoryPlayAndRecord :錄制和播放
AVAudioSessionCategoryAmbient       :用于非以語音為主的應(yīng)用,隨著靜音鍵和屏幕關(guān)閉而靜音.
AVAudioSessionCategorySoloAmbient   :類似AVAudioSessionCategoryAmbient不同之處在于它會中止其它應(yīng)用播放聲音。
AVAudioSessionCategoryPlayback      :用于以語音為主的應(yīng)用,不會隨著靜音鍵和屏幕關(guān)閉而靜音.可在后臺播放聲音
AVAudioSessionCategoryRecord        :用于需要錄音的應(yīng)用,除了來電鈴聲,鬧鐘或日歷提醒之外的其它系統(tǒng)聲音都不會被播放,只提供單純錄音功能.
*/
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
[session setActive:YES error:nil];
// 錄音參數(shù)
NSDictionary *setting = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,// 編碼格式
                               [NSNumber numberWithFloat:8000], AVSampleRateKey, //采樣率
                               [NSNumber numberWithInt:2], AVNumberOfChannelsKey, //通道數(shù)
                               [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,  //采樣位數(shù)(PCM專屬)
                               [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,  //是否允許音頻交叉(PCM專屬)
                               [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,  //采樣信號是否是浮點數(shù)(PCM專屬)
                               [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,  //是否是大端存儲模式(PCM專屬)
                               [NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey,  //音質(zhì)
                               nil];
self.audioRecorder.delegate = self;
//開啟音頻測量
self.audioRecorder.meteringEnabled = YES;
//保存路徑
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:filePath] settings:setting error:nil];
//準備 / 開始錄音
[self.audioRecorder prepareToRecord];
[self.audioRecorder record];

//暫停錄音
[self.audioRecorder pause];
//停止錄音
[self.audioRecorder stop];
//刪除錄音

//AVAudioRecorderDelegate
//when a recording has been finished or stopped. This method is NOT called if the recorder is stopped due to an interruption.(錄音完成)
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;
//if an error occurs while encoding it will be reported to the delegate(編碼發(fā)生錯誤)
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * __nullable)error;
//when the audio session has been interrupted while the recorder was recording. The recorded file will be closed.(被打斷)
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder NS_DEPRECATED_IOS(2_2, 8_0);
//when the audio session interruption has ended and this recorder had been interrupted while recording(被打斷結(jié)束)
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags NS_DEPRECATED_IOS(6_0, 8_0);
播放
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[session setActive:YES error:nil];

//開啟接近監(jiān)視(靠近耳朵的時候聽筒播放,離開的時候揚聲器播放)
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:)name:UIDeviceProximityStateDidChangeNotification object:nil];

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];

self.audioPlayer.delegate = self;
//準備播放 / 播放
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];

//停止播放
[self.audioPlayer stop];
//暫停播放
[self.audioPlayer pause];

//proximityStateChange:(NSNotificationCenter *)notification方法
if ([[UIDevice currentDevice] proximityState] == YES) {
    //靠近耳朵
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
} else {
    //離開耳朵
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}

//AVAudioPlayerDelegate
//when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption(播放完成)
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
//if an error occurs while decoding it will be reported to the delegate.(解碼結(jié)束)
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error;
//when the audio session has been interrupted while the player was playing. The player will have been paused(被打斷)
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 8_0);
//when the audio session interruption has ended and this player had been interrupted while playing(被打斷結(jié)束)
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags NS_DEPRECATED_IOS(6_0, 8_0);
剪輯

將路徑filePath下的音頻文件從time截取到time2后在resultPath中輸出

//AVURLAsset是AVAsset的子類,AVAsset類專門用于獲取多媒體的相關(guān)信息,包括獲取多媒體的畫面、聲音等信息.而AVURLAsset子類的作用則是根據(jù)NSURL來初始化AVAsset對象.
AVURLAsset *videoAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:filePath]];
//音頻輸出會話
//AVAssetExportPresetAppleM4A: This export option will produce an audio-only .m4a file with appropriate iTunes gapless playback data(輸出音頻,并且是.m4a格式)
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:videoAsset presetName:AVAssetExportPresetAppleM4A];
//設(shè)置輸出路徑 / 文件類型 / 截取時間段
exportSession.outputURL = [NSURL fileURLWithPath:resultPath];
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.timeRange = CMTimeRangeFromTimeToTime(CMTimeMake(time1, 1), CMTimeMake(time2, 1));
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    //exporeSession.status
}];
合成

將路徑filePath1和路徑filePath2下的音頻合成

//AVURLAsset子類的作用則是根據(jù)NSURL來初始化AVAsset對象.
AVURLAsset *videoAsset1 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:filePath1] options:nil];
AVURLAsset *videoAsset2 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:filePath2] options:nil];
//音頻軌跡(一般視頻至少有2個軌道,一個播放聲音,一個播放畫面.音頻有一個)
AVAssetTrack *assetTrack1 = [[videoAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVAssetTrack *assetTrack2 = [[videoAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
//AVMutableComposition用來合成視頻或音頻
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
// 把第二段錄音添加到第一段后面
[compositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset1.duration) ofTrack:assetTrack1 atTime:kCMTimeZero error:nil];
[compositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset2.duration) ofTrack:assetTrack2 atTime:videoAsset1.duration error:nil];
//輸出
AVAssetExportSession *exporeSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
exporeSession.outputFileType = AVFileTypeAppleM4A;
exporeSession.outputURL = [NSURL fileURLWithPath:resultPath];
[exporeSession exportAsynchronouslyWithCompletionHandler:^{
    //exporeSession.status
}];
壓縮轉(zhuǎn)碼

下載LAME (Lame Aint an MP3 Encoder)

雙擊解壓后放到一個文件夾下,文件夾需要命名為lame,否則無法生成.h和.a文件

使用Terminal進入該文件夾,編譯生成靜態(tài)庫,腳本代碼

將fat-lame文件夾下的include文件夾和lib文件夾放入工程,再寫一個OC的類調(diào)用lame.h

@try {
    int read, write;
    FILE *pcm = fopen([filePath cStringUsingEncoding:1], "rb");//被轉(zhuǎn)換的音頻文件位置
    fseek(pcm, 4*1024, SEEK_CUR);
    FILE *mp3 = fopen([resultPath cStringUsingEncoding:1], "wb");//生成的Mp3文件位置

    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;
    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];

    // 初始化lame編碼器
    lame_t lame = lame_init();
    // 設(shè)置lame mp3編碼的采樣率 / 聲道數(shù) / 比特率
    lame_set_in_samplerate(lame, 8000);
    lame_set_num_channels(lame,2);
    lame_set_out_samplerate(lame, 8000);
    lame_set_brate(lame, 8);
    // MP3音頻質(zhì)量.0~9.其中0是最好,非常慢,9是最差.
    lame_set_quality(lame, 7);

    // 設(shè)置mp3的編碼方式
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);

    do {
        size_t size = (size_t)(2 * sizeof(short int));
        read = fread(pcm_buffer, size, PCM_SIZE, pcm);
        if (read == 0) {
            write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
        } else {
            write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
        }
        fwrite(mp3_buffer, write, 1, mp3);

    } while (read != 0);

    lame_close(lame);
    fclose(mp3);
    fclose(pcm);
}
@catch (NSException *exception) {
    NSLog(@"%@",[exception description]);
}
@finally {
    // 轉(zhuǎn)碼完成
    return resultPath;
}

基本上可以將100K左右的錄音文件壓縮到10K以下

參考1 參考2 參考3 參考4

最后編輯于
?著作權(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)容