I am using soundpool to play sound but i observe that sound gets cut off. Only half of sound plays. I dont know what is happening.
I am using sound when i stretch the arrow and when it touches any object it plays.I am sharing code
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool sp, int sampleId, int status) {
soundPool.play(sampleId, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
soundPool.load(GameActivity.this, R.raw.btn_click, 1);`
Anyone aware about this.
SoundPool is used to play small length of sounds. It have maximum buffer limit of 1MB. It decompresses audio file to pcm format, which depends on sampling rate and length of song. So your file may not need to be actually of 1MB.
For lengthy sounds you can use android's MediaPlayer.
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
Related
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).
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.
Hi I am working on an music app for Android.In that I want to change playback speed & reverb value for music dynamically.
In my view there will be two SeekBar for both playback speed & reverb value, so on changing SeekBar's value sound must change accordingly.
Currently I am using MediaPlayer but after search I found the MediaPlayer doesn't support functionality for changing playback speed.
Then I tried with SoundPool, with this I am able to changes the speed of track.But with SoundPool I am not able to use the big music files.
I want a way where I can change both speed & reverb value.
I am searching for weeks for solution but still I am not able to find it.
Please please help me...
EDIT
float playbackSpeed = 1.5f;
SoundPool soundPool;
int soundId;
void soundPooler()
{
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
final int soundId = soundPool.load("/storage/sdcard0/Music/departed.mp3", 1);
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
#Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2)
{
soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
}
});
}
This is the code I have tried.
Thanks..
I have a class that extends Service, where i have a MediaPlayer that manages the background music of my app.
MediaPlayer player = MediaPlayer.create(this, R.raw.background_music);
player.setLooping(true);
The problem is that it doens't "loop" well: I mean, when the mp3 file ends, there's a second of silence before it starts again.
But, in the mp3 file, actually, there's not any second of silence.
How can i solve this problem?
You can't. This bug has been there for ages now, but they still didn't fix it and it does not look like they are going to do it in the close future!
http://code.google.com/p/android/issues/detail?id=18756
You can try to use SoundPool to play your audio file (it loops very well and without delay, however might be inappropriate for some audio resources).
Here is some kind of implementation (deprecation removal and appropriate closing of resource is left as a homework :))
SoundPool pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
afd = getResources().openRawResourceFd(audioResource);
pool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (0 == status) {
soundPool.play(sampleId, 1f, 1f, 10, -1 /* looping parameter */, 1f);
}
}
});
pool.load(afd, 10);
afd.close();
From other side if MediaPlayer is a must have you might look into method setNextMediaPlayer. So the idea is to initialize several MediaPlayers for the same file (what a mess) and set them in a smart way.
I have a class called Sound.java that consists of 3 functions (initSound, addSound, and playSound).
public static void initSound(Context con) {
mContext = con;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
public static void addSound(int index, int SoundID) {
soundPoolMap.put(index, soundPool.load(mContext, SoundID, 1));
}
public static void playSound(int index) {
soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
I called the initSound and addSound in the MainGame.java constructor.
Sound.initSound(getContext());
Sound.addSound(1, R.raw.machine_gun);
and called playSound inside a Thread (looping) in MainGame.java. PlaySound called every second when an event is triggered (for example, when an enemy is in sight, troops (more than one) will shoot (play the sound) continuously until the enemy is dead).
Sound.playSound(1);
The problem is when the sound plays, the app is slowing down.
I'm using soundpool because as far as I know for sound effects, soundpool is better than mediaplayer. (I've tried mediaplayer and the lag is even more.)
The sound file that I use is .wav (unsigned 8bit PCM, 1 Channel, 8000hz) with size 5.44KB.
Is there a better way to play effect sounds without slowing the game performance, or is mine wrong? I really appreciate any ideas and responses.
According to the SoundPool docs, it decodes to 16-bit PCM, so you could change to that and see if you get some performance out of that. Other than that, your code seems pretty similar (at least as far as I can remember) to stuff I've done before and I didn't see any significant perf problems (I believe I wasn't even using WAV, I was just using OGGs, but I don't remember for sure). Are you trying this on an actual device, or just the emulator?
I had the same problem. Then I've created a new thread for the soundpool and added 20ms sleep into the while loop. The problem is gone.