Cocos2d 播放音乐

原文参考 http://www.pawapps.com/2011/10/24/tutorial-cdaudiomanager-and-cdsoundengine/

做了下封装

GameSound.h

 1 #import <Foundation/Foundation.h>
 2 #import "CDAudioManager.h"
 3 
 4 // This is a shared header
 5 // Don't mind too much what is in this file yet, it'll be explained below.
 6 /** CDAudioManager supports two long audio source channels called left and right
 7  typedef enum {
 8  kASC_Left = 0,
 9  kASC_Right = 1
10  } tAudioSourceChannel;    */
11 #define CGROUP_BG       kASC_Left    // Channel for background music
12 #define CGROUP_EFFECTS  kASC_Right   // Channel for sound effects
13 #define SND_BG_LOOP 1     // Identifier for background music audio
14 #define SND_CLICK   2     // Identifier for click sound effect
15 // Helper macro for playing sound effects
16 #define playEffect(__ID__)      [[CDAudioManager sharedManager].soundEngine playSound:__ID__ sourceGroupId:CGROUP_EFFECTS pitch:1.0f pan:0.0f gain:1.0f loop:NO]
17 
18 
19 @interface GameSound : NSObject
20 {
21     
22 }
23 +(GameSound *)sharedData;
24 
25 -(void) initData;
26 -(CDAudioManager*) sharedManager;
27 
28 -(void) playBackgroundMusic;
29 -(void) pauseBackgroundMusic;
30 -(void) stopBackgroundMusic;
31 
32 -(void) playEffect:(int)Identifier;
33 @end

GameSound.m

  1 #import "GameSound.h"
  2 
  3 #import "CDAudioManager.h"
  4 #import "CocosDenshion.h"
  5 
  6 
  7 static GameSound *instance = nil;
  8 
  9 @implementation GameSound
 10 
 11 -(void) initData
 12 {
 13     /**/
 14     // 初始化CDSoundEngine对象
 15     /**/
 16     CDSoundEngine *sse = [CDAudioManager sharedManager].soundEngine;
 17     
 18     // 定义一个音源组的数组。一个音源组代表一个声道。
 19     //设置2个声道:一个允许播放一种声音;另一个允许播放31种声音。(最多同时支持32种)。
 20     NSArray *sourceGroups = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:31], nil];
 21     [sse defineSourceGroups:sourceGroups];
 22     
 23     //Initialise audio manager asynchronously as it can take a few seconds
 24     /** Different modes of the engine
 25      typedef enum {
 26      kAMM_FxOnly,                    //!Other apps will be able to play audio
 27      kAMM_FxPlusMusic,                //!Only this app will play audio
 28      kAMM_FxPlusMusicIfNoOtherAudio,    //!If another app is playing audio at start up then allow it to continue and don't play music
 29      kAMM_MediaPlayback,                //!This app takes over audio e.g music player app
 30      kAMM_PlayAndRecord                //!App takes over audio and has input and output
 31      } tAudioManagerMode;*/
 32     // 使用异步方式初始化CDAudioManager对象,选用特定的方式管理音效
 33     [CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];
 34     
 35     // 将所有音效存储数组中,并使用CDSoundEngine的loadBuffersAsynchronously方法,载入到缓存中。以备用。
 36     NSMutableArray *loadRequests = [[[NSMutableArray alloc] init] autorelease];
 37     
 38     [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_BG_LOOP filePath:@"backgroundLoop.m4a"] autorelease]];
 39 
 40     [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_CLICK filePath:@"Start.caf"] autorelease]];
 41     
 42     //[loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_CANNON filePath:@"cannon.mp3"] autorelease]];
 43     
 44     //[loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_BLAST filePath:@"explosion.mp3"] autorelease]];
 45     
 46     [sse loadBuffersAsynchronously:loadRequests];
 47     
 48     //1.播放背景音效(简单)
 49     //[[CDAudioManager sharedManager] playBackgroundMusic:@"backgroundLoop.m4a" loop:YES];
 50     //[[CDAudioManager sharedManager] pauseBackgroundMusic];
 51     //[[CDAudioManager sharedManager] stopBackgroundMusic];
 52     
 53     //2.播放背景音效(高级)
 54     
 55      //ALuint bg = [[CDAudioManager sharedManager].soundEngine playSound:SND_BG_LOOP sourceGroupId:CGROUP_BG pitch:1.0f pan:0.0f gain:1.0f loop:YES];//播放背景
 56      //[[CDAudioManager sharedManager].soundEngine stopSound:bg];//停止背景音乐
 57     /**/
 58     //end 播放声音
 59     /**/
 60 }
 61 -(CDAudioManager*) sharedManager
 62 {
 63     return [CDAudioManager sharedManager] ;
 64 }
 65 -(void) playBackgroundMusic
 66 {
 67     [[CDAudioManager sharedManager] playBackgroundMusic:@"backgroundLoop.m4a" loop:YES];
 68 }
 69 -(void) pauseBackgroundMusic
 70 {
 71     [[CDAudioManager sharedManager] pauseBackgroundMusic];
 72 }
 73 -(void) stopBackgroundMusic
 74 {
 75     [[CDAudioManager sharedManager] stopBackgroundMusic];
 76 }
 77 -(void) playEffect:(int)Identifier
 78 {
 79     playEffect(SND_CLICK);
 80 }
 81 
 82 /***************************************************************************************************************************/
 83 /* 实现单例 */
 84 +(GameSound *)sharedData{
 85     @synchronized(self){  //为了确保多线程情况下,仍然确保实体的唯一性
 86         if (!instance) {
 87             [[self alloc] init]; //该方法会调用 allocWithZone
 88         }
 89     }
 90     return instance;
 91 }
 92 +(id)allocWithZone:(NSZone *)zone{
 93     @synchronized(self){
 94         if (!instance) {
 95             instance = [super allocWithZone:zone]; //确保使用同一块内存地址
 96             return instance;
 97         }
 98     }
 99     return nil;
100 }
101 - (id)copyWithZone:(NSZone *)zone;{
102     return self; //确保copy对象也是唯一
103 }
104 -(id)retain{
105     return self; //确保计数唯一
106 }
107 - (unsigned)retainCount
108 {
109     return UINT_MAX;  //装逼用的,这样打印出来的计数永远为-1
110 }
111 - (id)autorelease
112 {
113     return self;//确保计数唯一
114 }
115 - (oneway void)release
116 {
117     //重写计数释放方法
118 }
119 /* end 实现单例 */
120 @end

依赖

转载于:https://www.cnblogs.com/thc7/p/3294895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值