I get popping sounds when simultaneously playing multiple SuperpoweredAudioPlayers. When one SuperpoweredAudioPlayer is playing a moderately loud sound, a popping sound gets made when I call play() on a second SuperpoweredAudioPlayer, even if there is initial silence in the second player and the process() volume is set to 0. I can't have pops in the output since I'm making a music production app. The output also needs to be generated in real time. Am I doing something wrong or is there a workaround? Would it be fixed by having the players in separate threads?
Separate threads are definitely not the solution, as all your audio processing should happen on the same thread. Two audio players are very low with CPU, you don't have a processing problem.
Perhaps you forgot to handle the return value of the process() function. If it's false, then it means silence, and the buffer is not updated (zeroed) at all.
Related
I'm trying to loop a ~30 seconds audio track multiple times. I'm using a MediaPlayer to play the sounds, here is the code where its created:
mAnimalMediaPlayer = MediaPlayer.create(mContext, fish.getSound());
I then call:
mAnimalMediaPlayer.setLooping(true);
mAnimalMediaPlayer.start();
when a play button is clicked. When the track loops, there is a small but noticeable pause between the track ending and the new loop beginning. I am positive that the audio file im using does not contain any actual pause.
Here is an android bug report which has been up for some time: https://code.google.com/p/android/issues/detail?id=18756
I've tried a few workarounds (such as using .ogg files instead of .mp3) but the problem persists.
I'm looking for any possible workaround that will allow me to seamlessly loop the playback multiple times.
There is a very noticeable pause. However, I have just discovered through experimentation on an emulator running level 25 that if one prepares two identical players and alternately starts them one after the other when the other has finished then the gap becomes much more difficult to hear.
I am trying to make an app that plays a sound like a piano organ. When the user presses a button it will play a piano-like sound. The difficulty is that the user could hold their finger down for any length of time. When they lift their finger off the should fade rather than stop abruptly.
I've played with SoundPool and Media Player but they don't seem suitable.
There are a lot of piano apps out there... does anyone know how they solve this problem?
You'll want to use the AudioTrack class. The basic usage is to write your sound data using AudioTrack's write() method for whatever duration that you want it to be heard. You'll need to use a separate thread for each simultaneous voice that you want to "play".
Here's the AudioTrack reference page and here's a blog entry where the author does a nice job of showing what to do without a lot of distractions.
i understand that there are some issues why android can't ply low latency audio and has a >100ms delay on everything (well.. actually vibrations are faster as audio!!! Shame on you!).. but is there some possibility to figure out how much earlier i need to run the sound to actually be on time?
e.g. how to calculate audio delay?
Im creating a rhythm game and i need to play "ticks" in sync with music.
Im using libGDX Sound - e.g. sound pool - play() now.
Any suggestions?
Your app could emit a sound with the speaker and then use the microphone to detect the sound emited by itself (something similar to remote.js).
Even though there are many variables involved (the mic will also have a latency), you can make the device calibrate it self by continuously trying to guess how long the sound will take to be detected, and emiting it again and again until your guess gets "fine tuned".
Since my last question hasnt been answered where I used simple medaiplayer (Sound sometimes remains silent when playing more sounds in a row, why?) and tried to create the whole thing using SoundPool. But guess what, almost same thing happens but with an error
AudioFlinger could not create track, status: -12
Error creating AudioTrack
I read about that at most 32 sound can be stored in the memory of soundpool then I will get the error. Thats right, after the 32th sound I cannot play any of the sounds since I always get the error. So how could I use more than 32 sounds with SoundPool? I tried to use 3 Soundpools with 20 sounds in each of them, and I always unload all the sound from the other pools with this:
mSoundPoolMapV1.clear(); //clearing HashMap
for(int i=1;i<15;i++){
mSoundPoolV1.unload(i);
}
But same error comes again. (I use 22k sounds instead of 44.1k, I read this could solve it but nothing happened)
Any suggestions appreciated. What the hell is that I cannot play about 50 sounds neither with mediaplayer and nor with soundpool? How is that possible??
With release() :
mSoundPoolMap.clear();
for(int i=1;i<50;i++){
mSoundPool.unload(i);
}
mSoundPool.release();
mSoundPoolMap.put(1, mSoundPool.load(Main.this, R.raw.sound1, 1));
...
mSoundPoolMap.put(50, mSoundPool.load(Main.this, R.raw.sound50, 1));
SO I clear the hashmp, then unload all the sounds, then I release the soundpool. Then I fill the hashmap again, but now It gives me no sound at all. I must miss something, could you tell me what?
There's a limitation in the AudioMixer that only allows 32 AudioTracks at the same time. The SoundPool will try to create an AudioTrack whenever it starts playback of one of the channels (I guess this corresponds to a sample) belonging to the pool. However, the track is not destroyed until you destroy the SoundPool, so the track name will remain allocated even if it has been stopped.
You could try calling release() on your SoundPool. That should cause the native SoundPool object to be destroyed, thereby destroying all AudioTracks it has created and deallocating their names in the AudioMixer.
Underlying audio hardware is not unlimited. You can only hold so many sounds in memory at a time. When I made a 3D audio library for desktops the limit was variable so I had to query the limit from the hardware before starting or it would lead to seemingly random failures. You should have a single SoundPool and just load and unload sounds into as you need them. Then you need to set up some kind of a priority for the sounds that should be playing so you can make sure that the sounds you really need don't get unloaded to play an optional background noise.
The SoundPool Documentation actually has a really nice description of a typical use-case.
I'm writing my first android app, trying to playback two 10min soundfiles synchronously (imagine an instrumental track and an acapella), to be able to change the volume of each track independently). I am using two MediaPlayers for this, since a SoundPool is targeted at shorter audio samples as far as I read.
Now my problem is that, when pausing and resuming the playback, sometimes the players are not synchronous anymore, even though I set their positions to the same value before resuming playback.
I know that this is kind of inevitable, because they cannot be started at exactly the same moment and they may require different amounts of time for starting playback, but: Is there maybe any other approach to meet my requirements?
You can take a look at JetPlayer, this may accomplish what you want as far as synchronization.
To use it you create audio channels (your instrument channel and vocal channel) as MIDI files in a track and the player can keep them synchronized while allowing you to mute or unmute the different channels as appropriate.
The user guide for creating JET resouces can be found here.