@Steve Oldmeadow,
I have put AVPlayer and PASoundMgr together, it work very well. Thank you very much!
I should mention here, if somebody hope to use PASoundMgr or PASoundSource, he should copy folder"sound-engine" &"Tremor" to the project himself, it is not in the project template of 0.8.1-beta. and the "ivorbisfile_example.c" should be removed.
Here is the Codes base on 0.8.1-beta template, for the Thanks to the kindness from Steve Oldmeadow.
//HelloWorldScene.h
// When you import this file, you import all the cocos2d classes
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <AVFoundation/AVAudioPlayer.h>
#import "PASoundMgr.h"
#import "PASoundSource.h"
// HelloWorld Layer
@interface HelloWorld : Layer <AVAudioPlayerDelegate>
{
AVAudioPlayer *player;
PASoundSource *source1;
Sprite *source1Sprite;
}
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
@end
---------------------------------------
#import "HelloWorldScene.h"
// HelloWorld implementation
@implementation HelloWorld
+(id) scene
{
// 'scene' is an autorelease object.
Scene *scene = [Scene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {
// create and initialize a Label
Label* label = [Label labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[Director sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
NSString *mySoundClip = [[NSBundle mainBundle] pathForResource:@"BackGroundShort" ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mySoundClip] error:NULL];
[player setDelegate:self];
[player prepareToPlay];
[player play];
player.numberOfLoops=-1;
[PASoundMgr sharedSoundManager];
source1 = [[PASoundSource alloc] initWithFile:@"cowbell" looped:YES];
[[[PASoundMgr sharedSoundManager] listener] setPosition:ccp(240,160)];
source1Sprite = [Sprite spriteWithFile:@"Gift.png"];
[self addChild:source1Sprite z:0];
source1Sprite.position = ccp(10,60);
[source1 setGain:0.5f];
[source1 setRolloff:0.3f];
//[source1 setPitch:0.1f];
id move = [MoveBy actionWithDuration:4 position:ccp(460,0)];
id sequence = [Sequence actions:move,[move reverse],nil];
[source1Sprite runAction:[RepeatForever actionWithAction:sequence]];
[source1 playAtPosition:ccp(240,160)];
[self schedule:@selector(updateSoundPosition:)];
}
return self;
}
-(void)updateSoundPosition: (id)theSender
{
[[[PASoundMgr sharedSoundManager] listener] setPosition:source1Sprite.position];
//[source1 playAtPosition:source1Sprite.position];
}
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)aPlayer{
//[aPlayer pause];
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)aPlayer{
[aPlayer play];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end