iOS development-Audio-related

Source: Internet
Author: User

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
    • Convert to AIFF Format
-f-d I8 filename
    • Convert to CAF format
-f-d-b32000 filename
    • Batch Processing
.-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.