Sample 1 not ready - Soundpool in Android 2.1 - android

guys i have a audio file which i am reading from sdcard it.On click of a button i am playing the audio file from sdcard.It successfully plays the audio file for 6 to 7 times but after that it shows unable to load (null) sample 1 not ready
i am working in Android 2.1 i.e., API 7.
String path="/sdcard/var/audio.mp3"
sound1 = mSoundPool.load(path,1);
mSoundPool.play(sound1, 1, 1, 1, time - 1, 1);
What is soundpool indicating me by saying unable to load (null) sample 1 not ready?
How can i fix this please help me i am struggling fro long time.

I have had similar problem with a wav file. Let's start discarding some things:
It is NOT a problem of waiting, although it may be in another cases. Please note that you have posted two DIFFERENT error messages like it were one:
unable to load (null)
sample 1 not ready
The first error is raised when you try to load the sample into the SoundPool, the second one when you try to play it. But in this case the second error is clearly a consequence of the first one: the sample could not be ready if it has not been loaded.
So you should concentrate before in the first error.
It is NOT related to MediaPlayer neither, since you are using the SoundPool that is a different thing AFAIK.
So the source of the problem may be, and should be discarded by order:
The file is not there.
The file is there, but it is not readable for some reason.
The file is there, and it is readable, but it is corrupt or not an audio file.
The file is there, and it is readable, and it is a non corrupted audio file, but SoundPool dislikes it.
This last was my case. For some reason SoundPool were unable to load a wav sample that in fact works in every other player that I have used. So I simply ended using another file with a different format. Here is the format of the offending file, as told by mplayer on my GNU/LiNUX box:
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 96000 Hz, 1 ch, s16le, 1536.0 kbit/100.00% (ratio: 192000->192000)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
And here it is the one for the other file that does work:
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 22050 Hz, 2 ch, s16le, 705.6 kbit/100.00% (ratio: 88200->88200)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
There are many differences, but the point is that the second works, so I simply discarded the first one and move into the second. Knowing this if I needed the first sound sample, I just need to adjust its rate and channels with an audio editor like Audacity to solve the problem.
Why it did not just work in first case? Who knows, but if I can solve it so easy... who cares at all?
Regards,

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) {
mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}
});
try {
stream= mSoundPool.load(aMan.openFd(strSound), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

I think that's your problem. Whole file is not loaded and you're trying to play it. You have to wait some time before playing it.

Have you tried calling mediaplayer.release() and re-initilizing? also http://developer.android.com/reference/android/media/MediaPlayer.html

Related

precision of Android MediaPlayer seekTo

I have a number of mp3 files that I use with Android MediaPlayer to play from certain offsets.
Using seekTo() seems to stop at correct location. player.getCurrrentPosition() returns the correct offset, but in some cases the real position is off for as much as 200 ms. The files are about 3 minutes worth of recording and the incorrect offsets seem to appear at the end. Of some of the files.
I have the same effect either trying with Android 4.0.3 device or 4.3 emulator.
Anybody has experience with "finetuning" MediaPlayer offsets? Any experience why MediaPlayer might not be working correctly with some files? They are all CBR, stereo, some have sampling frequency 22050, some 44100, different bitrates.
I'm setting the offsets from another program and saving to mp3 tags, then in case of doubt verifying manually using Audacity. Audacity agrees with my estimate of what the correct offset is, MediaPlayer seems to disagree.
I'm aware that I could use AudioTrack with raw sound files and have a better control, however it might be impractical as there are many mp3 files, so using raw sound data will make pretty large application or many large data files.
The code is nothing fancy:
player.seekTo(start);
player.start();
CountDownTimer timer = new CountDownTimer(length, 100) {
#Override
public void onTick(long millisUntilFinished) {
if (player!=null) setInt(R.id.nLocation, player.getCurrentPosition());
}
#Override
public void onFinish() {
if (player!=null) {
if (player.isPlaying()) {
player.pause();
}
setInt(R.id.nLocation, player.getCurrentPosition());
player.stop();
player.release();
player = null;
}
}
};
timer.start();
I did not manage to find the rule why the MediaPlayer interprets offset (seekTo) differently for a group of MP3 files. For example when creating a new MP3 file with the same parameters from Audacity+Lame (MPEG1, Layer III, 44100 Hz, 192 Kb/s) it worked perfectly.
However:
this can be reproduced - rip MP3 file using Windows Media Player, settings: MP3, 192 kb/s [added when edited]
I found the workaround that seems to work for any recording.
The background - in order to tell MediaPlayer to play from certain offset, I store certain data in MP3 tags. I use a separate program to set up the playback (in frames): Label A, start frame=1000, length=100 frames, Label B, start #1500 etc. Now when I need to play it back, I read the MP3 headers, determine the frame length, for example 26.12245 ms/frame and calculate the offset (1000 frames will be 26122 ms).
The workaround is to store in MP3 tag also the frame count and length in ms (or pass through again and count the frames). Then when start MediaPlayer, compare MediaPlayer.getDuration() (MediaPlayer estimate) with the duration stored in MP3 tag. Then adjust the frame size:
adjustedFrameSizeMs = realFrameSizeMs + (player.getDuration()-storedDurationMs)/storedframeCount;
In my case (for the files with incorrect offset) the adjusted frame length always was between 26.08 and 26.09 ms (instead of 26.12245).
I attempted to try see if this is because Android plays the recording quicker (so it estimates the "real time", not the time according to frame size and frame count). It seems that it really does plays quicker. But even quicker than its own estimate. For example a recording of about 1 hour:
my estimate: 2448 s
MediaPlayer: 2444 s (4 sec difference)
Audacity: 2442 s (here we are in disagreement)
Foobar: 2448 s (another witness that agrees with my estimate :-)
MediaPlayer, real play time: 2438 s
The real playtime was 6 s (0.25%) less than MediaPlayer own estimate. Another attempt on a different sample gave the same percentage difference. However the fact that Audacity and Foobar did not always agree with my estimates, does not let me put all the blame on MediaPlayer.

Soundpool to file

I've got a SoundPool[] that I use to play mixed audio files.
Soundpools play together without any problem, and it's very efficient!
Very briefly:
private SoundPool[] sound = new SoundPool[C.ROWS];
// every sound is initizialized with a load()
// and when asked...
public void play(int row, int variation) {
if (mute[row]) return;
if (variation != 0)
sound[row].play(variation_scheme[row][variation-1], volume[row], volume[row], PRI, 0, 1f);
}
This is just to show! :)
I don't put other pieces of code because everything works very fine and it is not really a problem.
My question, instead, is:
how can I redirect the whole output of the application to a file, instead of (or in addition to) the audio speaker?
In other words, there are two main solutions:
1. how can I instruct AudioPool to play() the sounds to a file instead of to an audio device?
2. or how can I redirect all the audio output of my app (or even of all the phone) to a file?
Thank you.
The only way I found how to record audio is by recording each information of the sound being press such as, duration (using mediaplayer), soundID, millisecond time stamp to do play back to the millisecond, volume, rate (if needed). Then write these to a text file (internally or externally) for later use. Create a play back system that decodes that saved information from text file and organize it in some arrays or hashmaps and play back throught some sort of play back method with maybe using a handler and postDelay to play sound and desired time and then plug in necessary data to play sound from array or hashmap.
In this way you can have your own data files for playing sound sequences.

AudioFlinger could not create track. status: -12

I am programming for android 2.2 and am trying to using the
SoundPool class to play several sounds simultaneously but at what feel like random times sound will stop coming out of the speakers.
for each sound that would have been played this is printed in the logcat:
AudioFlinger could not create track. status: -12
Error creating AudioTrack
Audio track delete
No exception is thrown and the program continues to execute without any changes except for the lack of volume. I've had a really hard time tracking down what conditions cause the error or recreating it after it happens. I can't find the error in the documentation anywhere and am pretty much at a loss.
Any help would be greatly appreciated!
Edit: I forgot to mention that I am loading mp3 files, not ogg.
i had almost this exact same problem with some sounds i was attempting to load and play recently.
i even broke it down to loading a single mp3 that was causing this error.
one thing i noted: when i loaded with a loop of -1, it would fail with the "status 12" error, but when i loaded it to loop 0 times, it would succeed. even attempting to load 1 time failed.
the final solution was to open the mp3 in an audio editor and re-edit it with slightly lesser quality so that the file is now smaller, and doesn't seem to take up quite as many resources in the system.
finally, there is this discussion that encourages performing a release on the objects you are using, because there is indeed a hard limit on the resources that can be used, and it is system-wide, so if you use several of the resources, other apps will not be able to use them.
https://groups.google.com/forum/#!topic/android-platform/tyITQ09vV3s/discussion%5B1-25%5D
For audio, there's a hard limit of 32 active AudioTrack objects per
device (not per app: you need to share those 32 with rest of the system), and AudioTrack is used internally beneath SoundPool,
ToneGenerator, MediaPlayer, native audio based on OpenSL ES, etc. But
the actual AudioTrack limit is < 32; it depends more on soft factors
such as memory, CPU load, etc. Also note that the limiter in the
Android audio mixer does not currently have dynamic range compression,
so it is possible to clip if you have a large number of active sounds
and they're all loud.
For video players the limit is much much lower due to the intense load
that video puts on the device.
I'll use this as an opportunity to remind media developers: please
remember to call release() for media objects when your app is paused.
This frees up the underlying resources that other apps will need.
Don't rely on the media objects being cleaned up in finalize by the
garbage collector, as that has unpredictable timing.
I had a similar issue where the music tracker within my Android game would drop notes and I got the Audioflinger error (although my status was -22). I got it working however so this might help some people.
The problem occurred when a single sample was being output multiple times simultaneously. So in my case it was a single sample being played on two or more tracks. This seemed to occasionally deadlock or something and one of the two notes would be dropped. The solution was to have two copies of the sample (two actual ogg files - identical but both in the assets). Then on each track even although I was playing the same sample, it was coming from a different file. This totally fixed the issue for me.
Not sure why it works as I cache the samples into memory, but even loading the same file into two different sounds didn't fix it. Only when the samples came out of two different files did the errors go away.
I'm sure this won't help everyone and it's not the prettiest fix but it might help someone.
john.k.doe is right. You must reduce the size of your mp3 file. You should keep the size under 100kb per file. I had to reduce my 200kb file to 72kb using a constante bit rate(CBR) of 32kbps instead of the usual 128kbps. That worked for me!
Try
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 50);
tg.startTone(ToneGenerator.TONE_PROP_BEEP, 200);
tg.release();
Releasing should keep your resources.
I was with this problem. In order to solve it i run the method .release() of SoundPool object after finish playing the sound.
Here's my code:
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 50);
final int teste = pool.load(this.ctx,this.soundS,1);
pool.setOnLoadCompleteListener(new OnLoadCompleteListener(){
#Override
public void onLoadComplete(SoundPool sound,int sampleId,int status){
pool.play(teste, 20,20, 1, 0, 1);
new Thread(new Runnable(){
#Override
public void run(){
try {
Thread.sleep(2000);
pool.release();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}).start();
}
});
Note that in my case my sounds had length 1-2 seconds max, so i put the value of 2000 miliseconds in Thread.sleep(), in order to only release the resources after the player have had finished.
Like said above, there is a problem with looping: when I set repeat to -1 I get this error, but with 0 everything is working properly.
I've noticed that some sounds give this error when I'm trying to play them one by one. For example:
mSoundPool.stop(mStreamID);
mStreamID = mSoundPool.play(mRandID, mVolume, mVolume, 1, -1, 1f);
In such case, first track is played ok, but when I switch sounds, next track gives this error. It seems that using looping, a buffer is somehow overloaded, and mSoundPool.stop cannot release resources immediately.
Solution:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mStreamID = mSoundPool.play(mRandID, mVolume, mVolume, 1, -1, 1f);
}, 350);
And it's working, but delay is different for different devices.
In my case, reducing the quality and thereby the file sizes of the MP3's to under 100kb wasn't sufficient, as some 51kb files worked while some longer duration 41kb files still did not.
What helped us was reducing the sample rate from 44100 to 22050 or shortening the duration to less than 5 seconds.
I see too many overcomplicated answer. Error -12 means that you did not release the variables.
I had the same problem after I played an OGG audio file 8 times.
This worked for me:
SoundPoolPlayer onBeep; //Global variable
if(onBeep!=null){
onBeep.release();
}
onBeep = SoundPoolPlayer.create(getContext(), R.raw.micon);
onBeep.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) { //mp will be null here
loge("ON Beep! END");
startGoogleASR_API_inner();
}
}
);
onBeep.play();
Releasing the variable right after .play() would mess things up, and it is not possible to release the variable inside onCompletion, so notice how I release the variable before using it(and checking for null to avoid nullpointer exceptions).
It works like charm!
A single soundPool has an internal memory limitation of 1 (one) Mb. You might be hitting this if your sound is very high quality. If you have many sounds and are hitting this limit, just create more soundpools, and distribute your sounds across them.
You may not even be able to reach the hard track limit if you are running out of memory before you get there.
That error not only appears when the stream or track limit has been reached, but also the memory limit. Soundpool will stop playing old and/or de-prioritized sounds in order to play a new sound.

Getting audio volume during playback

I have some audio data (raw AAC) inside a byte array for playback. During playback, I need to get its volume/amplitude to draw (something like an audio wave when playing).
What I'm thinking now is to get the volume/amplitude of the current audio every 200 milliseconds and use that for drawing (using a canvas), but I'm not sure how to do that.
.
.
.
.
** 2011/07/13 add following **
Sorry just been delayed on other project until now.
What I tried is run the following codes in a thread, and playing my AAC audio.
a loop
{
// int v=audio.getStreamVolume(AudioManager.MODE_NORMAL);
// int v=audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int v=audio.getStreamVolume(AudioManager.STREAM_DTMF);
// Tried 3 settings above
Log.i(HiCardConstants.TAG, "Volume - "+v);
try{Thread.sleep(200);}
catch(InterruptedException ie){}
}
But only get a fixed value, not dynamic volume...
And I also found a class named Visualizer, but unfortunately, my target platform is Android 2.2 ... :-(
Any suggestions are welcome :-)
After days and nights, I found that an Android app project called ringdroid
can solve my problem.
It helps me to get an audio gain value array, so that I can use to to draw my sound wave.
BTW, as my experience, some .AMR or .MP3 can't be parsed correctly, due to too low bitrate...

Android SoundPool sometimes plays sound twice when loop param is set to 0

Here is what I'm doing:
private SoundPool pool;
private int soundId;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// new SoundPool with three channels, STREAM_MUSIC, default quality
pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// load sound with current context, click resource, priority 1
soundId = pool.load(this, R.raw.click, 1);
// originally I wasn't using this but it seemed to help a bit
// set no loop for soundId
pool.setLoop(soundId, 0);
}
private void play()
{
Log.v(TAG, "Play the sound once!");
// half volume L & R, priority 1, loop 0, rate 1
pool.play(soundId, 0.5f, 0.5f, 1, 0, 1);
}
#Override
public void onDestroy()
{
super.onDestroy();
// release the pool
pool.release();
}
Originally I was using a .wav file for the click sound and the problem would occur 90% of the time. I added setLoop and it appears to reduce it a tiny bit.
I thought it might be a problem with loading .wav files so I converted the file to .mp3. Now the problem happens 5% of the time, but it still happens. I built with and without the setLoop call and it appears that including it helps a tiny bit.
As you can see I have added a debug log message to ensure that I am not accidentally calling the play function twice. According to the log output, the sound is only played once but I hear the sound twice.
I have also used several different sound files. Some of the files seem to repeat more frequently than others. I don't see any correlations except it happens more frequently with .wav files.
I see the problem happening on Samsung Continuum running 2.1 (min & target API: level 7). I haven't experienced any extra looping with any market apps I've downloaded on the same device. Unfortunately, I don't have other devices to test with.
I have only found one other person experiencing this issue and he or she was also using a Samsung device.
Here is a link to the other issue reported:
https://stackoverflow.com/q/4873995/695336
At this point I guess I'll try making a release build to see if it still happens and then maybe convert to .ogg format. After that I'll probably try switching to MediaPlayer to see if I get the same results.
Thanks for your help.

Categories

Resources