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.

alex
  • 8,904
  • 6
  • 49
  • 75
  • 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. – CommonsWare Mar 20 '15 at 12:00
  • See also this answer: https://stackoverflow.com/a/74055895/619673 – deadfish Dec 16 '22 at 13:41

5 Answers5

32

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.

Pouya Heydari
  • 2,496
  • 26
  • 39
2

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.

2

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.

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) {}
}
Sergey P.
  • 61
  • 1
  • 2
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

Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
  • 1
    For a simplified variant: `gradle.buildFinished { buildResult -> if (buildResult.failure) { "say Build failed.".execute() } else { "say Ok.".execute() }}` – Joshua Goldberg Aug 25 '21 at 20:21