Android SoundPool - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android SoundPool class.

This code example shows how to use the following methods:load, setVolume, unload
				player = new MediaPlayer();
				player.setDataSource(fileName);
				player.prepare();
			}else{ 
				if(soundID!=-1) unloadSound();
				soundID = pool.load(fileName, 1);
			} 
			setVolume(volume);
			bIsLoaded = true;
			this.fileName = fileName;
			this.stream = stream;
		} catch (Exception e) {
			Log.e("OF","couldn't load " + fileName,e);
		}  
	} 
	 
	void unloadSound(){ 
		if(player!=null){
			player.release();
			player = null;

9
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play, stop
        private boolean TestPriority() throws java.lang.InterruptedException { 
            if (DEBUG) Log.d(LOG_TAG, "Begin priority test");
            boolean result = true;
 
            // play a normal priority looping sound 
            int normalId = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    NORMAL_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (DEBUG) Log.d(LOG_TAG, "Start note " + normalId);
            if (normalId == 0) {
                Log.e(LOG_TAG, "Error occurred starting note");
                return false; 
            } 
            sleep(250);
 
            // play a low priority sound 
            int id = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                    LOW_PRIORITY, DEFAULT_LOOP, 1.0f);
            if (id > 0) {
                Log.e(LOG_TAG, "Normal > Low priority test failed");
                result = false;

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play, release, sleep
	} 
	 
	public void play() throws SoundException, InterruptedException { 
		if (soundPool == null) return;
		try { 
			if (soundPool.play(soundID, 1, 1, 0, 0, 1) == 0) {
				//try twice as the sound maybe is not ready; 
				Log.w("AndroidPoolSingleSoundPlayer.play()", "Sound " + filePath + " not ready wait 1s");
				Thread.sleep(1000);
				if (soundPool.play(soundID, 1, 1, 0, 0, 1) == 0) {
					throw new SoundException("Sound not ready " + 
							filePath);
				} 
			} 
			Thread.sleep(duration);
		} finally { 
			unprepare(); 
		} 
	} 
	 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:
 
	private void initSoundPool()  
	{ 
		if ( soundPool == null ) 
		{ 
			soundPool = new SoundPool(DEFAULT_POLYPHONY_VOICES, AudioManager.STREAM_MUSIC, 1);
		} 
		 
		if ( soundMap == null) 
		{ 
			soundMap = new HashMap<String, Integer>();
		} 
		 
		if ( streamMap == null) 
		{ 
			streamMap = new HashMap<String, ArrayList<Integer>>();
		} 
		 
		if ( assetMap ==null ) 
		{ 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:release
			} 
		} 
	} 
 
	public void end() { 
		this.mSoundPool.release();
		this.mPathStreamIDsMap.clear();
		this.mPathSoundIDMap.clear();
		this.mEffecToPlayWhenLoadedArray.clear();
 
		this.mLeftVolume = 0.5f;
		this.mRightVolume = 0.5f;
		 
		this.initData(this.simultaneousStreams);
	} 
 
	public int createSoundIDFromAsset(final String pPath) {
		int soundID = Cocos2dxSound.INVALID_SOUND_ID;
 
		try { 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play, stop
		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;
			} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play
		play(1); 
	} 
 
	@Override public void play (float volume) {
		if (streamIds.size == 8) streamIds.pop();
		streamIds.add(soundPool.play(soundId, volume, volume, 1, 0, 1));
	} 
 
	public void stop () { 
		for (int i = 0, n = streamIds.size; i < n; i++)
			soundPool.stop(streamIds.get(i));
	} 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play, stop
	} 
	 
	public void initialEffect() 
	{ 
		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;
		Log.i("cloudbox-app", "initialEffect succeed.");
	} 
	 
	public int createSoundIdFromAsset(String fileName){
		int soundId = INVALID_SOUND_ID;
		 
		try { 
			soundId = mSoundPool.load(mContext.getAssets().openFd(fileName), 0);
		} catch(Exception e){
			 Log.e(TAG, "error: " + e.getMessage(), e);

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:load, setOnLoadCompleteListener
	 
	 
	private SoundPool pool;
	 
	public SoundManager(Context c){
		pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
		pool.setOnLoadCompleteListener(this);
		 
		loadSwap = false;
		loadRotate = false;
		loadWin = false;
		 
		swap = pool.load(c, R.raw.swap, 1);
		rotate = pool.load(c, R.raw.rotate, 2);
		win = pool.load(c, R.raw.win, 3);
	} 
	 
	public int playSound(int soundId){
		if(soundId == swap && !loadSwap
				|| (soundId == rotate && !loadRotate)
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:play
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    } 
 
    public void onUntanButton(View v) {
        mSoundPool.play(mSounds[0], 1.0F, 1.0F, 0, 0, 1.0F);
    } 
 
    public void onUntanHighButton(View v) {
        mSoundPool.play(mSounds[0], 1.0F, 1.0F, 0, 0, 2.0F);
    } 
 
    public void onUntanLowButton(View v) {
        mSoundPool.play(mSounds[0], 1.0F, 1.0F, 0, 0, 0.5F);
    } 
 
    // ホームボタンで閉じて戻ってきた時に 
    // MediaPlayerを正しい状態にしてあげるため 
    // onCreateではなくonResumeで準備する 
    @Override 
6
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Android SoundPool Questions & Answers:

See Code Examples for Android SoundPool Methods: