Android 2.2 - SoundPool sample 0 is not READY - android

I have searched StackOverflow and cannot find a situtation like mine. I am using four buttons with each button playing a sound file.
I am using SoundPool:
SoundPool sound = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
I am also using the OnLoadCompleteListener() which uses Log to create an I notification in LogCat.
When I launch the program in the emulator I see all four samples complete loading. During the program three of the sounds will play, however, one will always say:
WARN/SoundPool(4842): sample 0 not READY
Any Ideas.. cause i'm quite flabbergasted. The sound files are 16-bit pcm wave file playing squarewave tones.
Load Code:
sound.setOnLoadCompleteListener(new OnLoadCompleteListener(){
#Override
public void onLoadComplete(SoundPool sound, int sampleId, int status) {
if(status != 0)
Log.e("SOUND LOAD"," Sound ID: " + sampleId + " Failed to load.");
else
Log.i("SOUND LOAD"," Sound ID: " + sampleId + " loaded.");
}
});
soundID[0] = sound.load(this, R.raw.greennote, 1);
soundID[1] = sound.load(this, R.raw.rednote, 1);
soundID[2] = sound.load(this, R.raw.yellownote, 1);
soundID[3] = sound.load(this, R.raw.bluenote, 1);
Play Sound:
streamid.setStreamId(myActivity.sound.play(id, 0.5f, 0.5f, 0, 0, 1));

I'm having the same issues. From my experiments, it looks like there's something wrong with the ID handling. SoundPool just doesn't like sound IDs with the number 0.
So I have found a work-around. Start my sample IDs with 1, not 0. Hope this works.

Related

Android - Playing and stopping sound

What is the best way to play a sound and stop it?
I tried using RingtoneManager, MediaPlayer and SoundPool, but failed to stop the sound.
Is there a way to stop sound when using RingtoneManager TYPE_ALARM?
Please a simple snippet.
This is the last thing I tried:
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
List<Integer> streams = new ArrayList<Integer>();
int soundId = pool.load(getApplicationContext(), R.drawable.alarm, 1); //There are several versions of this, pick which fits your sound
try{
if(myWifiInfo.getRssi() < -55)
{
/*
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();*/
Thread.sleep(1000);
int streamId = -1;
streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamId);
textRssi.setText(String.valueOf(myWifiInfo.getRssi() + " WARNING"));
}
else {
Log.e("WIFI","Usao");
for (Integer stream : streams) {
pool.stop(stream);
}
streams.clear();
Log.e("WIFI","Izasao");
}
I'm assuming this function gets called many times? The problem is that you're adding the streamID to a local list. That list is getting recreated each time the function is called, so when you try to call stop the list will always be empty.
If its only being called once, then the problem is you're never calling stop, only play (they're in separate branches of that if).

Android AudioTrack slow playback

I'm trying to stream music in using AudioTrack.
The track plays, but the audio plays at half the rate. It's like the song has become slow motion.
try{
AudioTrack track= new AudioTrack( AudioManager.STREAM_MUSIC,
44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,
android.media.AudioFormat.ENCODING_PCM_16BIT,
android.media.AudioTrack.getMinBufferSize( 44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
android.media.AudioFormat.ENCODING_PCM_16BIT ),
AudioTrack.MODE_STREAM );
System.out.println("Min buffer: " + android.media.AudioTrack.getMinBufferSize( 44100,
android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
android.media.AudioFormat.ENCODING_PCM_16BIT ));
int cnt;
int totalWrite = 0;
boolean play = true;
byte buff[]=new byte[16384];
while((cnt=CircularByteBuffer.getInstance().getInputStream().read(buff))>0){
totalWrite += cnt;
System.out.println("Writing: " + cnt);
track.write(buff, 0, cnt);
if ( totalWrite > 60000 && play ){
track.play();
play = false;
}
}
}catch (Exception e)
{
}//end catch
In the CircularByteBuffer, the bytes are being written on another thread and read on this one. The song plays consistently without any pauses, but it just plays at a slow rate. I have no idea what it could be. Any ideas?
Assuming this is in fact a stereo stream, why are you creating the AudioTrack with CHANNEL_CONFIGURATION_MONO?
try with
track.setPlaybackRate(88200);
track.play();
It should play in normal speed
So it turns out that all the settings were fine. I ended up using the "CHANNEL_CONFIGURATION_STEREO" setting but I tried to run the application on an actual device. When I streamed it from the device, the music was perfectly fine. But the simulator was causing all the problems for the song.
I hope that is the reason and the simulator is just a memory hog and runs inefficiently.

Alarmmanager & Soundpool - Android

I'm doing an alarm System. The alarms are working OK, but the sound doesn't work.. I'm doing a little test and I don't understand where is the problem.. In the Alarm activity, I've the following code:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
try {
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("sound.ogg");
mySoundId = soundPool.load(descriptor, 1);
} catch (IOException e) {
Log.d(TAG,"We've problem's!!, "
+ e.getMessage());
}
soundPool.play(mySoundId, 1, 1, 0, 0, 1);
There are not errors.. but music doesn't work.. Instead, if I program the PLAY in a bottom work's!! Can anyone help me please??
I just tried your code and found the problem. It is a common problem in SoundPool. You need to wait for the sound to finish loading before you play it. That's why soundPool.play() returned zero. The sound was not ready.
In API 8, setOnLoadCompleteListener() was introduces which will solve the problem:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
//play the sound.
}
});
however, if you are targeting lower APIs, You can't tell for sure if your sound is ready to be played or not. In this case, I suggest you use MediaPlayer.

Android Soundpool - looped playback anomaly

I am encountering problems with playing looping sounds using SoundPool and .OGG files. I have this HashMap set up for finding a sound associated to a name and playing it/stopping it
public void playLoopSound(String soundName){
currentSound = (Integer) soundMap.get(soundName);
if(currentSound != -1){
try{
Logger.log("Playing Loop Sound: " + currentSound);
loopingSound = soundPool.play(currentSound, 1, 1, 0, -1, 1);
} catch (Exception e) {
Logger.log("Sound Playing Error: " + e.getMessage());
}
} else {
Logger.log("Sound Not Found");
}
}
public void stopLoopSound(){
soundPool.stop(loopingSound);
loopingSound = 0;
}
This set up works fine, i start the loop when the character starts walking and stop it when it stops walking.
The sound would however stop playing randomly, usually a minute or so after having been used (being turned on and off)...
Has anyone else encountered similar problems with SoundPool and looped sounds?
Reading the documentation on Soundpool clears up a lot:
-playing a sound you pass an int referring to the loaded sound. This method (and others starting a sound playing), soundpool.play(int) returns an int referring to the threadID in which the sound now plays.
-when you want to stop the sound (or the looping sound) you have to use the int of the threadID you just got back when you started playing the sound, NOT the int of the sound itself!
Within the soundpool class then, you set a private int threadIDInt = mSoundPool.play/setloop/whatever(int soundtobeplayed, int/float volumeleft, int/float volumeright, float speed, int loopornot); [note: the arguments are a bit made up; look 'em up]
To then stop the sound (or pause, or set it to loop or not) you pass mSoundPool.stop(threadIDINT);
TLDR: there's a differnce between the int which denotes your sound and the internal int soundpool uses to denote the stream in which your current sound is playing.

Dynamically re-reading a midi file

I am writing an app that dynamically creates a midi file which I then want to play. Each time I press a button in my app a new midi file is created and written to external storage. I then want the app to read that file and play it. I am using SoundPool to play the file. However, from the samples I have found, SoundPool is initialized during onCreate and therefore my file is only read once. Meaning the same file is played everytime I hit the button regardless of any changes I make to it. I have tried moving the SoundPool initialisation into my Audio1ButtonHandler method but then I get no sound or get a selection of “sample x not ready” errors. The relevant code from my app is here:
public void onCreate(Bundle savedInstanceState) {
…………
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUNDPOSITION, soundPool.load("/sdcard/test.mid", 1));
}
public void Audio1ButtonHandler(View target) {
ArrayList<Integer> chord = new ArrayList<Integer>();
chord = Chord.calculateChord(kSelection, cSelection);
for (int i = 0; i < chord.size(); i++) {
System.out.println("chord value is " + chord.get(i).toString());
}
Midi.createMidiFile(chord);
playSound();
releaseSound();
}
public void playSound() {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(SOUNDPOSITION), volume, volume, 1, 0, 1f);
}
public void releaseSound() {
soundPoolMap.clear();
soundPool.release();
}
As you can see I have tried to release the resources (within releaseSound) to try and force them to be re-read but to no avail.
Any pointers would be greatly received.
Thanks
First stop playing before you release! Then, after release, set soundPool to null.

Categories

Resources