I found many questions regarding Soundpool but they all state that it is not working at all. for me the case is different.
i have declared a soundpool object and track ids as follow
public static SoundPool sp;
public static int comeCloseID, goFurtherID, blinkID, smileID, rotateLeftID, rotateRightID;
then i have created a initSounds function to initialize the sound pool
public static void initSounds()
{
sp = new SoundPool.Builder().build();
comeCloseID = sp.load(mContext, R.raw.comeclose,1);
goFurtherID = sp.load(mContext, R.raw.gofurther,1);
blinkID = sp.load(mContext, R.raw.blink, 1);
smileID = sp.load(mContext, R.raw.smile, 1);
rotateLeftID = sp.load(mContext, R.raw.rotateleft, 1);
rotateRightID = sp.load(mContext, R.raw.rotateright, 1);
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
}
then i paly the sounds as follows:
GlobalParams.sp.play(GlobalParams.comeCloseID, 1, 1, 1, 0, 1);
etc...
the audio files paly well using my samsung note5 device. but when i try them on a J1 device or a lenovo device the sounds wont play. (thogh i can log the value of loaded to be true).
what have i missed? and why the audio isn't playing?
Knowing that I tried using different formats for the audio (WAV, MP3....) but id didnt solve the case.
Try increasing setMaxStreams(5) to say 5, you have it set to default of 1:
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build();
sp = new SoundPool.Builder()
.setMaxStreams(5)
.setAudioAttributes(attributes)
.build();
Related
Solved: I forgot the track.play(); at the end...
I want to play a sound on my Android Smartphone (4.0.4 Api level 15).
I tried to hear some random noise, but its not working:
public class Sound {
private static int length = 22050 * 10; //10 seconds long
private static byte[] data = new byte[length];
static void fillRandom() {
new Random().nextBytes(data); //Create some random noise to listen to.
}
static void play() {
fillRandom();
final int TEST_SR = 22050; //This is from an example I found online.
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
track.write(data, 0, length);
}
}
I have played a little bit with the variabels, but could not get it to work.
All you have left to do is play it. Add this line to the end of your play() function:
track.play();
I am using the ToneGenarator in order to play a continuous beep.
The problem is that the volume is far too low!
We can't hear anything when the volume is lower than 8.
And with the maximum volume, it is not loud at all...
Is it a limitation of the DTMF sound or am I doing something wrong?
Here is the code:
private final int TONE_TYPE = ToneGenerator.TONE_DTMF_5;
private final int STREAM = AudioManager.STREAM_MUSIC;
private final int DOT_TIME = 3;
public SoundManager(Activity anActivity) {
audio = (AudioManager) anActivity
.getSystemService(Context.AUDIO_SERVICE);
generator = new ToneGenerator(STREAM,
audio.getStreamMaxVolume(STREAM));
}
private void playBeep() {
generator.startTone(TONE_TYPE, DOT_TIME);
}
Use this one, For using Current System Volume.
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
I found where the problem was:
ToneGenerator takes a volume between 0 and 100, while the stream volume is between 0 and 15.
Thus, audio.getStreamMaxVolume(STREAM) gives 15 out of 100, it is low...
I have a button that when pressed sounds a short sound effect with SoundPool. the file is a 16k mp3.
The sound effect works for hundreds of button clicks, but at some point i get this error (and the sound isn't playing for clicks anymore):
E/AudioFlinger( 34): no more track names available
E/AudioTrack( 904): AudioFlinger could not create track, status: -12
E/SoundPool( 904): Error creating AudioTrack
W/AudioFlinger( 34): write blocked for 81 msecs, 4573 delayed writes, thread
xb3f0
onCreate:
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) {
// TODO Auto-generated method stub
loaded = true;
}
});
//save the effect somewhere:
MyUtil.regularSound = soundpool.load(this, R.raw.regular_click,1);
onClick:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVol = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVol = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float vol = actualVol/maxVol;
if (loaded) {
soundpool.play(MyUtil.regularSound,vol,vol,1,0,1f);
}
before starting another intent:
Intent i = new Intent(con, EndScreen.class);
if (soundpool != null) {
soundpool.release();
soundpool = null;
}
startActivity(i);
I just spent hours with the same problem.
The -12 error means that sound allocated to the app is out of memory. using .finish() doesn't release the memory of SoundPool. You have to use .release().
In my app I have a soundPool.release() on the onclick method that starts the next activity,
Changing the sound formats didn't work for me either.
Im getting the same error. The thing I did was to try to resample the track, convert it to OGG format and change the quality to 0. I did all this with Audacity
You try code it: here
/*
private SoundPool mSoundPool;
private SparseArray<Integer> mSoundPoolMap;
private AudioManager mAudioManager;
*/
public static void clear() {
if (mSoundManager != null) {
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}
I play a sound using:
public class NotificationFunction implements FREFunction {
public FREObject call(FREContext context, FREObject[] args) {
Context appContext = context.getActivity().getApplicationContext();
AudioManager mAudioManager = (AudioManager)
appContext.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.loadSoundEffects();
mAudioManager.playSoundEffect(0);
It plays a default sound effect, how can I get it to play a custom sound effect.
Also I have tried to implement sound pool but it doesnt work, I implemented it the standard way but it doesnt work. Any ideas why? Or if I can use the audio manager for sounds instead?
Check with following code
using (AudioManager am= (AudioManager)WebViewActivity.Instance.GetSystemService(Context.AudioService))
{
int maxVol = am.GetStreamMaxVolume(Stream.Music);
float myVol= maxVol * volume;
am.SetStreamVolume(Stream.Music, (int)(myVol), 0);
using (SoundPool soundPool = new SoundPool(1, Stream.Music, 0))
{
int soundFileId = soundPool.Load(soundFilePath, 1);
soundPool.LoadComplete += (sender, eventArgs) =>
{
using (SoundPool soundPoolLoaded = sender as SoundPool)
{
soundPoolLoaded.Play(soundFileId, myVol,
myVol, 0, 0, 1.0f);
}
};
}
}
I am writing an app that dynamically creates a midi file which I then want to play. Each time I press a button in my app a new midi file is created and written to external storage. I then want the app to read that file and play it. I am using SoundPool to play the file. However, from the samples I have found, SoundPool is initialized during onCreate and therefore my file is only read once. Meaning the same file is played everytime I hit the button regardless of any changes I make to it. I have tried moving the SoundPool initialisation into my Audio1ButtonHandler method but then I get no sound or get a selection of “sample x not ready” errors. The relevant code from my app is here:
public void onCreate(Bundle savedInstanceState) {
…………
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUNDPOSITION, soundPool.load("/sdcard/test.mid", 1));
}
public void Audio1ButtonHandler(View target) {
ArrayList<Integer> chord = new ArrayList<Integer>();
chord = Chord.calculateChord(kSelection, cSelection);
for (int i = 0; i < chord.size(); i++) {
System.out.println("chord value is " + chord.get(i).toString());
}
Midi.createMidiFile(chord);
playSound();
releaseSound();
}
public void playSound() {
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;
soundPool.play(soundPoolMap.get(SOUNDPOSITION), volume, volume, 1, 0, 1f);
}
public void releaseSound() {
soundPoolMap.clear();
soundPool.release();
}
As you can see I have tried to release the resources (within releaseSound) to try and force them to be re-read but to no avail.
Any pointers would be greatly received.
Thanks
First stop playing before you release! Then, after release, set soundPool to null.