SoundPool in fragment not playing Android Studio - android

I am trying to play a sound clip from a fragment, but the sound is not playing. I am not getting any errors in console and no crashing, just no sound is played. yes, my volume is up and nothing is muted. All relevant code is below
int soundId;
int currentSound;
SoundPool sp;
boolean pressed;
boolean loaded;
sp = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
currentSound = R.raw.whatarethose;
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
loaded = true;
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(loaded) {
soundId = sp.load(getActivity(), currentSound, 1);
sp.play(soundId, 1, 1, 0, 0, 1);
}
}
});

You are loading the sound in the button click. try to load the sound after yu set the OnCompleteListener, before the click.
int soundId;
int currentSound;
SoundPool sp;
boolean pressed;
boolean loaded;
sp = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
currentSound = R.raw.whatarethose;
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
loaded = true;
}
});
soundId = sp.load(getActivity(), currentSound, 1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(loaded) {
sp.play(soundId, 1, 1, 0, 0, 1);
}
}
});

Related

SoundPool not playing sound (while MediaPlayer does)

I am trying to play a sound in an app. However, using SoundPool I am unable to do so. The code is really simple and I don't see where it can fail.
A similar code but using MediaPlayer does work, but I am interested in using SoundPool.
SoundPool code:
private void playSound2(){
SoundPool sp = new SoundPool.Builder().build();
int soundID = sp.load(MainActivity.this, R.raw.sound, 1);
sp.play(soundID, 1, 1, 1, 0, 1);
}
Context:
public void goButtonClick(View view){
Button goButton = findViewById(id.button2);
if (onGoing){
goButton.setText("GO!");
} else {
goButton.setText("STOP");
//playSound(); // MediaPlayer
playSound2(); // SoundPool
}
onGoing = !onGoing;
}
MediaPlayer version (works):
private void playSound(){
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.sound);
mediaPlayer.start();
}
Solved!
I was missing to wait for the loading of the sound file into soundPool. The correct code is:
private void playSound2() {
SoundPool sp = new SoundPool.Builder().build();
int soundID = sp.load(MainActivity.this, raw.sound, 1);
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool sp, int soundID, int i1) {
sp.play(soundID, 1, 1, 1, 0, 1);
}
});
}

Android - How to mute SoundPool in TimerTask without stopping it

I am trying to create a metronome sound, however, what isn't working is the ability to mute it. I would like the ability to mute it without stopping the TimerTask since I want the rate to be consistent once it is unmuted. Here is my code:
public class Metronome {
private boolean mute;
private boolean playing = false;
private Timer mainTimer;
private SoundPool mSoundPool;
int mSoundID;
public Metronome(Context context) {
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mSoundID = mSoundPool.load(context, R.raw.metronome, 1);
}
public void play(float pace, boolean mute) {
mainTimer = new Timer();
MyTimerTask mainTimerTask = new MyTimerTask();
mainTimer.schedule(mainTimerTask, 0, Math.round(pace * 1000));
this.mute = mute;
playing = true;
}
public void stop() {
mainTimer.cancel();
mainTimer.purge();
playing = false;
}
public boolean isPlaying() {
return playing;
}
public void setMute(boolean mute) {
this.mute = mute;
if (mute) {
mSoundPool.setVolume(mSoundID, 0, 0);
} else {
mSoundPool.setVolume(mSoundID, 1, 1);
}
}
private void playSound() {
if (!mute) {
mSoundPool.play(mSoundID, 1, 1, 1, 0, 1);
}
}
class MyTimerTask extends TimerTask {
#Override
public void run() {
playSound();
}
}
}
However, calling setMute(true) does not work. Does anyone know how I can mute my SoundPool?
Figured it out. It works when I use static methods and variables.
public class Metronome {
public static boolean mute = false;
public static boolean playing = false;
private static Timer mainTimer;
private static SoundPool soundPool;
private static int soundID;
public static void loadSound(Context context) {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
soundID = soundPool.load(context, R.raw.metronome, 1);
}
public static void play(float pace, boolean isMuted) {
mainTimer = new Timer();
MyTimerTask mainTimerTask = new MyTimerTask();
mainTimer.schedule(mainTimerTask, 0, Math.round(pace * 1000));
mute = isMuted;
playing = true;
}
public static void stop() {
mainTimer.cancel();
mainTimer.purge();
playing = false;
}
static class MyTimerTask extends TimerTask {
#Override
public void run() {
playSound();
}
private void playSound() {
if (!mute) {
soundPool.play(soundID, 1, 1, 1, 0, 1);
}
}
}
}

How to create multiple Soundpools?

I have one SoundPool in my Android app, but I don't know how to write code for 2 or more SoundPool instances. Is there a way to rework this code for multiple SoundPools?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button1);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);
}
public boolean onTouch( View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
Try:
// 8 soundpools
SoundPool[] soundPool = new SoundPool[8];
int[] soundID = new int[8];
// You can have more that one set per sound if need, just make more raw sound
// arrays with different sounds.
// 8 sounds per soundpool
int[] rawSounds = new int[]{ R.id.sound1, R.id.sound2, R.id.sound3,
R.id.sound4, R.id.sound5, R.id.sound6,
R.id.sound7, R.id.sound8 };
boolean[] loaded = new boolean[8];
// Go through each soundpool
for(int i = 0; i < 8; i++)
{
soundPool[i] = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
// 8 sounds in one soundpool
for(int h = 0; h < 8; h++)
{
soundID[i] = soundPool.load(this, rawSounds[i], 1);
}
soundPool[i].setOnLoadCompleteListener(new OnLoadCompleteListener()
{
Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status)
{
// One boolean per soundpool
loaded[i] = true;
}
});
}
Then use a for loop to see what is being press for which sound and use soundID[i].play to play sounds.
Hope this helps even though a little late!)

Soundpool with loop not working properly

I have a sound clip play while I am displaying some numbers, then want it to stop. Then I want to repeat the process.
init stuff:
private SoundPool soundPool;
private int soundRoll;
boolean soundloaded = false;
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundloaded = true;
}
});
soundRoll = soundPool.load(this, R.raw.roll, 1);
Thread Call:
public void update() {
.......
new Thread(new Runnable() {
public void run() {
int streamID = 0;
streamID = soundPool.play(soundRoll, volume, volume, 1, -1, 1);
... loop to animate text....
soundPool.stop(streamID);
}
}).start();
}
This works fine every other time.
so the first time it plays and stops at the right time.
second time I hear nothing
but then it works the third time
If I take out the looping, then it works.
Any help would be appretiated
Seems that if I switch to an OGG file from an MP3, the bug goes away

Sound Pool Crashing

I use sound pool to play a sound when a user presses a button. After a number of button presses the app force closes. The sound that is playing is only a few seconds long. Is there a better way to implement audio?
I use this class:
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(200, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
public void clear(){
mSoundPoolMap.clear();
mSoundPool.release();
}
}
It seems you are trying to play sound before it is loaded. SoundPool.load method is not synchronous. So if you call
addSound(...);
playSound(..);
You will get 100% crash in runtime.
You should invoke play() only after onLoadComplete is called:
public void addSound(int index, int soundResId) {
soundPool.setOnLoadCompleteListener((sp, sampleId, status) -> {
if (status == 1) {
mSoundPoolMap.put(index, sampleId);
}
});
soundPool.load(context, soundResId, 1);
}

Categories

Resources