Bug 299180

Summary: Event "trackChanged" is notified twice
Product: [Applications] amarok Reporter: Fabsk <fabsk>
Component: NotificationsAssignee: Amarok Developers <amarok-bugs-dist>
Status: RESOLVED NOT A BUG    
Severity: normal CC: kfunk
Priority: NOR    
Version: 2.6.0   
Target Milestone: 2.7   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Fabsk 2012-05-01 16:51:17 UTC
I'm using the script Amarok2LibreFM. I noticed that since Amarok 2.5 (included in Kubuntu 12.04), the callback function passed to «Amarok.Engine.trackChanged.connect» is called twice in a raw when starting a new track (and once when Amarok is closed while playing).
In Amarok 2.4.3, it was called only once.

Reproducible: Always

Steps to Reproduce:
1. install the script Amarok2LibreFM (I guess that any simple script calling «Amarok.Engine.trackChanged.connect( onTrackChange )» and tracing the current track would do the job)
2. install «lastfmsubmitd» and stop it
3. once a track is played, notice that there are two files in /var/spool/lastfm/
Actual Results:  
callback called twice

Expected Results:  
callback should be called only once
Comment 1 Myriam Schweingruber 2012-11-27 15:12:39 UTC
Looks like this report slipped from our attention, sorry for that, but it was not reported for the correct component. Could you please upgrade to Amarok 2.6 and try again?
Comment 2 Fabsk 2012-11-27 19:00:40 UTC
Amarok 2.6 is now present in Kubuntu 12.10, and unfortunately the double-submit is still present.
Comment 3 Myriam Schweingruber 2012-11-27 21:14:11 UTC
And this only happens when you activate that script? Then you should talk to the script author, as we are not responsible for 3rd-party add-ons.
Comment 4 Fabsk 2012-11-28 04:52:23 UTC
I only have this script to reproduce this issue. But this script is simple enough for me to say that this issue would bother any script that would want to be notified when the current track is changed.

Here is a reduced script sample (hard to make shorter):

Importer.include ( "config.js" );
function onTrackChange() { Amarok.alert("Track changed"); }
Amarok.Engine.trackChanged.connect( onTrackChange );

The message "Track changed" will be displayed twice when starting the track (and only once when stopping the track). The are some work around at the script level (like ignoring one of the notifications), but it is ugly.
Comment 5 Myriam Schweingruber 2012-11-28 10:26:29 UTC
Then it is the script to blame, as it triggers an action Amarok will already perform if you have the notifications enabled. That is redundant.
Comment 6 Fabsk 2012-11-28 18:23:50 UTC
I'm afraid that it is the other way around. The script asks Amarok to be notified when the song is changed, and . The script does not trigger anything and does not notify anything, it is being notified by Amarok.
And Amarok send the notification twice to the script. It is no only a problem with the script «Amarok2LibreFM» in particular, it is a problem with any script wanting to be notified when a track changes.
The notification system is a standard functionality of the scripting engine and is described there:

http://amarok.community.kde.org/Development/Script_API#Amarok.Engine
Comment 7 Fabsk 2012-11-28 20:09:18 UTC
I had a look in the code and I found that we currently can't rely on the notifications for the new tracks. In "src/EngineController.cpp", there is this comment:

// only update stats if we are called for something new, some phonon back-ends (at
// least phonon-gstreamer-4.6.1) call slotNewTrackPlaying twice with the same source

And on this blog about an Amarok statistic project:

«EngineController has to deal with different phonon back-ends and this is delicate. Phonon-vlc likes to emit aboutToFinish() twice in some cases while phonon-gstreamer likes to emit currentSourceChanged() twice and metaDataChanged() even more often (witch no reason). EngineController has to be written very carefully not to explode in case of such rough handling.»

http://strohel.blogspot.com/2012/07/amarok-statsyncing-gsoc-weeks-8-and-9.html

That's a pity that we don't get the notifications as one would expect (in the Amarok code as shown above, and in the script). So I guess that for the time being, the solution would be what I have done for «Amarok2LibreFM», add some logic in the script to ignore the second notification :-(