If you think the Android project LucyTheMoocher listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.lucythemoocher.sounds;
//fromwww.java2s.comimport java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import com.lucythemoocher.R;
import com.lucythemoocher.Globals.Globals;
import com.lucythemoocher.util.Resources;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
/**
* Class aimed to play sounds\n
* Warning : I use SoundPool to play the music, this is bad because SoundPool
* uses audio stream uncompressed in memory, but this is the only way to have the power
* to choice the speed of the sounds.\n
* N.B. the music samples must have the exact same duration !
*/publicclass SoundManager {
privatestaticfinalint BACKGROUND_THEME = 0;
privatestaticfinalint BACKGROUND_LVL1 = 1;
privatestaticfinalint BACKGROUND_LVL2 = 2;
privatestaticfinalint BACKGROUND_LVL3 = 3;
private SoundPool sounds_ = null;
private HashMap<Integer, Integer> loaded_ = null;
private HashMap<Integer, Integer> playing_ = null;
private Vector<ArrayList<Integer>> soundsBackground_;
privateint sampleDuration_;
privatefloat lastPlay_;
privateboolean started_;
private SoundsState state_;
private MediaPlayer player_;
public SoundManager() {
player_ = MediaPlayer.create(Resources.getActivity(), R.raw.theme_loop);
}
/**
* Update
*/publicvoid update() {}
/**
* Load sound resources
*/publicvoid load() {}
/**
* Start playing
*/publicvoid start() {
player_.start();
player_.setLooping(true);
}
/**
* Stop playing and rewind
*/publicvoid stop() {
player_.pause();
player_.seekTo(0);
}
/**
* Pause
*/publicvoid pause() {
player_.pause();
}
/**
* Resume
*/publicvoid resume() {
player_.start();
}
/**
* Play sound from resource id
* @param resource
*/void playSound(int resource) {
AudioManager mgr = (AudioManager)Resources.getActivity().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
//TODO last argument should be the game speed
playing_.put(resource, sounds_.play(loaded_.get(resource), volume, volume, 1, 0, 1f));
}
/**
* Change state
* @param newState
*/void changeState(SoundsState newState) {
state_ = newState;
}
}