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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This quick guide will show you how to use AirPlay, Apple's innovative wireless streaming feature to easily share and display content. Allows you to use Wi-Fi to connect your iPhone or iPad to AirPlay-compatible Apple TVs, smart TVs, speakers, and Macs, as well as certain third-party speakers and smart TVs, for seamless data transfer. AirPlay operates on a peer-to-peer basis, connecting your Apple devices through the local network. It uses a combination of technologies, including Real-Time Transport Protocol (RTP) for streaming audio and video and Bonjour for device discovery. Simply put, when you use AirPlay, your device sends a stream of data and then

How to use Layui to develop a multimedia page that supports audio and video playback. With the development of the modern Internet, media content has become an indispensable part of people's daily lives. In order to provide a better user experience, we often need to embed audio and video players on web pages. This article will introduce how to use Layui to develop a multimedia page that supports audio and video playback, and provide detailed code examples. Step 1: Introduce Layui and the necessary CSS and JavaScript files to start using Lay

How to play a video file with subtitles using PHP? With the development of Internet technology, video has become one of the most popular forms of online content. Now, many video files are equipped with subtitles, which makes it easier for users to understand and digest video content. So, how to use PHP to play video files with subtitles on a web page? This article will introduce it to you in detail. First, we need to prepare the following environment: a server with a PHP running environment (such as Apache or Nginx); a video file (support

Using uniapp to implement the audio playback function With the development of the mobile Internet, the audio playback function has become one of the essential functions for many applications. The audio playback function can be easily implemented using uniapp, and it has cross-platform characteristics and can run on different mobile terminals. Before developing uniapp, we need to prepare audio resource files. In this article, we will use an audio file named "music.mp3" as an example. First, we need to create the uniapp project

How does iQiyi play local videos? In the iQiyi mobile app, users can not only play film and television resources on various platforms for viewing, but also use this app to play their own local videos! The easiest way to watch local videos Naturally, using the photo album is simple and straightforward, but playing videos in the photo album is not that easy to use. It lacks many functions and basically only has some basic functions. Although it is not unusable, the user experience is completely incomparable with these platforms! Let’s Let’s take a look at how to use this app to watch local videos! iQiyi tutorial for playing local videos 1. Enter my page and click Download. 2. Click the small dot in the upper right corner. 3. Click on the local video. 4. Click to watch local video. 5.

How to use PHP to implement a simple online video upload and playback system. With the development of the Internet, video content has gradually become one of the important ways for people to obtain information and entertainment. In order to meet users' needs for video upload and playback, we can use the PHP programming language to implement a simple online video upload and playback system. The following will introduce how to use PHP to implement this system, including database creation, file upload and video playback functions. Create a database First, we need to create a MySQL database to store the user

PHP learning experience: How to handle errors When developing PHP applications, handling errors is a very important aspect. Good error handling can improve the stability and reliability of the code, and can also better help us debug the code and solve problems. This article will introduce some common error types and how to handle them, with corresponding code examples. Syntax Errors Syntax errors are the most common and easiest to find errors in the coding process. It usually causes the PHP parser to not understand the code correctly, resulting in

In iOS 17.1, which is currently in beta, Apple has added the ability for users to customize playlist artwork in Apple Music using a series of preset options. Read on to find out how it's done. Previously in iOS, when creating a custom playlist, you had to use a basic tile image consisting of the track's album artwork, or upload a custom image. Given that your custom graphics or related photos aren't always readily available for playlists, and the default tiled illustrations are relatively simple, playlists can be difficult to identify and look rather generic. Thankfully, in iOS 17.1 Apple now offers eight different illustration styles for you to choose from, featuring geometric shapes and gradients, with playlist titles displayed in
