SoundPool Not Loading in Android App - android

I'm making an app where I'm trying to load a single sound onto a SoundPool so I can manage the playback rate when I want, play it when I want, and release it when I want. However, my SoundPool is not loading. I've tried to change a lot of different things and nothing seems to work. I put a Log.d in the OnLoadComplete function to see if it loads, and the log doesn't appear when running... Maybe after looking at my code somebody else could help me. Thanks!
private int currentVoice = 0;
private SoundPool voices = null;
....
public void onCreate(Bundle savedInstanceState) {
....
voices = new SoundPool(1, 3, 0);
voices.setOnLoadCompleteListener(this);
....
}
private void giveSentence() throws IOException {
....
voices.release();
currentVoice = voices.load(this, sentences[index].voice, 1);
voices.play(currentVoice, 1, 1, 1, 0, 1f);
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1, 1, 1, 0, 1f);
boolean loaded = true;
Log.d("Game","Is it loaded? " + loaded);
}

You should initialize your sound pool somewhere in a onCreate method, I guess.
private SoundPool voices;
#Override
public void onCreate(Bundle savedInstanceState) {
voices = new SoundPool(1, 3, 0);
voices.setOnLoadCompleteListener(this);
}

Fixed myself, I was doing voices.release(); before anything was loaded. For some reason that just turned the SoundPool off. Took out that line and did voices.release() after I checked if it was already loaded. Now it works.

Related

SoundPool doesn`t play sound after clicking several times

When i click button plays sound on my app, but keep clicking for a while doesn`t play anymore. Maybe it is memory issue? Could you solve me my issue? Here is implemented play sound via SoundPool: when i click button i called play method: and play method works background thread
private void play(int resId) {
soundPool = buildBeforeAPI21();//TODO: refactor with deprecated constructor
soundPool.load(activity, Integer.parseInt(resId), 1);
}
public SoundPool buildBeforeAPI21() {
if (soundPool == null) {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (status == 0) {
soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);
}
}
});
}
return soundPool;
}
Don't create a new SoundPool with each click. That is extremely wasteful. Do this instead:
1) Create the SoundPool just once
2) Load it with all the sounds you want to play, just once
3) Wait for all the sounds to load
4) Just call soundPool.play() with each click.
Maybe this answer cames too late; but here we go. The problem is in the line
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
That '2' number is the int maxStreams, following the documentation "the maximum number of simultaneous streams for this SoundPool object" (you can go deep on this here). Now change it and set it to 10, for example, and try again. And after that, you cand adjust it as your wishes. So:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
Hope this helps (if it is still necessary).

How to speed up SoundPool in Android?

I am attempting to create a simple Morse code application. When the user presses a button the Morse sound should start before ending when released.
The problem is the latency- the Morse sound does not start until roughly 400ms after the user presses the button. I am not sure why this is exactly but after researching the problem i think it is the way my code is structured. The file i am trying to play is in Mp3 format and is located in the raw folder.
I had this completed using the Media Player but i ran into the same problem in regard it not being responsive enough so i opted to try and use Sound pool. Does anyone have any advice/suggestions as to how i can speed up the operation? This is new territory to me in regards development.
public int S1 = R.raw.morse;
private SoundPool soundPool;
private boolean loaded;
static int x;
public void initSounds(Context context) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
final int y = soundPool.load(context, R.raw.morse, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
playSound(y);
}
});
}
public void playSound(int soundID) {
if(loaded) {
x = soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);
}
}
//Calling code
pad.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
//State of a toggle button
if(audioOnOff==true) {
//Sound pool object is created and initialized as a global variabe
sp.initSounds(getApplicationContext());
}
}
It looks to me like what's happening here is that each time the button is pressed, you're loading the sound, waiting for it to finish loading, then playing it. What you actually want is to load the sound once, then just play it each time the button is pressed.
So instead of calling initSounds in your onTouchListener, call initSounds somewhere else before the button is pressed. Then in your onTouchListener you just call your playSound method.
Finally, make sure you remove the playSound method call from your onLoadCompleteListener so you don't get a mysterious noise when you initially load up the sound ;)

In Android, Where to load SoundPool? In Oncreate or need to create a separate method?

I am programming a board game in android and need a sound effect for every click.I am using sound pool. There will be a lot of clicks and so I wanted to know where I have to declare the SoundPool and load it? In the onCreate function , or have a function that creates and loads and plays the sound and is called at every click?
//make soundpool public
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//load sound here and play wherever needed
int soundID=soundPool.load(this,R.raw.metalimpact,1);
}
//or put it all in a function
private void makeSound(){
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
final int soundID=soundPool.load(this,R.raw.metalimpact,1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(soundID, 1, 1, 0, 0, 1f);
}});
}
What is the right way to impement the SoundPool?
Will there be performance issues in the ways I have implemented it?
When should I release the soundpool object?..In the OnDestroy or onPause()?
Also SoundPool has been deprecated, but my min api level supported is 11. So what should I do?
Please help!!

SoundPool is loading sounds but every time I play it it says "sample ___ is not ready"?

I am having a bit of difficulty with my SoundPool. I have an ArrayList<Integer> of my soundIDs located in my res/raw folder. I'm creating the SoundPool like so in my ViewHolder class for my RecyclerView:
//create the SoundPool
sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
//create the AudioManager for managing volume
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
//store the streamVolume in an int
streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//set the load listener for my soundfiles
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
I am playing the sound on the click of an ImageView like so:
#Override
public void onClick(View v) {
if (v.equals(cardImg)) {
sp.load(context, cardData.getSoundId(), 1);
if (loaded) {
sp.play(cardData.getSoundId(), streamVolume, streamVolume, 1, 0, 1f);
}
}
}
And every time I click on the cardImg it gives me this error:
W/SoundPool﹕ sample 2130968609 not READY
Where the sample int changes depending on which card I click, like it should.
So I know that it's GETTING the proper soundID from my ArrayList (stored in CardData class), and it definitely sees that it's loaded, because it wouldn't try to play otherwise because loaded would never be true. My question is: Why are my sounds never ready?
My sounds are .m4a format which Android says is supported in the documentation. I've tried on an Android 5.0 device and an Android 4.4 device but nothing changes.
I have tried using a MediaPlayer but I am running into memory errors as I have many short clips that need to be able to be spammed and I read that a SoundPool was a much better way to manage this (?).
Any help would be appreciated! I am super vexxed here.
You're using the wrong id. SoundPool.load returns an id that is suppose to be passed in to SoundPool.play.

A click noise is happening at the end of the Audio playing.

I am having a couple problems I could really use some help on both. The first is I am trying to get an audio file to play when the application is started and as soon as the shake occurs it stops. (It is not playing it at all on either screen it is supposed to). The other issue I am having is when I shake the phone an audio is supposed to play. It is doing just that, but the problem is that at the end of the audio playing it make a pop sound. That noise isn't on the audio files so I am not exactly sure where that sound is coming from. Any help would be appreciated. Thanks in advance.
public class Ask extends Activity{
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
String[] answer;
int possibleAnswers, randomAnswer, talkRun=0;
long lastClick;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask);
final Random generator = new Random();
//Sounds
final SoundPool sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
final int sound0 = sounds.load(this, R.raw.coughing, 1);
final int sound25 = sounds.load(this, R.raw.askbud, 1);
sounds.play(sound25, 1f, 1f, 1, 0, 1f);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
final TextView tv = (TextView)findViewById(R.id.answer);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
if (System.currentTimeMillis() - lastClick > 500) {
lastClick = System.currentTimeMillis();
sounds.stop(sound25);
sounds.stop(sound0);
randomAnswer = generator.nextInt(possibleAnswers);
if(randomAnswer==0){
sounds.play(sound0, 1f, 1f, 1, 0, 1f);
}
}
}
});
}
#Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
#Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}
}
I also had this sound click problem (or pop as you call it) at the end of the sounds I was playing. My files were .wav files then, but he click sound disappeared after I converted the sound files to .ogg.
I was experiencing this on the Android emulator.
its not working in the on create because sounds are not loaded yet when you call play here
sounds.play(sound25, 1f, 1f, 1, 0, 1f);
please look at
public void setOnLoadCompleteListener (SoundPool.OnLoadCompleteListener listener)
http://developer.android.com/reference/android/media/SoundPool.html#setOnLoadCompleteListener(android.media.SoundPool.OnLoadCompleteListener)
I fixed it by switching from using sound pool to using media player. Also got to make sure to release() or it was causing other problems like the sound stopped playing.

Categories

Resources