Sounds do not play when loops set to -1 on Android - android

I'm using a SoundPool to play sounds for my Android app. All of the sounds are loaded up at the beginning of the app to prevent any lagging during the app itself. All sounds play fine as long as they are not set to looping. When i set looping to be true (set as -1) then the sound does not play.
There are 6 different sounds that are stopping and starting depending on the user's input. It plays the first sound fine but then sounds afterwards fail to play.
I have tried various ways around it, such as pausing rather than stopping, setting volume to 0 and loops to 0 rather than pausing, setting loops to an arbitrary large-ish number rather than true repeating, making the sounds much shorter and none of these have worked.
My code is as follows:
public int loadSound(int soundFile)
{
int soundID;
try
{
if (m_loadedSounds.containsKey(soundFile))
{
soundID = m_loadedSounds.get(soundFile);
}
else
{
soundID = m_soundPool.load(m_context, soundFile, 1);
m_loadedSounds.put(soundFile, soundID);
}
}
catch (Exception e)
{
soundID = -1;
}
return soundID;
}
public void playSound(int soundFile, boolean loop)
{
// Grab it from the map
int soundID = loadSound(soundFile);
int loops = loop ? -1 : 0;
float streamVolume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume /= m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// If succeeded then play the sound
if (soundID != -1)
{
if (m_soundStreams.containsKey(soundID))
{
int streamID = m_soundStreams.get(soundID);
m_soundPool.setLoop(streamID, loops);
m_soundPool.resume(streamID);
}
else
{
int streamID = m_soundPool.play(soundID, streamVolume, streamVolume, 1, loops, 1.0f);
Log.d("SOUNDS", "Sound played! ID: " + soundID + " At volume: " + streamVolume + " Looping: " + (loop ? "Yes": "No") + " Success: " + (streamID != 0 ? "Yes" : "No"));
if (streamID != 0)
{
m_soundStreams.put(soundID, streamID);
}
}
}
}
public void stopSound(int soundFile)
{
int soundID = loadSound(soundFile);
Integer streamID = m_soundStreams.get(soundID);
if (streamID != null)
{
m_soundPool.pause(streamID);
}
}
And the error given in LogCat at run time is:
01-03 16:03:20.142: ERROR/AudioFlinger(2359): not enough memory for AudioTrack size=670096
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): AudioTrack (0x389a0, size=1048576)
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 0: 0005b090 | 0x00000000 | 0x00010080 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 1: 0006db58 | 0x00010080 | 0x0007B8A0 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 2: 0005af70 | 0x0008B920 | 0x0005C280 | A
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): 3: 000752c0 | 0x000E7BA0 | 0x00018460 | F
01-03 16:03:20.142: DEBUG/MemoryDealer(2359): size allocated: 883488 (862 KB)
01-03 16:03:20.142: ERROR/AudioTrack(11584): AudioFlinger could not create track, status: -12
01-03 16:03:20.142: ERROR/SoundPool(11584): Error creating AudioTrack
Does anybody know of any solutions to this annoying problem, or any workarounds that I haven't tried?

The best solution I could come up with is to put a special case in for the sounds that are doing this and use a MediaPlayer for these sounds. Then I have to stop & release them when they're not used and then reload them when I want to play them again. Has a noticeable, yet slight, loading time when the sounds change but it's the best I can do. Any other answers would still be much appreciated.

Make sure you are releasing your SoundPool resources in onPause() to prevent leaking memory. You should be loading your sounds in onResume() also.

I had the same problem. When you are begining to play the sound you want to be looped, set priority of sound to 99 and lower the rest. The heigher value the heigher priority. However I have a big problem of latency when playing sounds. If you have any solution please notify me.(I'm using soundpool too and int array as ID pool.)
Which I mean:
int streamID = m_soundPool.play(soundID, streamVolume, streamVolume, 99, loops, 1.0f);

SoundPool soundPool;
int soundID;
boolean plays = false, loaded = false;
float actVolume, maxVolume, volume;
AudioManager audioManager;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actVolume / maxVolume;
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
}
});
soundID = soundPool.load(this, R.raw.audiofile, 1);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
soundPool.play(soundID, volume, volume, 1, -1, 1f);
}
},1000);
Try this solution. its should work.

Related

Android soundpool unexpectedly making the application close with no message when lots of sounds played

In my application I have a makesound(int) method that is called when i want sounds to be made at different stages of the game.
I have created the audiomanager and soundpool variable public and intialized the sound pool and the audio manager in the OnCreate method.
audioManager = (AudioManager) this.getSystemService(this.AUDIO_SERVICE);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
My makesound(int) method is below
private void makeSound(int w){
if(onVolume) { //onvolume is a public boolean var that is set
final int volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
int id;
switch(w){
case 1:
id = soundPool.load(this, R.raw.metalimpact, 1);
break;
case 2:
id = soundPool.load(this, R.raw.swoosh, 1);
break;
case 3:
id = soundPool.load(this, R.raw.win, 1);
case 4:
id = soundPool.load(this, R.raw.win, 1);//second chance
break;
case 5:
id = soundPool.load(this, R.raw.fanfare, 1);//second chance
break;
default:id = soundPool.load(this, R.raw.metalimpact, 1);
}
final int soundID = id;
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(soundID, volume, volume, 0, 0, 1f);
}
});
}
}
This was working fine when I had just one sound to be played. But after I set the switch case stmt and had a lot of sounds playing, I noticed that the application would run fine for sometime, but suddenly crash with no message or anything. Am i loading the Soundpool too much?..Please let me know what i'm doing wrong. Thankyou!!
You are using wrong way to implement your logic.
Firstly You are not required to load sounds each time in makeSound(int w) method. Instead Load all sound first and then manage them whether they are loaded or not in LoadCompleteListener.
HashMap<Integer, Integer >loadedSound=new Map<>();
loadedSound.put(1,soundPool.load(this, R.raw.metalimpact, 1));
loadedSound.put(2,soundPool.load(this, R.raw.swoosh, 1));
loadedSound.put(3,soundPool.load(this, R.raw.win, 1));
loadedSound.put(4,soundPool.load(this, R.raw.win, 1));
loadedSound.put(5,soundPool.load(this, R.raw.fanfare,1));
Now you have soundIds return by load method,use then to play sound as
switch(w)
{
case w: soundPool.play(loadedSound.get(w), volume, volume, 0, 0, 1f);
break;
default:soundPool.play(loadedSound.get(1), volume, volume, 0, 0, 1f);
}

android sound pool not appear

I want to add a sound effect when a user clicks on a button.
I tried this:
case R.id.b_all_addresses_addAddress:
// 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;
if (loaded)
soundPool.play(soundID, volume, volume, 1, 1, 1f);
else
Toast.makeText(this, "could I hate you God more?", Toast.LENGTH_SHORT).show();
in my on create I did this:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
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.thip, 1);
the sound is just 6 KB.
My Problem
I can't hear the voice
what values are you getting back for volume?
They should be between 0.1 and 0.99. Try using 0.5 as a hard-coded setting and see whether that works. I vaguely remember someone here once reported a "bug" in SoundPool which meant that 0 and 1 meant "mute" and your volume had to be between these two values in order to be heard.
Also: What format is your sound in? not all phones can play all the formats. Some need ogg, some are fine with WAV.
This here works for me on HTC Sensation XE, Android 4.03:
*WAV
*2 Channels
*16 bit PCM
*44.1kHz

Soundpool plays only first 5 secs of file. Why?

I use Soundpool in my app, so far it works good, but I do have a wav file which is 10 secs. Unfortunately, soundpool plays only the first 5 secs. How to make soundpool to play the whole track? I have converted wav to -- ogg and mp3 still the same issue. It plays only the first 5 secs. Any help would be much appreciated.
//set up audio player
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
//load fx
mSoundPoolMap.put(RAW_1_1, mSoundPool.load(this, R.raw.loop1, 1));
//playing soundpool
case R.id.button1:
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
UPD Last: Maybe someone will find it here and read it. Seems soundpool cant play more then 5 secs. It is his maximum, for more longer sounds use MediaPlayer. I hope you will not spend so much of your time like i did)
So I think you reached the 1M limit in SoundPool.
SoundPool is hard code the buffer size as 1M, for all loaded file, store in pcm format.
So it do not care of ogg or wav.
We have solved similar problem by decreasing in our *.ogg effects sample rate. Our initial sample rate was 44 kHz ~ and only 10 sec of sound played, decreasing to 16 kHz increase playability to 30 seconds
Solution was found in this discussion
SoundPool designed to play short sound effects. To play music (big audio files) you need to use MediaPlayer
// R.raw.audio_file - res/raw/audio_file.mp3
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
This activity is works for me
public class MainActivity extends Activity {
private Integer soundID;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
final float volume = actualVolume / maxVolume;
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
try {
Thread.sleep(5000); // play twice
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
soundPool.play(soundID, volume, volume, 1, 0, 1f);
}
});
}
});
soundID = soundPool.load(this, R.raw.sound2, 1);
//load fx
//playing soundpool
}
}
You must load you sound asynchrony and check audio cache - it can be overflow.
Read this article
http://www.google.by/url?http://www.vogella.com/articles/AndroidMedia/article.html
Its very heedful
Your file is probably too large. Try to change from stereo to mono, if you do not need stereo anyway.
Or try to downsample your file.
Reducing the filesice worked in my case.

Android play music in a random amount of time

Now I can play a music in android. But I want to play this sound in a random amount of time between 2 to 8 seconds.How Can I randomly play it that for example the first time, it plays for 2 seconds, the next time 7 sec and so on? Can anybody help me?
Go through these links:
Random number Generation
Media Player
Timer
you will get an idea.
Sound in android is played like this :
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int mSoundID = mSoundPool.load(this, R.raw.sound1, 1);
float lActualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float lMaxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float lVolume = lActualVolume / lMaxVolume;
// Is the sound loaded already?
if (mSoundIsLoaded) {
mSoundPool.play(mSoundID, lVolume, lVolume, 1, 0, 1f);
I think you have been given plenty of help to figure the random number part out.
You will have to put the sound file in your assets/raw directory.
edit:
I forgot to mention where the mSoundIsLoaded parameter came from.
I set it when my sound has been loaded. I do this in my onCreate method. when the sound is loaded I set the boolean field called mSoundIsLoaded. I do this to prevent NullPointerExceptions when playing the sound
the loading of the sound looks like this:
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
mSoundIsLoaded = true;
}
});
mSoundID = mSoundPool.load(this, R.raw.sound1, 1);

Soundpool sample not ready

I have a .wav file that I'd like to use across my game, currently I am loading the sound in onCreate() of each activity in the game.
soundCount = soundpool.load(this,R.raw.count, 1);
The sound will be played once the activity starts.
soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);
Problem is at times I will hit the error "sample x not ready".
Is it possible to load the .wav file once upon starting the game and keep it in memory and use it later across the game? Or is it possible to wait for 1-2 seconds for the sound to load finish?
You'll need to wait for it to finish by adding a listener via SoundPool.setOnLoadCompleteListener.
Since my project is compatible with Android 1.5 and I couldn't use setOnLoadCompleteListener, I resolved making the play of sound delayed.
My source code follows:
playSound_Delayed(soundId, 100);
// (..)
private void playSound_Delayed (final int soundId, final long millisec) {
// TIMER
final Handler mHandler = new Handler();
final Runnable mDelayedTimeTask = new Runnable() {
int counter = 0;
public void run() {
counter++;
if (counter == 1) {
boolean ret = mHandler.postDelayed(this, millisec);
if (ret==false) Log.w("playSound_Delayed", "mHandler.postAtTime FAILED!");
} else {
playSound(soundId);
}
}
};
mDelayedTimeTask.run();
}
you should use setOnLoadCompleteListener if possible ... if not, wrap a while loop around your call to 'play'. something like:
int waitLimit = 1000;
int waitCounter = 0;
int throttle = 10;
while(soundPool.play(soundId, 1.f, 1.f, 1, 0, 1.f) == 0 && waitCounter < waitLimit)
{waitCounter++; SystemClock.sleep(throttle);}
this will retry 'play' 1000 times on a 10ms interval. this should be run on a non-ui thread of course, and is still not ideal. but maybe a little stronger than waiting an arbitrary time and expecting the pool to be ready.
The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream which we simply can call preparation of Stream, that takes some times depending on size of sound file. And if we try to play the sound before that process is over, we get "sample x not ready" error.
There are two solutions for this
implement setOnLoadCompleteListener or
wait arbitrary amount of time so that preparation is over
and then play it
Note that there is no exception for this condition or application is not crashing without any try-catch
private SoundPool soundPool;
private int my_sound;
boolean loaded = false;
// In the constructor
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
my_sound = soundPool.load(this, R.raw.blast_sound, 1);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
loaded = true;
}
});
// then where ever you want to play the sound, type
if (loaded) {
soundPool.play(my_sound, 0.9f, 0.9f, 1, 0, 1f);
}
I solved with problem with simple do-while cicle. The method play() return non-zero streamID if successful, zero if failed. So, it's sufficient to check the return value.
int streamID = -1;
do {
streamID = soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
} while(streamID==0);
This would work for you definitely !
public void playWavFile() {
Thread streamThread = new Thread(new Runnable() {
#Override
public void run() {
SoundPool soundPool;
final int wav;
String path = "/mnt/sdcard/AudioRecorder/record.wav";
soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC, 0);
wav = soundPool.load(path, 1);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
// TODO Auto-generated method stub
soundPool.play(wav,100, 100, 0, 0, 1f);
}
});
}
});
streamThread.start();
}
This sounds crazy, but don't play sounds right away. Give your app a couple of seconds to initialize the SoundPool. In my app, this was exactly the issue; I added a fake "loading" screen of ~3 seconds, and then everything worked. (I didn't even need to preload the sounds.)

Categories

Resources