Last active
August 29, 2015 14:17
-
-
Save davebren/5a8daea7631070e43696 to your computer and use it in GitHub Desktop.
Sound_Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.morse_tutor.util.sound; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.Vector; | |
import android.content.Context; | |
import android.media.AudioManager; | |
import android.media.SoundPool; | |
import android.media.SoundPool.OnLoadCompleteListener; | |
import android.util.Log; | |
import com.morse_tutor.R; | |
public class Sound_Manager { | |
private SoundPool sound_pool; | |
private HashMap<Integer, Integer> resource_locator; | |
private Context context; | |
private Vector<Integer> available_sounds = new Vector<Integer>(); | |
private On_Load loader; | |
private LinkedList<Integer> streams; | |
public boolean initialized; | |
public Sound_Manager(Context context){ | |
this.context = context; | |
} | |
public Sound_Manager initialize(int... sound_ids) { | |
sound_pool = new SoundPool(sound_ids.length, AudioManager.STREAM_MUSIC, 0); | |
resource_locator = new HashMap<Integer, Integer>(); | |
loader = new On_Load(sound_ids.length+1); | |
sound_pool.setOnLoadCompleteListener(loader); | |
streams = new LinkedList<Integer>(); | |
for (int i=0; i < sound_ids.length; i++) { | |
add(i+1,sound_ids[i]); | |
} | |
initialized = true; | |
return this; | |
} | |
public void add(int Index, int SoundID) { | |
available_sounds.add(Index); | |
resource_locator.put(Index, sound_pool.load(context, SoundID, 1)); | |
} | |
public SoundPool get_sound_pool() { | |
return sound_pool; | |
} | |
public void clear_streams() { // to use when playing a melody. | |
streams.clear(); | |
} | |
public void play_multi(int[] indexes) { // use this when playing a chord. | |
streams.clear(); | |
for (int i=0; i < indexes.length; i++) { | |
play(indexes[i]); | |
} | |
} | |
public int play(int index) { | |
if(available_sounds.contains(index)){ | |
if (loader.check_loaded(index)) { | |
int returner = -1; | |
streams.add(returner = sound_pool.play(resource_locator.get(index), 0.99f, 0.99f, 1, 0, 1f)); | |
return returner; | |
} else { | |
loader.play_on_load(index); | |
return -1; | |
} | |
} return -1; | |
} | |
public void stop_all() { | |
Iterator<Integer> iterate_streams = streams.iterator(); | |
while (iterate_streams.hasNext()) { | |
sound_pool.stop(iterate_streams.next()); | |
} | |
} | |
public void stop(int stream_id) { | |
sound_pool.stop(stream_id); | |
} | |
private class On_Load implements OnLoadCompleteListener { | |
LinkedList<Integer> play_on_load; | |
boolean[] loaded; | |
public On_Load(int num_sounds) { | |
loaded = new boolean[num_sounds]; | |
play_on_load = new LinkedList<Integer>(); | |
} | |
public void onLoadComplete(SoundPool sound_pool, int index, int status) { | |
loaded[index] = true; | |
Log.d("load complete", "" + index + ", " + status); | |
Iterator<Integer> to_play = play_on_load.iterator(); | |
int counter = 0; | |
while (to_play.hasNext()) { | |
if (to_play.next() == index) { | |
streams.add(sound_pool.play(resource_locator.get(index), 0.99f, 0.99f, 1, 0, 1f)); | |
play_on_load.remove(counter); | |
break; | |
} | |
counter++; | |
} | |
} | |
public boolean check_loaded(int index) { | |
if (index > -1 && index < loaded.length) { | |
return loaded[index]; | |
} else { | |
return false; | |
} | |
} | |
public void play_on_load(int index) { | |
play_on_load.add(index); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment