39

Is it possible to play a sound after my app was compiled and deployed on smartphone in Android Studio / IntelliJ

My workaround is to play a sound inside of onStart() method of my StartActivity, but I have to implement (copy/paste) this pattern for every new project. Not realy nice solution.

2
  • 2
    Since Android Studio uses Gradle for the builds, if you can figure out how to make a Gradle task play a sound, you should be able to chain that into the build process. Commented Mar 20, 2015 at 12:00
  • See also this answer: stackoverflow.com/a/74055895/619673 Commented Dec 16, 2022 at 13:41

5 Answers 5

33

In Android Studio, go into Preferences > Appearance & Behavior > Notifications, go down to Gradle Build (Logging) and check the Read aloud box.

This will speak Gradle build finished in x minutes and x seconds when your build is complete.

Sign up to request clarification or add additional context in comments.

5 Comments

Could not find in Android Studio 3.4.2. Can you post some screenshots?
This only supports to mac
Well, I activated this last week (thanks to your answer). I had to trace this answer again to learn how to deactivate it. It gets annoying real quick.
Note that for build failure this solution says nothing
it will says BUILD FAILED in xxs for android studio version Dolphin 2021.3.1
3

A blend of https://stackoverflow.com/a/66226491/8636969 and https://stackoverflow.com/a/63632498/8636969 that's very simple for mac:

gradle.buildFinished { BuildResult buildResult -> try { "afplay /System/Library/Sounds/Submarine.aiff".execute() } catch (Throwable ignored) {} } 

in your build.gradle. This just plays the Submarine sounds everytime a build finishes.

Comments

3

Refer to this we need to call the task inside afterEvaluate. And since I can't comment, I will put my working code here. This code works for windows.

You can add this to your app/.gradle file inside android{ } tag :

afterEvaluate { gradle.buildFinished{ BuildResult buildResult -> if (buildResult.failure) { ['powershell', """(New-Object Media.SoundPlayer "C:\\failed_notif.wav").PlaySync();"""].execute() println("failed doing task") } else { ['powershell', """(New-Object Media.SoundPlayer "C:\\succeed_notif.wav").PlaySync();"""].execute() println("build finished") } } } 

Please note that this method can only run with .wav file. If you want to use an .mp3 you can try this.

Comments

1

On Windows you can do it like this (in build.gradle):

gradle.buildFinished { BuildResult buildResult -> // Beep on finish try { String filename = buildResult.getFailure() ? 'Alarm10.wav' : 'Alarm02.wav' ['powershell', '-c', """(New-Object Media.SoundPlayer "C:\\Windows\\Media\\${filename}").PlaySync();"""].execute() } catch (Throwable ignored) {} } 

2 Comments

Where should we put this code? In project level or app level and inside which tag?
@MayankSharma, anywhere in the root of build.gradle file.
1

On mac based on this gist and this answer to find the folder do:

Create file speak.gradle with below content inside ~/.gradle/init.d folder (if you can't find init.d folder, you can create it)

speak.gradle

// When runnning a Gradle build in the background, it is convenient to be notified immediately // via voice once the build has finished - without having to actively switch windows to find out - // and being told the actual exception in case of a build failure. // Put this file into the folder ~/.gradle/init.d to enable the acoustic notifications for all builds gradle.addBuildListener(new BuildAdapter() { @Override void buildFinished(BuildResult result) { def projectName = gradle.rootProject.name if (result.failure) { playSound('Submarine.aiff') def e = getExceptionParts(result) "say '$projectName build failed: ${e.first} ${e.second}.'".execute() } else { if (projectName != "buildSrc") { playSound('Glass.aiff') "say '$projectName build successful.'".execute() } } } private Tuple2<String, String> getExceptionParts(BuildResult result) { def exception = result.failure if (exception.cause != null) { exception = exception.cause } def className = exception.class.simpleName def of = className.indexOf('Exception') new Tuple2<String, String>(className.substring(0, of), className.substring(of)) } private void playSound(def sound) { "afplay /System/Library/Sounds/$sound".execute() sleep(100) } }) 

you can simplify more the sound to done and fail

1 Comment

For a simplified variant: gradle.buildFinished { buildResult -> if (buildResult.failure) { "say Build failed.".execute() } else { "say Ok.".execute() }}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.