sound and a listener

5 replies [Last post]
terasoft
User offline. Last seen 1 day 12 hours ago. Offline
Joined: 5 Jun 2010

I have this problem: I need to use a listener to determine the end of a sound. Then I need to start a different sound (without a listener).
It works OK when the first sound is completely played. But when the user's action interrupts the first sound, the listener is not canceled but it is processed after the second sound ends.

Replies

Tom
User offline. Last seen 1 hour 58 min ago. Offline
Ansca Staff
Joined: 13 Jul 2010

Have you tried removing the listener after user interrupts the sound?

Could you post some sample code showing the problem?

Thanks,
-Tom

SumeragiSubaru
User offline. Last seen 19 weeks 5 days ago. Offline
Joined: 31 May 2010

Well, can you tell me, how can I remove onComplete listener?
There is no solution in API reference
https://developer.anscamobile.com/content/multimedia-features#Completion_Listener-1
There is how I create listener but nothing about removing..
When I tried remove it with
removeEventListener I haven't assign to. I need remove listener in longe (mp3) sounds and there is no way how could I do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function completeFunction1()
--some code
print("1");
end
function completeFunction2()
--some other code
print("2");
end
function myFunctionTwo()
  media.playSound("someSound2.mp3",completeFunction2,false);
end
media.playSound("someSound1.mp3",completeFunction1,false);
myButt:addEventListener("touch",myButtTouch);
function myButtTouch(event)
        local t = event.target;
        local phase = event.phase;
        if "began" == phase then
                -- Make target the top-most object
                t.isFocus = true;
                t:stopAtFrame(2);
        elseif t.isFocus then
                if "ended" == phase or "cancelled" == phase then
                        t.isFocus = false;
                        t:stopAtFrame(1);
                        if phase=="ended" then
                          myFunctionTwo();
                        end
                end
        end
end 

If listener is done, everything is OK
but if I click on myButt while sound is still playing, sound1 is interupted and sound2 is going to play, but after complete sound2 is done completeFunction1 and it is bad. I need completeFunction2 to proceed.
thx
Sumeragi

Tom
User offline. Last seen 1 hour 58 min ago. Offline
Ansca Staff
Joined: 13 Jul 2010

You can only play one sound at a time with media.playSound. If the user interrupts sound1 then you need to call media.stopSound() before starting sound2. This will remove the event listener for sound1.

Your media.playSound("someSound1.mp3",completeFunction1,false); should be written as:
media.playSound("someSound1.mp3",completeFunction1);

The "false" parameter should not be there. If you want the sound to loop, you replace the completeFunction1 function with "true".

-Tom

SumeragiSubaru
User offline. Last seen 19 weeks 5 days ago. Offline
Joined: 31 May 2010

I donť wont disappoint you, but unfortunately you are wrong.
removing parameter "false" in playSound("someSound1.mp3",completeFunction1,false)
has no effect on removing listener
I also tried media.stopSound() call but it still calls first listener.
Please try this sample and you will see.http://leteckaposta.cz/351045374

after run there will be yellow ring and dog will be barking. after complete first sound console will print "1" and next sound will be played. after second sound conosle will print "2"
try click to yellow ring while dog's barking and this interrupt this sound and runs next one but it will call first listener because console will print "1"

thx.. I am almost sure there is no way how can I remove that listener till sound ends

Tom
User offline. Last seen 1 hour 58 min ago. Offline
Ansca Staff
Joined: 13 Jul 2010

I ran your sample app and it does show a problem. The media.stopPlayer() does stop Sound1 but it doesn't remove Sound1 listener so when Sound2 ends, it still calls Sound1 listener. I filed this as bug #762 in our internal database.

Until it's fixed I would suggest that you only use one sound Listener function and that you set a flag indicating which sound file is associated with the call.

Thanks.
-Tom

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.