search
HomeDatabaseMysql TutorialCocos2d-x 3.0心得(04)

用cocos2dx播放音乐音效算是很简单的事了,没什么特殊需求的话,用SimpleAudioEngine就可以搞定。不过特殊需求总是会出现的。要是你经常听歌,应该会有印象,某些播放器在切换音乐的时候,正在播放的音乐会逐渐淡出直到消失,然后才会播放新的音乐。如果想在

用cocos2dx播放音乐音效算是很简单的事了,没什么特殊需求的话,用SimpleAudioEngine就可以搞定。不过特殊需求总是会出现的。要是你经常听歌,应该会有印象,某些播放器在切换音乐的时候,正在播放的音乐会逐渐淡出直到消失,然后才会播放新的音乐。如果想在游戏里也实现类似的效果,光靠SimpleAudioEngine是不够的。

cocos2dx里面的音频管理分3个层次,从高到低是SimpleAudioEngine、CDAudioManager、CDSoundEngine。当然这是官方的说法。通常一旦你用到了CDAudioManager,那么你基本上也得使用CDSoundEngine,因为CDAudioManager主管BGM,CDSoundEngine主管音效。另外比较遗憾的是,CDAudioManager和CDSoundEngine只能在iOS上使用。当然你可以写两套代码,在iOS上实现炫酷的音频效果,其他平台就只能soso了。


CDAudioManager是用来管理BGM的,这从它所包装的音频对象类名CDLongAudioSource就可以看出来,“长音频源”,没什么比BGM更合适了。而CDLongAudioSource实际是对AVAudioPlayer的包装,AVAudioPlayer是一个不折不扣的iOS原生类,所以你明白为什么CDAudioManager只能在iOS上用了。

CDLongAudioSource本身还算通俗易懂,加载、播放、暂停、循环、控制音量,该有的接口一应俱全。

CDAudioManager包含了一个CDLongAudioSource数组,这个数组在内部设定了固定长度为2。这意味着如果使用CDAudioManager,我们可以同时播放两首BGM。。。嗯,这不是说我们真的要同时放两首音乐,这是用来做其他效果的。比如说,当你准备换音乐的时候,让第一首淡出,同时第二首淡入,这一小段时间里,两首音乐是同时存在的,听起来的效果就是从第一首无缝切换到第二首。这不是很cool么Cocos2d-x 3.0心得(04)

CDAudioManager在内部把CDLongAudioSource数组里的第一个称为“左声道(kASC_Left)”,第二个称为“右声道(kASC_Right)”。但它其实跟你耳机那两根线没什么关系,都能出声。当然也可能是我没找到在什么地方设置,不过对于一个手机游戏我们还是不要太苛刻了。

你可以通过下面两个方法来加载和获取音乐对象:

/** 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;
至于播放控制,那就是CDLongAudioSource自己的事了。

CDAudioManager里有一些被声明废弃的接口,看得出来,早期版本的CDAudioManager只能播放一首BGM。。。嘛,时代总是会进步的。

CDAudioManager除了控制BGM,还能用来做一些跟BGM相关的跨App的事。你可以给CDAudioManager设置一个模式

-(void) setMode:(tAudioManagerMode) mode;

它能做的事情包括:

只让你的App能放BGM(kAMM_FxPlusMusic);

让你的App到后台还能放BGM(kAMM_MediaPlayback);

当别的播放器在放BGM时让你的App静音(kAMM_FxPlusMusicIfNoOtherAudio),等等。

你还可以通过

-(void) setResignBehavior:(tAudioManagerResignBehavior) resignBehavior autoHandle:(BOOL) autoHandle;
来设置当App被中断的时候(比如说,来电话了),怎么处理BGM:

先停止,回游戏后继续放(kAMRBStopPlay);
先停止,回游戏后不放了(kAMRBStop),等等。


说了这么多,似乎还没讲到怎么做BGM的混合、淡入淡出。这其实没什么奥秘,你要做的只是控制CDLongAudioSource的音量。从1逐渐减至0,就是淡出;从0逐渐增至1,就是淡入。cocos2dx提供了一个CDLongAudioSourceFader,可以帮助你比较规范的控制这些音量。它继承自CDPropertyModifier,看名字就知道这是用来动态控制音频属性的。它的作用原理跟cocos2dx的Action系列很类似,所以你应该很快就能熟悉它。只不过,这里没有ActionManager一类的管理器Cocos2d-x 3.0心得(04)所以你得自己弄一个了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool