這一篇主要是CoreAudio官方文檔的重點(diǎn)內(nèi)容的筆記。
iOS的CoreAudio是通過callback函數(shù)與App交互的。其中需要設(shè)置回調(diào)函數(shù)有以下幾種情況:
CoreAudio會(huì)向回調(diào)函數(shù)給App傳入PCM音頻數(shù)據(jù),然后App需要在回調(diào)函數(shù)中將音頻數(shù)據(jù)寫入文件文件系統(tǒng)。(錄音時(shí)候)CoreAudio會(huì)需要向App請(qǐng)求一些音頻數(shù)據(jù),App通過從文件系統(tǒng)中讀取音頻數(shù)據(jù),然后通過callback函數(shù)傳遞給CoreAudio。(播放時(shí)候)通過注冊(cè)屬性觀察者,監(jiān)聽CoreAudio的屬性,注冊(cè)回調(diào)函數(shù)下面是一個(gè)使用Audio Queue Services的屬性監(jiān)聽器的callback函數(shù)的調(diào)用模板。
12345 | typedef void (*AudioQueuePRopertyListenerProc) ( void * inUserData, AudioQueueRef inAQ, AudioQueuePropertyID inID ); |
在實(shí)現(xiàn)和使用這個(gè)callback函數(shù)時(shí)候,你需要完成兩件事:
實(shí)現(xiàn)這個(gè)函數(shù)。例如,你可以實(shí)現(xiàn)property listener callback,根據(jù)audio是否在running或者stop狀態(tài),去改變更新UI。注冊(cè)callback函數(shù)時(shí)候帶上userData數(shù)據(jù),在callback函數(shù)觸發(fā)時(shí)候使用。下面是一個(gè)property listener callback函數(shù)的實(shí)現(xiàn):
12345678910 | static void propertyListenerCallback ( void *inUserData, AudioQueueRef queueObject, AudioQueuePropertyID propertyID) { AudioPlayer *player = (AudioPlayer *) inUserData; // gets a reference to the playback object [player.notificationDelegate updateUserInterfaceOnAudioQueueStateChange: player]; // your notificationDelegate class implements the UI update method} |
下面是注冊(cè)一個(gè)callback函數(shù)的例子:
123456 | AudioQueueAddPropertyListener ( self.queueObject, // the object that will invoke your callback kAudioQueueProperty_IsRunning, // the ID of the property you want to listen for propertyListenerCallback, // a reference to your callback function self); |
這部分內(nèi)容是iOS中支持的音頻格式,理解以后對(duì)后面的音頻相關(guān)的編程能夠理解更加深刻。
在CoreAudio中,使用AudioStreamBasicDescription和AudioStreamPacketDescription這兩個(gè)類型描述了通用的音頻數(shù)據(jù)類型,包括壓縮音頻數(shù)據(jù),非壓縮的音頻數(shù)據(jù)。他們的數(shù)據(jù)結(jié)構(gòu)如下:
12345678910111213141516171819 | struct AudioStreamBasicDescription { Float64 mSampleRate; UInt32 mFormatID; UInt32 mFormatFlags; UInt32 mBytesPerPacket; UInt32 mFramesPerPacket; UInt32 mBytesPerFrame; UInt32 mChannelsPerFrame; UInt32 mBitsPerChannel; UInt32 mReserved; //0};typedef struct AudioStreamBasicDescription AudioStreamBasicDescription;struct AudioStreamPacketDescription { SInt64 mStartOffset; UInt32 mVariableFramesInPacket; UInt32 mDataByteSize;};typedef struct AudioStreamPacketDescription AudioStreamPacketDescription; |
compressed audio formats use a varying number of bits per sample. For these formats, the value of the mBitsPerChannel member is 0.
前面定義過,將一個(gè)或者多個(gè)frames稱為一個(gè)packet。或者說packet是最有意義的一組frames,它在audio file中代表一個(gè)有意義的時(shí)間單元。使用Core Audio中一般是對(duì)packets進(jìn)行處理的。
每個(gè)audio data格式在packets被裝配完成以后,它的fromat就被確定了。ASBD數(shù)據(jù)結(jié)構(gòu)通過mBytesPerPacket和mFramesPerPacket描述音頻格式的packet信息,其中頁包含其他的信息。
在整個(gè)Core Audio中可能會(huì)用到三種不同的packets:
CBR (constant bit rate) formats:例如 linear PCM and IMA/ADPCM,所有的packet使用相同的大小。VBR (variable bit rate) formats:例如 AAC,Apple Lossless,MP3,所有的packets擁有相同的frames,但是每個(gè)sample中的bits數(shù)目不同。VFR (variable frame rate) formats:packets擁有數(shù)目不同的的frames。在Core Audio中使用VBR或者VFR格式,使用aspD結(jié)構(gòu)體只能用來描述單個(gè)packet。如果是record或者play VBR或者VFR音頻文件,需要涉及到多個(gè)ASPD結(jié)構(gòu)。
在AudioFileService等接口中,都是通過pakcets工作的。例如AudioFileReadPackets會(huì)得到一系列的packets,同時(shí)會(huì)得到一個(gè)數(shù)組的AudioStreamPacketDescription。
下面是通過packets計(jì)算audio data buffer的大小:
12345678910111213141516171819202122232425262728293031323334353637 | - (void) calculateSizesFor: (Float64) seconds { UInt32 maxPacketSize; UInt32 propertySize = sizeof (maxPacketSize); AudioFileGetProperty ( audioFileID, kAudioFilePropertyPacketSizeUpperBound, &propertySize, &maxPacketSize ); static const int maxBufferSize = 0x10000; // limit maximum size to 64K static const int minBufferSize = 0x4000; // limit minimum size to 16K if (audioFormat.mFramesPerPacket) { Float64 numPacketsForTime = audioFormat.mSampleRate / audioFormat.mFramesPerPacket * seconds; [self setBufferByteSize: numPacketsForTime * maxPacketSize]; } else { // if frames per packet is zero, then the codec doesn't know the // relationship between packets and time. Return a default buffer size [self setBufferByteSize: maxBufferSize > maxPacketSize ? maxBufferSize : maxPacketSize]; } // clamp buffer size to our specified range if (bufferByteSize > maxBufferSize && bufferByteSize > maxPacketSize) { [self setBufferByteSize: maxBufferSize]; } else { if (bufferByteSize < minBufferSize) { [self setBufferByteSize: minBufferSize]; } } [self setNumPacketsToRead: self.bufferByteSize / maxPacketSize];} |
將音頻數(shù)據(jù)從一種audio data轉(zhuǎn)換成另外一種audio data。常見的有三中音頻格式轉(zhuǎn)換:
Decoding an audio format (such as AAC (Advanced Audio Coding)) to linear PCM format.Converting linear PCM data into a different audio format.Converting between different variants of linear PCM (for example, converting 16-bit signed integer linear PCM to 8.24 fixed-point linear PCM).如果要使用聲音文件,你需要使用Audio File Services的接口。一般而且,在iOS中需要與音頻文件的創(chuàng)建,操作都離不開Audio File Services。
使用AudioFileGetGlobalInfoSize和AudioFileGetGlobalInfo分別分配info的內(nèi)存和獲取info的內(nèi)容。你可以獲取以下的內(nèi)容:
為了創(chuàng)建一個(gè)能夠存儲(chǔ)音頻數(shù)據(jù)的audio file,你需要進(jìn)行以下三步:
使用CFURL或者NSURL表示的系統(tǒng)文件的路徑你需要?jiǎng)?chuàng)建的文件的類型的標(biāo)識(shí)identifier,這些identifier定義在Audio File Types枚舉中。例如,為了創(chuàng)建一個(gè)CAF文件,你需要使用kAudioFileCAFType的identifier。創(chuàng)建過程中你需要提供音頻數(shù)據(jù)的ASBD結(jié)構(gòu)體。為了獲取ASBD,你可以先提供ASBD結(jié)構(gòu)體的部分成員的值,然后通過函數(shù)讓Audio File Services將剩余的信息填滿。下面是創(chuàng)建一個(gè)AudioFile的方法:
1234567 | AudioFileCreateWithURL ( audioFileURL, kAudioFileCAFType, &audioFormat, kAudioFileFlags_EraseFile, &audioFileID // the function provides the new file object here); |
為了打開sound file,需要使用AudioFileOpenURL函數(shù),該函數(shù)會(huì)返回一個(gè)唯一ID,供后面使用。
為了獲取sound file的一些屬性,通常使用AudioFileGetPropertyInfo和AudioFileGetProperty,日常使用的屬性以下:
iOS中,我們經(jīng)常需要使用Audio File Services去讀寫audio data到sound file中。讀和寫是一對(duì)相反的內(nèi)容,操作的對(duì)象都可以是bytes或者packets,但是一般而言都是直接使用的packets。
iOS支持的sound file格式如下:
| Format name | Format filename extensions |
|---|---|
| AIFF | .aif, .aiff |
| CAF | .caf |
| MPEG-1, layer 3 | .mp3 |
| MPEG-2 or MPEG-4 ADTS | .aac |
| MPEG-4 | .m4a, .mp4 |
| WAV | .wav |
iOS中的native format是CAF file format。
在iOS中,app在運(yùn)行過程中有可能接到電話,如果此時(shí)正在播放sound,系統(tǒng)會(huì)做一定的處理。
AudioSession就是在這種情況的中間人,每個(gè)app都會(huì)有一個(gè)audio session。在播放或者錄音時(shí)候需要session在做正確的事情,需要我們自己弄清楚以下的情況:
app收到系統(tǒng)的中斷時(shí)候應(yīng)該如何響應(yīng),比如收到phone call?你是否需要app的sound和其他后臺(tái)運(yùn)行的app的sounds混合播放,或者需要獨(dú)占播放?你需要app如何響應(yīng)遠(yuǎn)音頻路徑的響應(yīng),比如拔插耳機(jī)時(shí)候AudioSession提供了三種類型的接口:
Categories: A category is a key that identifies a set of audio behaviors for your application. By setting a category, you indicate your audio intentions to iOS, such as whether your audio should continue when the screen locks.Interruptions and route changes :Your audio session posts notifications when your audio is interrupted, when an interruption ends, and when the hardware audio route changes. These notifications let you respond to changes in the larger audio environment—such as an interruption due to in an incoming phone call—gracefully.Hardware characteristics:You can query the audio session to discover characteristics of the device your application is running on, such as hardware sample rate, number of hardware channels, and whether audio input is available.Audio Session擁有一些默認(rèn)的行為策略:
當(dāng)用戶將靜音開關(guān)靜音時(shí),audio就會(huì)靜音。當(dāng)用戶鎖屏(手動(dòng),自動(dòng))時(shí)候,audio就會(huì)靜音。當(dāng)你app的audio啟動(dòng)時(shí),其他app正在使用的audio就會(huì)靜音。audio session的這個(gè)特定的默認(rèn)的行為策略被稱為kAudioSessionCategory_SoloAmbientSound。同時(shí),它還包括其他的多種策略選擇。
默認(rèn)的audio session的一個(gè)典型的特征是,audio會(huì)在中斷以后自動(dòng)恢復(fù)活動(dòng)。Audio session有兩個(gè)重要的狀態(tài):active和inactive。只有當(dāng)Audio session處于active狀態(tài)時(shí)候,app才能使用audio。
在app啟動(dòng)以后,你的默認(rèn)的audio session就會(huì)是active狀態(tài)。然而,如果一個(gè)電話被打進(jìn)來,你的session就會(huì)立刻被置為inactive,然后app中的audio就會(huì)停止。這個(gè)電話就被稱為一個(gè)中斷,如果用戶選擇忽略電話,app就會(huì)繼續(xù)運(yùn)行。但是此時(shí)你的audio session依然會(huì)是inactive狀態(tài),audio也就不會(huì)工作。
如果你使用 Audio Queue Services操作audio,我們就需要給中斷注冊(cè)listener回調(diào)函數(shù),手動(dòng)去重啟audio session。具體內(nèi)容可以見Audio Session Programming Guide。
一個(gè)錄音的app只有在設(shè)備的音頻硬件可用的時(shí)候才能錄音。為了檢查這個(gè)屬性,需要使用audio session的kAudioSessionProperty_AudioInputAvailable屬性。
123456789 | UInt32 audioInputIsAvailable;UInt32 propertySize = sizeof (audioInputIsAvailable); AudioSessionGetProperty ( kAudioSessionProperty_AudioInputAvailable, &propertySize, &audioInputIsAvailable // A nonzero value on output means that // audio input is available); |
你的app同時(shí)只能有一個(gè)audio session策略,你的所有的audio都需要遵循這個(gè)active策略的特點(diǎn)。如何響應(yīng)中斷,在Audio Session Programming Guide`中有更加詳細(xì)的內(nèi)容。
如果要測(cè)試Audio session,需要使用真機(jī)
AVAudioPlayer提供了簡(jiǎn)單的OC接口用于audio播放。如果非網(wǎng)絡(luò)stream,或者需要精確控制,apple推薦使用這個(gè)類,它可以用于播放iOS支持的任何audio format,同時(shí)這個(gè)類并不需要去設(shè)置audio session,因?yàn)樗鼤?huì)在中斷發(fā)生以后自動(dòng)恢復(fù)播放,除非你需要指定特地的行為。
它可以完成以下工作:
Play sounds of any durationPlay sounds from files or memory buffersLoop soundsPlay multiple sounds simultaneouslyControl relative playback level for each sound you are playingSeek to a particular point in a sound file, which supports such application features as fast forward and rewindObtain data that you can use for audio level metering下面就是使用AVAudioPlayer的具體流程:
12345678910111213141516 | NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"sound" ofType: @"wav"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];[fileURL release]; self.player = newPlayer;[newPlayer release]; [self.player prepareToPlay];[self.player setDelegate: self]; |
你的delegate對(duì)象用于處理interruptions或者音頻播放停止以后的操作。
123456 | - (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag { if (flag == YES) { [self.button setTitle: @"Play" forState: UIControlStateNormal]; }} |
123456789101112131415 | - (IBAction) playOrPause: (id) sender { // if already playing, then pause if (self.player.playing) { [self.button setTitle: @"Play" forState: UIControlStateHighlighted]; [self.button setTitle: @"Play" forState: UIControlStateNormal]; [self.player pause]; // if stopped or paused, start playing } else { [self.button setTitle: @"Pause" forState: UIControlStateHighlighted]; [self.button setTitle: @"Pause" forState: UIControlStateNormal]; [self.player play]; }} |
Audio Queue Services是一個(gè)更加直觀的record和play audio的方式。同時(shí)它還有更多個(gè)高級(jí)功能,可以使用這個(gè)服務(wù)完成更多的工作,比如對(duì)LPCM數(shù)據(jù)進(jìn)行壓縮等等。它和AVAudioPlayer是iOS中唯二可以播放壓縮后音頻格式的接口。使用Audio Queue Service播放和錄音都是通過回調(diào)方法完成的。
為了創(chuàng)建Audio Queue對(duì)象,它分成兩類:
AudioQueueNewInput用于錄音AudioQueueNewOutput用于播放使用audio queue object播放audio file,需要一些幾個(gè)步驟:
創(chuàng)建數(shù)據(jù)結(jié)構(gòu)用于管理audio queue需要的信息,例如audio format,audio fileID等。定義callback函數(shù),用于管理audio queue buffers。callback會(huì)使用Audio File Service去讀取audio file用來播放使用AudioQueueNewOutput用來播放audio file。Creating an audio queue object具體的代碼如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | static const int kNumberBuffers = 3;// Create a data structure to manage information needed by the audio queuestruct myAQStruct { AudioFileID mAudioFile; CAStreamBasicDescription mDataFormat; AudioQueueRef mQueue; AudioQueueBufferRef mBuffers[kNumberBuffers]; SInt64 mCurrentPacket; UInt32 mNumPacketsToRead; AudioStreamPacketDescription *mPacketDescs; bool mDone;};// Define a playback audio queue callback functionstatic void AQTestBufferCallback( void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inCompleteAQBuffer) { myAQStruct *myInfo = (myAQStruct *)inUserData; if (myInfo->mDone) return; UInt32 numBytes; UInt32 nPackets = myInfo->mNumPacketsToRead; AudioFileReadPackets ( myInfo->mAudioFile, false, &numBytes, myInfo->mPacketDescs, myInfo->mCurrentPacket, &nPackets, inCompleteAQBuffer->mAudioData ); if (nPackets > 0) { inCompleteAQBuffer->mAudioDataByteSize = numBytes; AudioQueueEnqueueBuffer ( inAQ, inCompleteAQBuffer, (myInfo->mPacketDescs ? nPackets : 0), myInfo->mPacketDescs ); myInfo->mCurrentPacket += nPackets; } else { AudioQueueStop ( myInfo->mQueue, false ); myInfo->mDone = true; }}// Instantiate an audio queue objectAudioQueueNewOutput ( &myInfo.mDataFormat, AQTestBufferCallback, &myInfo, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &myInfo.mQueue); |
Audio Queue對(duì)象提供了兩個(gè)方式控制音頻level。第一種是直接使用AudioQueueSetParameter以及kAudioQueueParam_Volume參數(shù),就可以設(shè)置,設(shè)置完成以后會(huì)立即生效。
123456 | Float32 volume = 1;AudioQueueSetParameter ( myAQstruct.audioQueueObject, kAudioQueueParam_Volume, volume); |
也可以通過AudioQueueEnqueueBufferWithParameters給audio queue buffer設(shè)置。這種方式只有在audio queue buffer 開始播放時(shí)候才起作用。
你也可以直接查詢audio queue的kAudioQueueProperty_CurrentLevelMeterDB屬性,得到的值是一組AudioQueueLevelMeterState結(jié)構(gòu)體(一個(gè)channel一個(gè)數(shù)組),具體的結(jié)構(gòu)體是AudioQueueLevelMeterState,顯示如下:
1234 | typedef struct AudioQueueLevelMeterState { Float32 mAveragePower; Float32 mPeakPower;}; AudioQueueLevelMeterState; |
如果你需要播放的音頻時(shí)間少于30s,那么可以使用System Sound Services。調(diào)用AudioServicesPlaySystemSound函數(shù)可以立即播放一個(gè)sound file。你也可以調(diào)用AudioServicesPlayAlertSound播放alert聲音。這兩個(gè)方法都會(huì)在手機(jī)靜音的情況下振動(dòng)。
當(dāng)然,你也在調(diào)用AudioServicesPlaySystemSound方法使用kSystemSoundID_Vibrate屬性,顯示的觸發(fā)振動(dòng)。
為了使用AudioServicesPlaySystemSound方法播放sound,首先需要將sound file注冊(cè)到系統(tǒng)中,得到一個(gè)sound ID,然后才能播放。
下面一段代碼顯示了使用System Sound Services去play sound:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | #include <AudioToolbox/AudioToolbox.h>#include <CoreFoundation/CoreFoundation.h> // Define a callback to be called when the sound is finished// playing. Useful when you need to free memory after playing.static void MyCompletionCallback ( SystemSoundID mySSID, void * myURLRef) { AudioServicesDisposeSystemSoundID (mySSID); CFRelease (myURLRef); CFRunLoopStop (CFRunLoopGetCurrent());} int main (int argc, const char * argv[]) { // Set up the pieces needed to play a sound. SystemSoundID mySSID; CFURLRef myURLRef; myURLRef = CFURLCreateWithFileSystemPath ( kCFAllocatorDefault, CFSTR ("../../ComedyHorns.aif"), kCFURLPOSIXPathStyle, FALSE ); // create a system sound ID to represent the sound file OSStatus error = AudioServicesCreateSystemSoundID (myURLRef, &mySSID); // Register the sound completion callback. // Again, useful when you need to free memory after playing. AudioServicesAddSystemSoundCompletion ( mySSID, NULL, NULL, MyCompletionCallback, (void *) myURLRef ); // Play the sound file. AudioServicesPlaySystemSound (mySSID); // Invoke a run loop on the current thread to keep the application // running long enough for the sound to play; the sound completion // callback later stops this run loop. CFRunLoopRun (); return 0;} |
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注