1

I would like to play music when all the tasks are done. I don't know how to hook it with the last task/event.

So far I managed to download and play music and I can execute it manually.

Here is my code (mostly working):

def tempSound = "/tmp/gradleBuildFinished.wav" task downloadMusic { doLast { println "downloadMusic" def sounds = [ "http://www.xxx.zzz/hos1.wav", "http://www.xxx.zzz/hos2.wav", ] def r = new Random() def soundIndex = r.nextInt(sounds.size()) println "Deleting temp sound: " + delete(tempSound) exec { commandLine("bash", "-c", "curl " + sounds.get(soundIndex) + " > " + tempSound) } } } task playMusic { dependsOn downloadMusic doLast { println "playMusic" assert file(tempSound).exists() ("afplay " + tempSound).execute() } } gradle.buildFinished{ // how to execute playMusic? It will trigger when all tasks are done, right? } 

1 Answer 1

0

I got it. The main task should have been finalized by the assemble task.

This is the working solution. It executes the script when you call 'assembleDebug'. You need to have 'curl' and 'afplay' available to use from the command line. It works on MacOS. You can change 'afplay' to your preferred music player, such as 'vlc'.

def tempSound = "/tmp/gradleBuildFinished.wav" task downloadAssembleSound() { doLast { println "> Task :downloadAssembleSound #" def sounds = [] if (i >= 1 && i <= 8) { sounds.add("http://cs.procs.lt/cstrike/sound/round_sound/rr" + i + ".mp3") } } def r = new Random() def soundIndex = r.nextInt(sounds.size()) delete(tempSound) def sound = sounds.get(soundIndex) println "Fetching.... " + sound exec { commandLine("bash", "-c", "curl " + sound + " > " + tempSound) } } } task stopAssembleSound { dependsOn { downloadAssembleSound } doLast { try { new ProcessBuilder("bash", "-c", "killall afplay").start().waitFor() } catch (Exception e) { println("Silently ignored exception: " + e.printStackTrace) } } } task playAssembleSound { dependsOn { stopAssembleSound } doLast { assert file(tempSound).exists() ("afplay " + tempSound).execute() } } tasks.whenTaskAdded { task -> if (task.name.contains('assemble') && task.name.contains('Debug')) { task.finalizedBy { playAssembleSound } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.