Home » Source Code » cocos2dx+socket » Cocos2dxSound.java

Cocos2dxSound.java ( File view )

  • By 云飞 2013-10-30
  • View(s):166
  • Download(s):5
  • Point(s): 2
			/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.lib;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

/**
 * 
 * This class is used for controlling effect
 *
 */

public class Cocos2dxSound {

	private Context mContext;
	private SoundPool mSoundPool;
	private float mLeftVolume;
	private float mRightVolume;
	
	// sound id and stream id map
	private HashMap<Integer,Integer> mSoundIdStreamIdMap;
	// sound path and sound id map
	private HashMap<String,Integer> mPathSoundIDMap;
	
	private static final String TAG = "Cocos2dxSound";
	private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5;
	private static final float SOUND_RATE = 1.0f;
	private static final int SOUND_PRIORITY = 1;
	private static final int SOUND_QUALITY = 5;
	
	private final int INVALID_SOUND_ID = -1;
	private final int INVALID_STREAM_ID = -1;
	
	public Cocos2dxSound(Context context){

		this.mContext = context;	
		initData();
	
}
	
	public int preloadEffect(String path){

		int soundId = INVALID_SOUND_ID;
		
		// if the sound is preloaded, pass it
		if (this.mPathSoundIDMap.get(path) != null){

			soundId =  this.mPathSoundIDMap.get(path).intValue();
		
} else {

			soundId = createSoundIdFromAsset(path);
			
			if (soundId != INVALID_SOUND_ID){

				// the sound is loaded but has not been played
				this.mSoundIdStreamIdMap.put(soundId, INVALID_STREAM_ID);
				
				// record path and sound id map
				this.mPathSoundIDMap.put(path, soundId);
			
}
		
}
			
		return soundId;
	
}
	
	public void unloadEffect(String path){

		// get sound id and remove from mPathSoundIDMap
		Integer soundId = this.mPathSoundIDMap.remove(path);
		
		if (soundId != null){

			// unload effect
			this.mSoundPool.unload(soundId.intValue());
			
			// remove record from mSoundIdStreamIdMap
			this.mSoundIdStreamIdMap.remove(soundId);
		
}
	
}
	
	public int playEffect(String path, boolean isLoop){

		Integer soundId = this.mPathSoundIDMap.get(path);
		
		if (soundId != null){

			// the sound is preloaded, stop it first
			
			this.mSoundPool.stop(soundId);
			
			// play sound
			int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume, 
					this.mRightVolume, SOUND_PRIORITY, isLoop ? -1 : 0, SOUND_RATE);
			
			// record sound id and stream id map
			this.mSoundIdStreamIdMap.put(soundId, streamId);
		
} else {

			// the effect is not prepared
			soundId = preloadEffect(path);	
			if (soundId == INVALID_SOUND_ID){

				// can not preload effect
				return INVALID_SOUND_ID;
			
}
			
			/*
			 * Someone reports that, it can not play effect for the
			 * first time. If you are lucky to meet it. There are two
			 * ways to resolve it.
			 * 1. Add some delay here. I don't know how long it is, so
			 *    I don't add it here.
			 * 2. If you use 2.2(API level 8), you can call 
			 *    SoundPool.setOnLoadCompleteListener() to play the effect.
			 *    Because the method is supported from 2.2, so I can't use
			 *    it here.
			 */
			playEffect(path, isLoop);
		
}
		
		return soundId.intValue();
	
}
	
	public void stopEffect(int soundId){

        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
        
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){

        	this.mSoundPool.stop(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        
}
	
}
	
	public void pauseEffect(int soundId){

        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
        
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){

        	this.mSoundPool.pause(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        
}
	
}
	
	public void resumeEffect(int soundId){

        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
        
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){

        	this.mSoundPool.resume(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        
}
	
}
	
	public void pauseAllEffects(){

		// autoResume is available since level 8
		pauseOrResumeAllEffects(true);
	
}
	
	public void resumeAllEffects(){

		// autoPause() is available since level 8
		pauseOrResumeAllEffects(false);
	
}
	
	@SuppressWarnings("unchecked")
	public void stopAllEffects(){

		Iterator<?> iter = this.mSoundIdStreamIdMap.entrySet().iterator();
		while (iter.hasNext()){

			Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)iter.next();
			int soundId = entry.getKey();
			this.stopEffect(soundId);
		
}
	
}
	
	public float getEffectsVolume(){

		return (this.mLeftVolume + this.mRightVolume) / 2;
	
}
	
	@SuppressWarnings("unchecked")
	public void setEffectsVolume(float volume){

		// volume should be in [0, 1.0]
		if (volume < 0){

			volume = 0;
		
}	
		if (volume > 1){

			volume = 1;
		
}
		
		this.mLeftVolume = this.mRightVolume = volume;
		
		// change the volume of playing sounds
		Iterator<?> iter = this.mSoundIdStreamIdMap.entrySet().iterator();
		while (iter.hasNext()){

			Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)iter.next();
			this.mSoundPool.setVolume(entry.getValue(), mLeftVolume, mRightVolume);
		
}
	
}
	
	public void end(){
		
		this.mSoundPool.release();	
		this.mPathSoundIDMap.clear();	
		this.mSoundIdStreamIdMap.clear();
		
		initData();
	
}
	
	public int createSoundIdFromAsset(String path){

		int soundId = INVALID_SOUND_ID;
		
		try {

			soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0);
		
} catch(Exception e){

			 Log.e(TAG, "error: " + e.getMessage(), e);
		
}
		
		return soundId;
	
}
	
	private void initData(){

		this.mSoundIdStreamIdMap = new HashMap<Integer,Integer>();
		mSoundPool = new SoundPool(MAX_SIMULTANEOUS_STREAMS_DEFAULT, AudioManager.STREAM_MUSIC, SOUND_QUALITY);
		mPathSoundIDMap = new HashMap<String,Integer>();
		
		this.mLeftVolume = 0.5f;
		this.mRightVolume = 0.5f;
	
}
	
	@SuppressWarnings("unchecked")
	private void pauseOrResumeAllEffects(boolean isPause){

		Iterator<?> iter = this.mSoundIdStreamIdMap.entrySet().iterator();
		while (iter.hasNext()){

			Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)iter.next();
			int soundId = entry.getKey();
			if (isPause) {

				this.pauseEffect(soundId);
			
} else {

				this.resumeEffect(soundId);
			
}
		
}
	
}

}
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 2

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
ChatClient.cpp513.00 B14-01-12 18:00
ChatClient.vcproj4.58 kB12-01-12 15:43
ChatClient.vcproj.PCoS-20111206OT.Administrator.user1.39 kB14-01-12 18:40
ODSocket.cpp3.69 kB12-01-12 16:37
ODSocket.h1.61 kB12-01-12 13:43
ReadMe.txt1.29 kB10-01-12 16:26
stdafx.cpp297.00 B10-01-12 16:26
stdafx.h320.00 B10-01-12 16:26
targetver.h765.00 B10-01-12 16:26
ChatClient.sln896.00 B10-01-12 16:26
ChatClient.suo14.00 kB14-01-12 18:40
Android.mk880.00 B12-01-12 17:39
readme.doc170.00 kB14-01-12 18:40
HelloWorld.mkb291.00 B01-12-11 18:36
Main.cpp250.00 B01-12-11 18:36
Main.h42.00 B01-12-11 18:36
AndroidManifest.xml1.13 kB01-12-11 18:36
build_native.sh605.00 B01-12-11 18:36
default.properties364.00 B01-12-11 18:36
R.java867.00 B01-12-11 18:36
Android.mk303.00 B01-12-11 18:36
Application.mk148.00 B01-12-11 18:36
Android.mk534.00 B01-12-11 18:36
main.cpp993.00 B01-12-11 18:36
list.sh362.00 B01-12-11 18:36
icon.png7.17 kB01-12-11 18:36
icon.png2.24 kB01-12-11 18:36
icon.png4.00 kB01-12-11 18:36
game_demo.xml578.00 B01-12-11 18:36
helloworld_demo.xml595.00 B01-12-11 18:36
strings.xml112.00 B01-12-11 18:36
ApplicationDemo.java2.37 kB01-12-11 18:36
Cocos2dxAccelerometer.java3.17 kB01-12-11 18:36
Cocos2dxActivity.java7.47 kB01-12-11 18:36
Cocos2dxBitmap.java11.16 kB01-12-11 18:36
Cocos2dxGLSurfaceView.java13.50 kB01-12-11 18:36
Cocos2dxMusic.java6.06 kB01-12-11 18:36
Cocos2dxRenderer.java4.70 kB01-12-11 18:36
Cocos2dxSound.java7.63 kB01-12-11 18:36
Cocos2dxTypefaces.java1.71 kB01-12-11 18:36
.badaprj171.00 B01-12-11 18:36
.cproject77.53 kB01-12-11 18:36
.project2.91 kB01-12-11 18:36
application.xml622.00 B01-12-11 18:36
MainMenu.png15.87 kB01-12-11 18:36
Splash.png84.47 kB01-12-11 18:36
manifest.xml871.00 B01-12-11 18:36
.badaprj260.00 B01-12-11 18:36
.cproject75.58 kB01-12-11 18:36
.project3.06 kB01-12-11 18:36
application.xml866.00 B01-12-11 18:36
Splash_type2.png29.58 kB01-12-11 18:36
Splash_type3.png34.29 kB01-12-11 18:36
Splash_type4.png84.47 kB01-12-11 18:36
test_type2.png8.00 kB01-12-11 18:36
test_type3.png8.00 kB01-12-11 18:36
test_type4.png12.04 kB01-12-11 18:36
manifest.xml779.00 B01-12-11 18:36
HelloWorldEntry.cpp1.23 kB01-12-11 18:36
Android.mk760.00 B01-12-11 18:36
AppDelegate.cpp4.05 kB23-12-11 11:12
AppDelegate.h1.09 kB01-12-11 18:36
HelloWorldScene.cpp4.26 kB14-01-12 18:18
HelloWorldScene.h899.00 B07-01-12 18:56
ODSocket.cpp3.69 kB12-01-12 17:11
ODSocket.h1.63 kB12-01-12 18:58
sdBall.cpp2.14 kB10-01-12 18:09
sdBall.h571.00 B22-12-11 19:54
sdDangBan.cpp2.51 kB14-01-12 18:05
sdDangBan.h725.00 B22-12-11 19:54
AppController.h1.45 kB01-12-11 18:36
AppController.mm4.93 kB01-12-11 18:36
HelloWorld-Info.plist963.00 B01-12-11 18:36
project.pbxproj132.52 kB01-12-11 18:36
HelloWorld_Prefix.pch189.00 B01-12-11 18:36
main.m368.00 B01-12-11 18:36
RootViewController.h1.36 kB01-12-11 18:36
RootViewController.mm2.61 kB01-12-11 18:36
project.pbxproj137.45 kB01-12-11 18:36
.cproject141.13 kB01-12-11 18:36
.project2.77 kB01-12-11 18:36
main.cpp213.00 B01-12-11 18:36
main.h114.00 B01-12-11 18:36
Makefile977.00 B01-12-11 18:36
postCompiled.sh804.00 B01-12-11 18:36
app.config.txt90.00 B01-12-11 18:36
app.icf139.00 B01-12-11 18:36
background.png477.73 kB22-12-11 19:30
Ball.png3.35 kB21-12-11 14:14
CloseNormal.png5.12 kB01-12-11 18:36
CloseSelected.png6.02 kB01-12-11 18:36
DangBan.png9.23 kB21-12-11 15:15
HelloWorld.png27.88 kB21-12-11 16:38
Icon.png9.11 kB01-12-11 18:36
HelloWorld.win32.vcproj5.21 kB12-01-12 17:16
HelloWorld.win32.vcproj.PCoS-20111206OT.Administrator.user1.44 kB14-01-12 18:17
HelloWorld.win32.vcproj.user487.00 B01-12-11 18:36
HelloWorld.win32.vcxproj7.01 kB01-12-11 18:36
HelloWorld.win32.vcxproj.filters1.18 kB01-12-11 18:36
HelloWorld.win32.vcxproj.user764.00 B01-12-11 18:36
main.cpp408.00 B01-12-11 18:36
main.h277.00 B01-12-11 18:36
HelloWorld.TMK33.03 kB01-12-11 18:36
HelloWorld.wophone.vcproj6.34 kB01-12-11 18:36
HelloWorld.wophone.vcproj.user1.40 kB01-12-11 18:36
HelloWorldUnicodeScript.h250.00 B01-12-11 18:36
HelloWorldUnicodeScript_str.h604.00 B01-12-11 18:36
main.cpp389.00 B01-12-11 18:36
Makefile.ARM2.07 kB01-12-11 18:36
NewDeleteOp.cpp2.05 kB01-12-11 18:36
HelloWorld.upiproj40.80 kB01-12-11 18:36
install.xml380.00 B01-12-11 18:36
TG3AppDllEntry.cpp5.27 kB01-12-11 18:36
TG3AppDllEntry.h1.39 kB01-12-11 18:36
ODSocket.cpp3.69 kB12-01-12 16:37
ODSocket.h1.61 kB12-01-12 13:43
ReadMe.txt1.27 kB10-01-12 16:34
Server.cpp701.00 B14-01-12 18:00
Server.vcproj4.53 kB12-01-12 16:24
Server.vcproj.PCoS-20111206OT.Administrator.user1.39 kB14-01-12 18:40
stdafx.cpp293.00 B10-01-12 16:34
stdafx.h320.00 B10-01-12 16:34
targetver.h765.00 B10-01-12 16:34
Server.sln884.00 B10-01-12 16:35
Server.suo18.00 kB14-01-12 18:40
<application>0.00 B14-01-12 18:39
<application>0.00 B14-01-12 18:39
<lib>0.00 B14-01-12 18:39
<cocos2dx>0.00 B14-01-12 18:39
<cocos2dx>0.00 B14-01-12 18:39
<org>0.00 B14-01-12 18:39
<helloworld>0.00 B14-01-12 18:39
<drawable-hdpi>0.00 B14-01-12 18:39
<drawable-ldpi>0.00 B14-01-12 18:39
<drawable-mdpi>0.00 B14-01-12 18:39
<layout>0.00 B14-01-12 18:39
<values>0.00 B14-01-12 18:39
<org>0.00 B14-01-12 18:39
<Icons>0.00 B14-01-12 18:39
<Icons>0.00 B14-01-12 18:39
<gen>0.00 B14-01-12 18:39
<jni>0.00 B14-01-12 18:39
<res>0.00 B14-01-12 18:39
<src>0.00 B14-01-12 18:39
<sdk1.0>0.00 B14-01-12 18:39
<sdk2.0>0.00 B14-01-12 18:39
<src>0.00 B14-01-12 18:39
<HelloWorld.xcodeproj>0.00 B14-01-12 18:39
<HelloWorld.xcodeproj>0.00 B14-01-12 18:39
<pak>0.00 B14-01-12 18:39
<ChatClient>0.00 B14-01-12 18:41
<airplay>0.00 B14-01-12 18:39
<android>0.00 B14-01-12 18:39
<bada>0.00 B14-01-12 18:39
<Classes>0.00 B14-01-12 18:39
<ios>0.00 B14-01-12 18:39
<iphone>0.00 B14-01-12 18:39
<Linux>0.00 B14-01-12 18:39
<Resource>0.00 B14-01-12 18:39
<win32>0.00 B14-01-12 18:42
<wophone>0.00 B14-01-12 18:39
<Server>0.00 B14-01-12 18:41
<PCClient>0.00 B14-01-12 18:41
<HelloWorld>0.00 B14-01-12 18:39
<PCServer>0.00 B14-01-12 18:41
...
Sponsored links

Cocos2dxSound.java (1.05 MB)

Need 2 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D