changing pitch of an sound file and save to mp3 file - android

changing pitch:
soundPool = new SoundPool(50, AudioManager.STREAM_MUSIC, 0);
/** Sound ID for Later handling of sound pool **/
soundId = soundPool.load(this, R.raw.sounds, 1);
play:
streamId = soundPool.play(soundId, 1, 1, 1, 3, pitch);
How can I save the file?

I don't think you can do this with the SoundPool, as it does not allow you to work at a byte level. I think you want to look into using AudioTracks.

Related

SoundPool issue on Android 11

It seems that soundPool Api is not working correctly on android 11. I hear the sound like slow motion. Have anyone any related issue ?
My code
private SoundPool soundPool;
private int wonSound;
...
soundPool = new SoundPool.Builder().setMaxStreams(1).build();
wonSound = soundPool.load(this, R.raw.slot_win_1, 1);
...
soundPool.play(wonSound, 1, 1, 0, 0, 0);
According to the documentation:
https://developer.android.com/reference/android/media/SoundPool#play(int,%20float,%20float,%20int,%20int,%20float)?
SoundPool's function play() takes as its last parameter:
float: playback rate (1.0 = normal playback, range 0.5 to 2.0)
So, playback rates below 1 will sound slower than the original sound. To play your sound in its original playback rate, you should call play() like this:
soundPool.play(wonSound, 1, 1, 0, 0, 1);

SoundPool doesnt make any sound in android

Im trying to play 3 midi files (200Kb each) simultaneously after loading them to the soundPool.
After the play command I unload each stream and release the soundPool.I set the volume to 1 and the phone isnt silence.(Sorry for the language).
There is no crushes or somthing.
What should i do?
thx in advance
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
soundPool.play(sound_id1, leftVolume, rightVolume, 1, 0, rate);
soundPool.play(sound_id2, leftVolume, rightVolume, 1, 0, rate);
soundPool.play(sound_id3, leftVolume, rightVolume, 1, 0, rate);
soundPool.unload(stream_id1);
soundPool.unload(stream_id2);
soundPool.unload(stream_id3);
soundPool.release();
EDIT:
When Im in debug mode and go over the play command it works great.
Tried to add a OnLoadCompleteListener still nothing.
Instead of unloading the sound resources and releasing the SoundPool object immediately after playing, create a Runnable to perform those tasks, and post it with a delay to a Handler. Declare the following:
final Handler handler = new Handler();
Runnable delayedUnload = new Runnable()
{
#Override
public void run()
{
soundPool.unload(sound_id1);
soundPool.unload(sound_id2);
soundPool.unload(sound_id3);
soundPool.release();
}
};
Then post the Runnable after you've played the sound resources:
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
soundPool.play(sound_id1, leftVolume, rightVolume, 1, 0, rate);
soundPool.play(sound_id2, leftVolume, rightVolume, 1, 0, rate);
soundPool.play(sound_id3, leftVolume, rightVolume, 1, 0, rate);
handler.postDelayed(delayedUnload, 5 * 1000);
The second parameter in postDelayed() is the delay in milliseconds; in this case, the delay is 5 seconds. SoundPool does not provide a method to determine when a sound has finished playing, so you will need to adjust the delay accordingly.

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

How to Sync Sounds using SoundPool

I have been trying to get a few sounds to play at the same time; currently i'm using a shared instance of SoundPool. I would like 1, 2 or 3 sounds to be played at the exact same time with no lag.
When calling SoundPool.play(...) X number of times in succession, the sounds are played in that order as one might think. what is the proper what to achieve this where i can prepare all of the sounds to be played at the same time and then play them as one?
Sudo Code:
SoundPool _soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
_soundPool.load(_context, soundId1, 1);
_soundPool.load(_context, soundId2, 1);
_soundPool.load(_context, soundId3, 1);
_soundPool.play(soundId1, vol, vol, 1, 0, 1f);
_soundPool.play(soundId2, vol, vol, 1, 0, 1f);
_soundPool.play(soundId3, vol, vol, 1, 0, 1f);
i done the sound pool for one sound at time it might help you.
Android:sound pool and service
Thank you
You need to understand that SoundPool.load method is asyncronous, so when you call it 3 times in a row, and then call play, there is no garantee that this sounds actually loaded. So you need to wait until all sounds is loaded. For that use OnLoadCompleteListener:
fun loadAndPlay(soundPool: SoundPool, context: Context, resIds: IntArray) {
val soundIds = IntArray(resIds.size) {
soundPool.load(context, resIds[it], 1)
}
var numLoaded: Int = 0
soundPool.setOnLoadCompleteListener { sPool, sampleId, status ->
numLoaded++
if (numLoaded == resIds.size) {
soundPool.setOnLoadCompleteListener(null)
for (id in soundIds) {
soundPool.play(id, 1f, 1f, 1, 0, 1f)
}
}
}
}
To use:
loadAndPlay(soundPool, context, intArrayOf(R.raw.sound_1, R.raw.sound_2, R.raw.sound_3))

Categories

Resources