Soundpool plays only first 5 secs of file. Why? - android

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.

Related

how can i set mediaPlayer speed for api less then 23

player.setPlaybackParams(player.getPlaybackParams().setSpeed(0.5f));
this requires an api call 23
iam trying to use SoundPool but it is not so efficent as of MediaPlayer Is any alternate to SoundPool for audioPlaying
final float playbackSpeed=1.5f;
final SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
final String path=Environment.getExternalStorageDirectory().getAbsolutePath() + "/folioreader/audio"+".mp3";
final int soundId = soundPool.load(path, 1);
AudioManager mgr = (AudioManager) mHalfSpeed.getContext().getSystemService(Context.AUDIO_SERVICE);
final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()
{
#Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2)
{
soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
}
});
soundPool.pause(soundId)
Use media codec class. This class can decode it and you can use Audio Track on decoded data to render (play) it. But you need to skip the samples as per your speed requirement to play in the particular speed. This approach should work. Refer mediacodec and audiotrack in android developer resources.

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

Streaming audio file to soundpool load() Android

I want to use SoundPool class to create Playback speed effects. To play sound fastly or slowly (1x,0.5x,2x etc).
But the problem is soundpool handles only small files. When i give a large file. It doesn't play anything.
I was wondering if it would be possible to load stream of music (some bytes of my large audio file) and play it via soundpool. any idea.??
Here is my code for soundPool to play song
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;
soundID = soundPool.load(Environment.getExternalStorageDirectory()+"/cm.mp3", 1);
soundPool.play(soundID, volume, volume, 1, 0, 1.0f);
I found JetPlayer and AudioTrack classes, but i m not finding any good tutorials to handle this.
You will need to check file is loaded successfully before playing it using SoundPool.setOnLoadCompleteListener
public void loadSound (String strSound, int stream) {
boolean loaded = false;
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
try {
stream= mSoundPool.load(aMan.openFd(strSound), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Is the sound loaded already?
if (loaded) {
mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}
}
where aMan is
AssetFileDescriptor aMan = getAssets();

android : when using MediaPlayer to play background music, sound effects are stopped prematurely

I'm trying to play background music on my app and occasional sound effects when you kill an enemy and stuff like that.
All the sound effects worked, but then I started using a MusicManager class (similar to this tutorial: http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion) to try to play background music and it works, but the sound effects get chopped after half a second or so.
I'm playing the sound effects using:
MediaPlayer mp = MediaPlayer.create(context, R.raw.fire);
mp.start();
Use soundpool instead.
SoundPool can play multiple streams at once at different volumes, speeds, and looping.
MediaPLayer isn't really meant to handle game audio.
I have 2 games published in the market, and both use SoundPool and have no issues.
here these two functions are taken right out of my game.
public static void playSound(int index, float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
}
public static void playLoop(int index, float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / 3f;
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, speed);
}
that is how easy it is. To take a closer look at this, I only use my playLoop() to play background music, so I lower the volume on it, but you could easily modify the code to manually set the volume each time you play.
also
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, speed);
the first argument mSoundPoolMap.get(index) is just a container holding all of my sounds. I assign each sound a final number such as
final static int SOUND_FIRE = 0, SOUND_DEATH = 1, SOUND_OUCH = 2;
I load thos sounds into those positions and the play them from it. (remember you dont want to be loading all your sounds every time you run one, just load them once.) The next 2 arguments are left/right volume, priority, and then -1 to set to loop.
mSoundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);
this sets my soundpool to 8 streams. the other is the source type and then the quality.
have fun!

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);

Categories

Resources