Android Question SoundPool - Sound played twice

LucaMs

Expert
Licensed User
Longtime User
In a project containing some AnimationSet (library AnimationPlus) I use a SoundPool object to play a sound.

Installed on a device with Android 4.4.2 the sound is correctly played once,
on a device with Android 4.0.4, twice.

I have developed a sample project (pay no attention to the stupid animations :)) - attached.

Has this happened to other members?


Thank you
 

Attachments

  • SoundPool Test.zip
    21.7 KB · Views: 368
Last edited:

LucaMs

Expert
Licensed User
Longtime User
The real intention was to play a sound for each animation; then, given that I obtained a single sound (and I don't know why, of course), I used a different sound file, for the whole group of animations.
This seemed to work, but when I checked the graphics on a different device... that problem.

Thank you, Erel
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't understand your code.

Why are you calling Play multiple times if you want it to play once?
B4X:
For A = 0 To NumOfAnim - 1
     AnimSets(A).Start(Images(A))
     mSoundPool.Play(mMySoundLoadID, 1, 1, 1000, 1, 1)
   Next

I tried to explain why in #3.

The goal is to move N Views, each with its own sound. The code would then be that.

During the tests, using that code but with the addition of other effects, it seemed that the tablet was playing only one sound; so I preferred to use a different sound file, a file that represented the "group" sound.

Anyway, the last attachment produces a sound on the tablet for which 3 views seem to be in motion (even if they are 5 such as sounds called); same thing on my smartphone but the "triple sound" is played twice, one after the other.

Thank you



P.S.
https://xenforo.com/community/resources/audio.3459/

:)
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Hi,

I have tested your code and did the same thing when exit with the back button. Change some things and now for me it works.

Try to change the initialization to Activity_Resume and free it in Activity_Pause
B4X:
Sub Activity_Resume
    'If Not(mSoundPool.IsInitialized) Then
        mSoundPool.Initialize(5)    'As many as you want to be played at the same time
        mMySoundLoadID = mSoundPool.Load(File.DirAssets, "notify.ogg")
    'End If
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    mSoundPool.Unload(mMySoundLoadID)   'Without this line, does not work
    mSoundPool.Release
End Sub

About the repetition, I can't reproduce it. But if you do mSoundPool.initialize(1), you are saying that only one sound can be played at the same time.
If in your routine you launch it 5 times (even if it is the same), perhaps depending on the OS, device speed or others, one of them is queued when the other has already started playing

Also, regarding Soundpool, it has never been clear to me if the "Loops" parameter in SoundPool.Play(...) has to be 0 or 1 for no-repetition. If the above does not work perhaps the difference is here.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I have tested your code
AOS 4.0.4?

Moving the initialization to the Resume event, sounds are played also when I "restart" the app (Back key and start again), as you suggested.
Anyway I don't know why writing:
B4X:
Sub Activity_Create
    If FirstTime then
        mSoundPool.Initialize(1)
    End If
End Sub

Sub Activity_Resume
    log(mSoundPool.IsInitialized)
End Sub

this does not create problems on my tablet but on my phone; mSoundPool is initialized, also when the app is restarted, in both devices.

The "sounds serie" is still repeated twice, unfortunately; is the problem my bad phone or AOS 4.0.4?

Also, regarding Soundpool, it has never been clear to me if the "Loops" parameter in SoundPool.Play(...)
Loops stands for "repetitions", I think. If you set it to -1, the sound will be played continuously (endlessly - damn English :D).



[I'm thinking that when you want to develop you should pray, before :D]
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
:eek::D

Testing again.

The problem was exactly that parameter, Loop; it means, I suppose now, the number of repetitions AFTER the first playing. Then, set to zero, it plays once, 1 = twice, and so on.
In my project I set it to 1 and this is why on my smartphone I get two series of sounds; but the "error" is on my tablet, then, which plays one series only!

In addition, the initialization does not convince me.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The animations are asynchronous. You need to handle the animation complete event if you want to do something after an animation completed.

Thank you, Erel, but I don't think could be this the problem, different behavior in the two devices.

As I wrote, the goal is to move N Views, each with its own sound... asynchronously:
View1 (animation) starts with its sound; after few milliseconds...
View2 (animation) starts with its sound.
Each sound should play during its related animation (translation).

To be clear, I'm trying to draw and move coins on a table.



The second question (that I discovered during these tests) is the absence of sound if I initialize the SoundPool object in Acitivity_Start, depending on the state of FirstTime; the object is always properly initialized but the sounds are not played even if I don't get errors ("Please, start a new thread for this question"? :)).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
View1 (animation) starts with its sound; after few milliseconds...
View2 (animation) starts with its sound.
This is not what you've implemented. You are starting all the animations at once.

You have also initialized SoundPool with MaxStreams set to 1 so it will only play a single sound at once.

About the absence of sound. Have you declared SoundPool as a process global variable?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This is not what you've implemented. You are starting all the animations at once.
They are executed sequentially, although the time offset can be very small (maybe microseconds). I used also the StartOffset property of AnimationSet.

You have also initialized SoundPool with MaxStreams set to 1 so it will only play a single sound at once.
I know, I tried with the number of views and then I set it to 1 because I had intended to use a single sound file for the whole group of coins, because I get unwanted havior.

About the absence of sound. Have you declared SoundPool as a process global variable?
Yes, and I initialized it:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        mSoundPool.Initialize(5)
    End If
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Discovered the problem of no sounds: I must declare also the LoadID in the process globals! Wake up, "Luca".

Well, now there is only one (little) problem: the "coin sounds" should be started with a time offset (otherwise it seems to be a unique sound with higher sound volume).
I'm thinking to use a timer: what about this?

(to test, touch the screen)

[please, pay no attention to the confusion in the project :D]
 

Attachments

  • MoneyAnim.zip
    123.7 KB · Views: 338
Upvote 0
Top