[A cainiao COCOS-2D programming path] COCOS2D sound-SimpleAudioEngine and CDAudioMa

Source: Internet
Author: User

There are three main sound interfaces:

SimpleAudioEngine, CDAudioManager, and CDSoundEngine

1. SimpleAudioEngine

@ Interface SimpleAudioEngine: NSObject
 
  
{BOOLmute _; BOOLenabled _;}/** Background music volume. range is 0.0f to 1.0f. This will only have an effect if willPlayBackgroundMusic returns YES */@ property (readwrite) float backgroundMusicVolume; // background music audio/** Effects volume. range is 0.0f to 1.0f */@ property (readwrite) float audio tsvolume; // audio sound settings/** If NO it indicates background music will not be played either because no background music is Loaded or the audio session does not permit it. */@ property (readonly) BOOL willPlayBackgroundMusic; // whether to play background music. If it is preloaded, YES is used.
 
/** Returns the shared instance of the SimpleAudioEngine object */+ (SimpleAudioEngine *) sharedEngine; // Singleton class method. When using the instance method, use the // [SimpleAudioEngine sharedEngine] instance method.
 
// Pre-load the background music/** Preloads a music file so it will be ready to play as background music */-(void) preloadBackgroundMusic :( NSString *) filePath;
 
// Use the path to load the background music/** plays background music in a loop */-(void) playBackgroundMusic :( NSString *) filePath;/** plays background music, if loop is true the music will repeat otherwise it will be played once */
-(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop;
 
// Stop/pause playing background music/** stops playing background music */-(void) stopBackgroundMusic;/** pauses the background music */-(void) pauseBackgroundMusic;
 
// Continue playing background music/** resume background music that has been paused */-(void) resumeBackgroundMusic;
 
// Replay the background music/** rewind the background music */-(void) rewindBackgroundMusic;
 
// Whether the background music is being played/** returns whether or not the background music is playing */-(BOOL) isBackgroundMusicPlaying;
 
// Set the sound effect. It is similar to the setting of background music, but it has three more parameters: pitch (tune) pan: sound source gain volume/** plays an audio effect with a file path */-(ALuint) playEffect :( NSString *) filePath;/** stop a sound that is playing, note you must pass in the soundId that is returned when you started playing the sound with playEffect */-(void) stopEffect :( ALuint) soundId; /** plays an audio effect with a file path, pitch, pan and gain */-(ALuint) playEffect :( NSString *) filePath pitch :( Float32) pitch pan :( Float32) pan gain :( Float32) gain;/** preloads an audio effect */-(void) preloadEffect :( NSString *) filePath;
 
// Uninstall this audio effect/** unloads an audio effect from memory */-(void) unloadEffect :( NSString *) filePath; /** Gets a CDSoundSource object set up to play the specified file. */-(CDSoundSource *) soundSourceForFile :( NSString *) filePath;
// End sound effect/** Shuts down the shared audio engine instance so that it can be reinitialised */+ (void) end; @ end

2. CDAudioManager is a more complex API than SimpleAudioEngine.

@ Interface CDAudioManager: NSObject
 
  
{CDSoundEngine * soundEngine; CDLongAudioSource * backgroundMusic; NSMutableArray * category; NSString * _ audioSessionCategory; category; region; SEL category; id category; BOOL willPlayBackgroundMusic; BOOL _ mute; // mute BOOL _ resigned; BOOL _ interrupted; // whether the BOOL _ audioSessionActive is interrupted; BOOL enabled _; // whether the BOOL _ audioSessionActive is ended // For handling resign/become activeBOOL _ isobservingappeven; response _ resignBehavior;} @ property (readonly) CDSoundEngine * soundEngine; @ property (readonly) CDLongAudioSource * backgroundMusic; @ property (readonly) BOOL willPlayBackgroundMusic; /** Returns the shared singleton */+ (CDAudioManager *) sharedManager; + (tAudioManagerState) sharedManagerState;/** Configures the shared singleton with a mode */+ (void) configure: (tAudioManagerMode) mode;/** Initializes the engine asynchronously with a mode */+ (void) initAsynchronously: (tAudioManagerMode) mode;/** Initializes the engine synchronously with a mode, channel definition and a total number of channels */-(id) init: (tAudioManagerMode) mode;-(void) audioSessionInterrupted;-(void) audioSessionResumed;-(void) setResignBehavior :( tAudioManagerResignBehavior) resignBehavior autoHandle :( BOOL) autoHandle;/** Returns true is audio is muted at a hardware level e. g user has ringer switch set to off */-(BOOL) isDeviceMuted; // interrupted/** Returns true if another app is playing audio such as the iPod music player */-(BOOL) isOtherAudioPlaying;
 
/** Sets the way the audio manager interacts with the operating system such as whether it shares output with other apps or obeys the mute switch */-(void) setMode:(tAudioManagerMode) mode;/** Shuts down the shared audio manager instance so that it can be reinitialised */+(void) end;/** Call if you want to use built in resign behavior but need to do some additional audio processing on resign active. */- (void) applicationWillResignActive;/** Call if you want to use built in resign behavior but need to do some additional audio processing on become active. */- (void) applicationDidBecomeActive;
//New AVAudioPlayer API/** Loads the data from the specified file path to the channel's audio source */-(CDLongAudioSource*) audioSourceLoad:(NSString*) filePath channel:(tAudioSourceChannel) channel;/** Retrieves the audio source for the specified channel */-(CDLongAudioSource*) audioSourceForChannel:(tAudioSourceChannel) channel;
// The same part as simpleAudioEngine // Legacy AVAudioPlayer API/** Plays music in background. the music can be looped or not It is recommended to use. aac files as background music since they are decoded by the device (hardware ). */-(void) playBackgroundMusic :( NSString *) filePath loop :( BOOL) loop;/** Preloads a background music */-(void) preloadBackgroundMusic :( NSString *) filePath; /** Stops playing the background music */-(void) stopBackgroundMusic;/** Pauses the background music */-(void) pauseBackgroundMusic; /** Rewinds the background music */-(void) rewindBackgroundMusic;/** Resumes playing the background music */-(void) resumeBackgroundMusic; /** Returns whether or not the background music is playing */-(BOOL) isBackgroundMusicPlaying;-(void) setBackgroundMusicCompletionListener :( id) listener selector :( SEL) selector; @ end/** Fader for long audio source objects */@ interface CDLongAudioSourceFader: CDPropertyModifier {}@ endstatic const int kCDNoBuffer =-1; /** Allows buffers to be associated with file names */@ interface CDBufferManager: NSObject {response * loadedBuffers; NSMutableArray * freedBuffers; optional * soundEngine; int nextBufferId;}-(id) initWithEngine :( CDSoundEngine *) theSoundEngine;-(int) bufferForFile :( NSString *) filePath create :( BOOL) create;-(void) releaseBufferForFile :( NSString *) filePath; @ end


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.