First, Introduction
In simple terms, audio can be divided into 2 types
1. Sound effects
- Also known as "short audio", usually plays in the program for 1 ~ 2 s
- Enhance the overall user experience with embellishment in the app
2. Music
- For example, the "background music" in the game, the general playing time is longer
Play audio can use frame: avfoundation.framework
Second, the sound effects of playback
//1.获得音效文件的路径NSURL *url = [[NSBundle mainBundle] URLForResource:@"aaa.wav" withExtension:nil];//2.加载音效文件,得到对应的音效 ID0;AudioServicesCreatessSystemSoundID((__brage CFURLRef)(url)&soundID);//3.播放音效AudioServicesPlaySystemSound(soundID);
Note: The sound file only needs to be loaded once, so you can use lazy loading.
- Summary of common functions for sound playback
1. Loading sound files
inFileURL, SystemSoundID *outSystemSoundID)
2. Releasing Sound resources
inSystemSoundID)
3. Play sound
inSystemSoundID)
4. Play sound with a little vibration
inSystemSoundID)
Third, the format of sound effects
Note: The hardware decoder can decode only one audio file at a time. In practice, the use of non-compressed audio format (AIFF) or CAF audio format, which reduces the system in the audio decoding of the consumption, to achieve the purpose of power saving.
Four, audio conversion tools
-f-d I8 filename
-f-d-b32000 filename
.-name‘*.mp3‘-exec-f-d-b32000\\;
V. Playback of Music
Music plays with a AVAudioPlayer class called
AVAudioPlayerCommon methods are as follows:
//load Music file -(id ) Initwithcontentsofurl: (nsurl *) URL error: (nserror * *) outerror;-(id ) Initwithdata: (NSData *) Data error: (nserror * *) Outerror; //ready to play -(bool ) Preparetoplay;//Play (asynchronous play) -(bool ) play; //pause -(void ) pause; //stop -(void ) stop; //update measured value -(void ) Updatemeters;//get the current average volume -(float ) Averagepowerforchannel: (Nsuinteger) channelnumber;
- The relevant properties are as follows:
//Is playing@property(ReadOnly, getter = isplaying)BOOLPlaying//Time length@property(ReadOnly)NstimeintervalDuration//Current playback position@property NstimeintervalCurrentTime;//Play Count@propertyNsinter Numberofloops;//Volume@property floatVolume//Whether to allow rate change@property BOOLEnablerate;//Playback rate (1 is the normal rate, 0.5 is the general rate, 2 is the double rate)@property floatRate//Number of channels@property(ReadOnly) Nsuinteger Numberofchannels//Channel@property floatPan//Whether to allow measurement of volume@property(getter = ismeteringenabled)BOOLmeteringenabled;
iOS development-Audio-related