issue 33: iOS & Android support looping playing effect

This commit is contained in:
minggo 2011-07-26 15:52:41 +08:00
parent b308effdc5
commit 1bb9e8139a
17 changed files with 46 additions and 42 deletions

View File

@ -168,7 +168,7 @@ namespace CocosDenshion
s3eSoundSetInt(S3E_SOUND_VOLUME, volume); s3eSoundSetInt(S3E_SOUND_VOLUME, volume);
} }
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath) unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{ {

View File

@ -119,9 +119,9 @@ namespace CocosDenshion
setEffectsVolumeJNI(volume); setEffectsVolumeJNI(volume);
} }
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath) unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{ {
return playEffectJNI(pszFilePath); return playEffectJNI(pszFilePath, bLoop);
} }
void SimpleAudioEngine::stopEffect(unsigned int nSoundId) void SimpleAudioEngine::stopEffect(unsigned int nSoundId)

View File

@ -151,17 +151,17 @@ extern "C"
} }
} }
unsigned int playEffectJNI(const char* path) unsigned int playEffectJNI(const char* path, bool bLoop)
{ {
int ret = 0; int ret = 0;
// int playEffect(String) // int playEffect(String)
jmethodID playEffectMethodID = getMethodID("playEffect", "(Ljava/lang/String;)I"); jmethodID playEffectMethodID = getMethodID("playEffect", "(Ljava/lang/String;Z)I");
if (playEffectMethodID) if (playEffectMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring StringArg = env->NewStringUTF(path);
ret = env->CallStaticIntMethod(classOfCocos2dxActivity, playEffectMethodID, StringArg); ret = env->CallStaticIntMethod(classOfCocos2dxActivity, playEffectMethodID, StringArg, bLoop);
} }
return (unsigned int)ret; return (unsigned int)ret;

View File

@ -13,7 +13,7 @@ extern "C"
extern bool isBackgroundMusicPlayingJNI(); extern bool isBackgroundMusicPlayingJNI();
extern float getBackgroundMusicVolumeJNI(); extern float getBackgroundMusicVolumeJNI();
extern void setBackgroundMusicVolumeJNI(float volume); extern void setBackgroundMusicVolumeJNI(float volume);
extern unsigned int playEffectJNI(const char* path); extern unsigned int playEffectJNI(const char* path, bool bLoop);
extern void stopEffectJNI(unsigned int nSoundId); extern void stopEffectJNI(unsigned int nSoundId);
extern void endJNI(); extern void endJNI();
extern float getEffectsVolumeJNI(); extern float getEffectsVolumeJNI();

View File

@ -127,8 +127,9 @@ public:
/** /**
@brief Play sound effect @brief Play sound effect
@param pszFilePath The path of the effect file,or the FileName of T_SoundResInfo @param pszFilePath The path of the effect file,or the FileName of T_SoundResInfo
@bLoop Whether to loop the effect playing, default value is false
*/ */
unsigned int playEffect(const char* pszFilePath); unsigned int playEffect(const char* pszFilePath, bool bLoop = false);
/** /**
@brief Stop playing sound effect @brief Stop playing sound effect

View File

@ -90,9 +90,9 @@ static void static_setEffectsVolume(float volume)
[SimpleAudioEngine sharedEngine].effectsVolume = volume; [SimpleAudioEngine sharedEngine].effectsVolume = volume;
} }
static unsigned int static_playEffect(const char* pszFilePath) static unsigned int static_playEffect(const char* pszFilePath, bool bLoop)
{ {
return [[SimpleAudioEngine sharedEngine] playEffect:[NSString stringWithUTF8String: pszFilePath]]; return [[SimpleAudioEngine sharedEngine] playEffect:[NSString stringWithUTF8String: pszFilePath] loop:bLoop];
} }
static void static_stopEffect(int nSoundId) static void static_stopEffect(int nSoundId)
@ -210,9 +210,9 @@ namespace CocosDenshion
static_setEffectsVolume(volume); static_setEffectsVolume(volume);
} }
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath) unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{ {
return static_playEffect(pszFilePath); return static_playEffect(pszFilePath, bLoop);
} }
void SimpleAudioEngine::stopEffect(unsigned int nSoundId) void SimpleAudioEngine::stopEffect(unsigned int nSoundId)

View File

@ -72,11 +72,11 @@
-(BOOL) isBackgroundMusicPlaying; -(BOOL) isBackgroundMusicPlaying;
/** plays an audio effect with a file path*/ /** plays an audio effect with a file path*/
-(ALuint) playEffect:(NSString*) filePath; -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop;
/** stop a sound that is playing, note you must pass in the soundId that is returned when you started playing the sound with playEffect */ /** 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; -(void) stopEffect:(ALuint) soundId;
/** plays an audio effect with a file path, pitch, pan and gain */ /** 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; -(ALuint) playEffect:(NSString*) filePath loop:(BOOL)loop pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain;
/** preloads an audio effect */ /** preloads an audio effect */
-(void) preloadEffect:(NSString*) filePath; -(void) preloadEffect:(NSString*) filePath;
/** unloads an audio effect from memory */ /** unloads an audio effect from memory */

View File

@ -123,16 +123,16 @@ static CDBufferManager *bufferManager = nil;
#pragma mark SimpleAudioEngine - sound effects #pragma mark SimpleAudioEngine - sound effects
-(ALuint) playEffect:(NSString*) filePath -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop
{ {
return [self playEffect:filePath pitch:1.0f pan:0.0f gain:1.0f]; return [self playEffect:filePath loop:loop pitch:1.0f pan:0.0f gain:1.0f];
} }
-(ALuint) playEffect:(NSString*) filePath pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain
{ {
int soundId = [bufferManager bufferForFile:filePath create:YES]; int soundId = [bufferManager bufferForFile:filePath create:YES];
if (soundId != kCDNoBuffer) { if (soundId != kCDNoBuffer) {
return [soundEngine playSound:soundId sourceGroupId:0 pitch:pitch pan:pan gain:gain loop:false]; return [soundEngine playSound:soundId sourceGroupId:0 pitch:pitch pan:pan gain:gain loop:loop];
} else { } else {
return CD_MUTE; return CD_MUTE;
} }

View File

@ -114,7 +114,7 @@ bool SimpleAudioEngine::isBackgroundMusicPlaying()
// effect function // effect function
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath) unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{ {
unsigned int nRet = _Hash(pszFilePath); unsigned int nRet = _Hash(pszFilePath);

View File

@ -204,7 +204,7 @@ void SimpleAudioEngine::setEffectsVolume(float volume)
// for sound effects // for sound effects
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath) unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{ {
preloadEffect(pszFilePath); preloadEffect(pszFilePath);
int nRet = -1; int nRet = -1;

View File

@ -149,8 +149,8 @@ public class Cocos2dxActivity extends Activity{
backgroundMusicPlayer.setBackgroundVolume(volume); backgroundMusicPlayer.setBackgroundVolume(volume);
} }
public static int playEffect(String path){ public static int playEffect(String path, boolean isLoop){
return soundPlayer.playEffect(path); return soundPlayer.playEffect(path, isLoop);
} }
public static void stopEffect(int soundId){ public static void stopEffect(int soundId){

View File

@ -28,7 +28,6 @@ public class Cocos2dxSound {
private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5; private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5;
private static final float SOUND_RATE = 1.0f; private static final float SOUND_RATE = 1.0f;
private static final int SOUND_PRIORITY = 1; private static final int SOUND_PRIORITY = 1;
private static final int SOUND_LOOP_TIME = 0;
private static final int SOUND_QUALITY = 5; private static final int SOUND_QUALITY = 5;
private final int INVALID_SOUND_ID = -1; private final int INVALID_SOUND_ID = -1;
@ -75,15 +74,17 @@ public class Cocos2dxSound {
} }
} }
public int playEffect(String path){ public int playEffect(String path, boolean isLoop){
Integer soundId = this.mPathSoundIDMap.get(path); Integer soundId = this.mPathSoundIDMap.get(path);
if (soundId != null){ if (soundId != null){
// the sound is preloaded // the sound is preloaded, stop it first
this.mSoundPool.stop(soundId);
// play sound // play sound
int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume, int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume,
this.mRightVolume, SOUND_PRIORITY, SOUND_LOOP_TIME, SOUND_RATE); this.mRightVolume, SOUND_PRIORITY, isLoop ? -1 : 0, SOUND_RATE);
// record sound id and stream id map // record sound id and stream id map
this.mSoundIdStreamIdMap.put(soundId, streamId); this.mSoundIdStreamIdMap.put(soundId, streamId);
@ -106,7 +107,7 @@ public class Cocos2dxSound {
* Because the method is supported from 2.2, so I can't use * Because the method is supported from 2.2, so I can't use
* it here. * it here.
*/ */
playEffect(path); playEffect(path, isLoop);
} }
return soundId.intValue(); return soundId.intValue();

View File

@ -149,8 +149,8 @@ public class Cocos2dxActivity extends Activity{
backgroundMusicPlayer.setBackgroundVolume(volume); backgroundMusicPlayer.setBackgroundVolume(volume);
} }
public static int playEffect(String path){ public static int playEffect(String path, boolean isLoop){
return soundPlayer.playEffect(path); return soundPlayer.playEffect(path, isLoop);
} }
public static void stopEffect(int soundId){ public static void stopEffect(int soundId){

View File

@ -28,7 +28,6 @@ public class Cocos2dxSound {
private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5; private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5;
private static final float SOUND_RATE = 1.0f; private static final float SOUND_RATE = 1.0f;
private static final int SOUND_PRIORITY = 1; private static final int SOUND_PRIORITY = 1;
private static final int SOUND_LOOP_TIME = 0;
private static final int SOUND_QUALITY = 5; private static final int SOUND_QUALITY = 5;
private final int INVALID_SOUND_ID = -1; private final int INVALID_SOUND_ID = -1;
@ -75,15 +74,17 @@ public class Cocos2dxSound {
} }
} }
public int playEffect(String path){ public int playEffect(String path, boolean isLoop){
Integer soundId = this.mPathSoundIDMap.get(path); Integer soundId = this.mPathSoundIDMap.get(path);
if (soundId != null){ if (soundId != null){
// the sound is preloaded // the sound is preloaded, stop it first
this.mSoundPool.stop(soundId);
// play sound // play sound
int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume, int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume,
this.mRightVolume, SOUND_PRIORITY, SOUND_LOOP_TIME, SOUND_RATE); this.mRightVolume, SOUND_PRIORITY, isLoop ? -1 : 0, SOUND_RATE);
// record sound id and stream id map // record sound id and stream id map
this.mSoundIdStreamIdMap.put(soundId, streamId); this.mSoundIdStreamIdMap.put(soundId, streamId);
@ -106,7 +107,7 @@ public class Cocos2dxSound {
* Because the method is supported from 2.2, so I can't use * Because the method is supported from 2.2, so I can't use
* it here. * it here.
*/ */
playEffect(path); playEffect(path, isLoop);
} }
return soundId.intValue(); return soundId.intValue();

View File

@ -149,8 +149,8 @@ public class Cocos2dxActivity extends Activity{
backgroundMusicPlayer.setBackgroundVolume(volume); backgroundMusicPlayer.setBackgroundVolume(volume);
} }
public static int playEffect(String path){ public static int playEffect(String path, boolean isLoop){
return soundPlayer.playEffect(path); return soundPlayer.playEffect(path, isLoop);
} }
public static void stopEffect(int soundId){ public static void stopEffect(int soundId){

View File

@ -28,7 +28,6 @@ public class Cocos2dxSound {
private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5; private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5;
private static final float SOUND_RATE = 1.0f; private static final float SOUND_RATE = 1.0f;
private static final int SOUND_PRIORITY = 1; private static final int SOUND_PRIORITY = 1;
private static final int SOUND_LOOP_TIME = 0;
private static final int SOUND_QUALITY = 5; private static final int SOUND_QUALITY = 5;
private final int INVALID_SOUND_ID = -1; private final int INVALID_SOUND_ID = -1;
@ -75,15 +74,17 @@ public class Cocos2dxSound {
} }
} }
public int playEffect(String path){ public int playEffect(String path, boolean isLoop){
Integer soundId = this.mPathSoundIDMap.get(path); Integer soundId = this.mPathSoundIDMap.get(path);
if (soundId != null){ if (soundId != null){
// the sound is preloaded // the sound is preloaded, stop it first
this.mSoundPool.stop(soundId);
// play sound // play sound
int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume, int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume,
this.mRightVolume, SOUND_PRIORITY, SOUND_LOOP_TIME, SOUND_RATE); this.mRightVolume, SOUND_PRIORITY, isLoop ? -1 : 0, SOUND_RATE);
// record sound id and stream id map // record sound id and stream id map
this.mSoundIdStreamIdMap.put(soundId, streamId); this.mSoundIdStreamIdMap.put(soundId, streamId);
@ -106,7 +107,7 @@ public class Cocos2dxSound {
* Because the method is supported from 2.2, so I can't use * Because the method is supported from 2.2, so I can't use
* it here. * it here.
*/ */
playEffect(path); playEffect(path, isLoop);
} }
return soundId.intValue(); return soundId.intValue();

View File

@ -122,7 +122,7 @@ void CocosDenshionTest::menuCallback(CCObject * pSender)
break; break;
// play effect // play effect
case 6: case 6:
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE); m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
break; break;
// stop effect // stop effect
case 7: case 7: