IOS音訊開發
阿新 • • 發佈:2019-01-26
首先學習音訊開發之前,必須瞭解音訊的一些基礎知識,比如檔案格式與資料格式(編碼格式),位元率,取樣率,軌道,聲道,FFT(傅立葉快速變化),頻譜等。查了網上很多資料,到現在還是一知半解啊。。。擦擦擦。。。。
接著我們要整體瞭解下ios為我們提供處理音訊的基礎技術:核心音訊(core Audio)
有5個框架:
1.Core Audio.framework 2.AudioToolbox.framework 3.AudioUnit.framework 4.AVFoundation.framework
5.OpenAL.framework
由於核心音訊本身是一個很大的話題,涉及到很多領域的不同服務。因此,我們又將核心音訊分割成較小的模組,方便我們去理解API以及框架
1.AudioToolbox.framework:
(1) 音訊佇列服務(Audio Queue service)
(2)音訊會話服務(Audio sessionservice) (3)音訊檔案服務(
(4)音訊檔案流式服務
(5)音訊轉化服務
(6)系統聲音服務
(7)擴充套件的音訊檔案服務
2.AudioUnit.framework:音訊單元服務 3.OpenAL.framework: OpenAL 4.AVFoundation.framework:(1)AVAudioPlayer(2)AVAudioRecorder(3)AVAudioSession 5.CoreAudio:這個框架並不提供服務,僅提供其他框架可以使用的標頭檔案和資料型別 一、音訊播放
詳見連結:http://www.cnblogs.com/jqyp/archive/2012/02/11/2346869.html
1.AVAudioPlayer
(1)在AVFoundation.framework框架中
(2)屬性
(3)方法
(4)如何使用
2.音訊服務( System SoundServices)
3.音訊佇列(Audio QueueServices)
4.OpenAL
5.MPMusciPlayController 6.音訊單元服務 7.系統聲音播放 等
二、音訊錄製
1.AVAuidoRecorder
2.Audio Queue Services 等
三、音訊剪下(擷取)
1.建立AVURLAsset物件(繼承了AVAsset)
NSString *path = [[NSBundlemainBundle] pathForResource:@"陳奕迅 - 想哭" ofType:@"mp3"];
NSURL *songURL = [NSURLfileURLWithPath:path];
AVURLAsset *songAsset =[AVURLAsset URLAssetWithURL:songURL options:nil];
2.建立音訊檔案
NSArray*dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentsDirectoryPath = [dirs objectAtIndex:0];
NSString*exportPath = [[documentsDirectoryPathstringByAppendingPathComponent:EXPORT_NAME]retain];//EXPORT_NAME為匯出音訊檔名
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]){
[[NSFileManager defaultManager] removeItemAtPath:exportPatherror:nil];
}
NSURL*exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter = [[AVAssetWriterassetWriterWithURL:exportURL
fileType:AVFileTypeCoreAudioFormat
error:&assetError]
retain];
if(assetError) {
NSLog (@"error: %@", assetError);
return;
}
3.建立音訊輸出會話
AVAssetExportSession *exportSession = [AVAssetExportSessionexportSessionWithAsset:songAsset
presetName:AVAssetExportPresetAppleM4A];
4.設定音訊擷取時間區域 (CMTime在CoreMedio框架中,所以要事先匯入框架)
CMTimestartTime = CMTimeMake([_startTime.text floatValue], 1);
CMTimestopTime = CMTimeMake([_endTime.text floatValue], 1);
CMTimeRangeexportTimeRange = CMTimeRangeFromTimeToTime(startTime,stopTime);
5.設定音訊輸出會話並執行
exportSession.outputURL = [NSURL fileURLWithPath:filePath];// output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output filetype
exportSession.timeRange = exportTimeRange; // trim timerange
[exportSessionexportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status){
NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed ==exportSession.status) {
// a failure may happen because of an event out of yourcontrol
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"AVAssetExportSessionStatusFailed");
} else {
NSLog(@"Export Session Status: %d", exportSession.status);
}
}];
總得來說:我們再APP的目錄下建立了音訊檔案,然後建立了一個音訊的AVAsset物件(AVAsset
四、歌詞同步
注:以上只是我的個人見解,如有什麼錯誤,請大家見諒。。也是最近剛看的。我會持續跟新的。
有5個框架:
1.Core Audio.framework 2.AudioToolbox.framework 3.AudioUnit.framework 4.AVFoundation.framework
由於核心音訊本身是一個很大的話題,涉及到很多領域的不同服務。因此,我們又將核心音訊分割成較小的模組,方便我們去理解API以及框架
1.AudioToolbox.framework:
(1) 音訊佇列服務(Audio Queue service)
(2)音訊會話服務(Audio sessionservice) (3)音訊檔案服務(
(4)音訊檔案流式服務
(5)音訊轉化服務
(7)擴充套件的音訊檔案服務
2.AudioUnit.framework:音訊單元服務 3.OpenAL.framework: OpenAL 4.AVFoundation.framework:(1)AVAudioPlayer(2)AVAudioRecorder(3)AVAudioSession 5.CoreAudio:這個框架並不提供服務,僅提供其他框架可以使用的標頭檔案和資料型別 一、音訊播放
詳見連結:http://www.cnblogs.com/jqyp/archive/2012/02/11/2346869.html
1.AVAudioPlayer
(1)在AVFoundation.framework框架中
(2)屬性
(3)方法
(4)如何使用
2.音訊服務(
3.音訊佇列(Audio QueueServices)
4.OpenAL
5.MPMusciPlayController 6.音訊單元服務 7.系統聲音播放 等
二、音訊錄製
1.AVAuidoRecorder
2.Audio Queue Services 等
三、音訊剪下(擷取)
1.建立AVURLAsset物件(繼承了AVAsset)
NSString *path = [[NSBundlemainBundle] pathForResource:@"陳奕迅 - 想哭" ofType:@"mp3"];
NSURL *songURL = [NSURLfileURLWithPath:path];
AVURLAsset *songAsset =[AVURLAsset URLAssetWithURL:songURL options:nil];
2.建立音訊檔案
NSArray*dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentsDirectoryPath = [dirs objectAtIndex:0];
NSString*exportPath = [[documentsDirectoryPathstringByAppendingPathComponent:EXPORT_NAME]retain];//EXPORT_NAME為匯出音訊檔名
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]){
[[NSFileManager defaultManager] removeItemAtPath:exportPatherror:nil];
}
NSURL*exportURL = [NSURL fileURLWithPath:exportPath];
AVAssetWriter *assetWriter = [[AVAssetWriterassetWriterWithURL:exportURL
fileType:AVFileTypeCoreAudioFormat
error:&assetError]
retain];
if(assetError) {
NSLog (@"error: %@", assetError);
return;
}
3.建立音訊輸出會話
AVAssetExportSession *exportSession = [AVAssetExportSessionexportSessionWithAsset:songAsset
presetName:AVAssetExportPresetAppleM4A];
4.設定音訊擷取時間區域 (CMTime在CoreMedio框架中,所以要事先匯入框架)
CMTimestartTime = CMTimeMake([_startTime.text floatValue], 1);
CMTimestopTime = CMTimeMake([_endTime.text floatValue], 1);
CMTimeRangeexportTimeRange = CMTimeRangeFromTimeToTime(startTime,stopTime);
5.設定音訊輸出會話並執行
exportSession.outputURL = [NSURL fileURLWithPath:filePath];// output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output filetype
exportSession.timeRange = exportTimeRange; // trim timerange
[exportSessionexportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status){
NSLog(@"AVAssetExportSessionStatusCompleted");
} else if (AVAssetExportSessionStatusFailed ==exportSession.status) {
// a failure may happen because of an event out of yourcontrol
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"AVAssetExportSessionStatusFailed");
} else {
NSLog(@"Export Session Status: %d", exportSession.status);
}
}];
總得來說:我們再APP的目錄下建立了音訊檔案,然後建立了一個音訊的AVAsset物件(AVAsset
代表一個抽象的媒體,包含標題,檔案大小等等
),接著建立一個AVAsset的輸出會話AVAssetExportSession,最後就是設定我們要剪下(擷取)的時間區域CMTimeRange,然後執行AVAssetExportSeesion的回撥函式exportAsynchronouslyWithCompletionHandler。四、歌詞同步
注:以上只是我的個人見解,如有什麼錯誤,請大家見諒。。也是最近剛看的。我會持續跟新的。