【问题标题】:Android Studio: sound notification on build successAndroid Studio:构建成功的声音通知
【发布时间】:2015-03-20 11:58:08
【问题描述】:

我的应用在 Android Studio / IntelliJ 中编译并部署在智能手机上后是否可以播放声音

我的解决方法是在我的 StartActivity 的 onStart() 方法中播放声音,但我必须为每个新项目实现(复制/粘贴)此模式。不是很好的解决方案。

【问题讨论】:

  • 由于 Android Studio 使用 Gradle 进行构建,如果您知道如何让 Gradle 任务播放声音,您应该能够将其链接到构建过程中。

标签: android android-studio notifications


【解决方案1】:

在 Android Studio 中,进入 Preferences > Appearance & Behavior > Notifications,进入 Gradle Build (Logging) 并选中 Read aloud 框。

当您的构建完成后,这将说明 Gradle 构建在 x 分 x 秒内完成。

【讨论】:

  • 在 Android Studio 3.4.2 中找不到。可以发一些截图吗?
  • 这个只支持到mac
  • 好吧,我上周激活了这个(感谢您的回答)。我不得不再次追踪这个答案以了解如何停用它。很快就会很烦人。
【解决方案2】:

参考this我们需要调用afterEvaluate里面的任务。 由于我无法发表评论,我将把我的工作代码放在这里。此代码适用于 Windows。

您可以将其添加到 android{ } 标签内的 app/.gradle 文件中:

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")
        }
    }
}

请注意,此方法只能使用.wav 文件运行。如果你想使用.mp3,你可以试试this

【讨论】:

    【解决方案3】:

    在 Windows 上,您可以这样做(在 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) {}
    }
    

    【讨论】:

    • 我们应该把这段代码放在哪里?在项目级别或应用级别,在哪个标签内?
    【解决方案4】:

    https://stackoverflow.com/a/66226491/8636969https://stackoverflow.com/a/63632498/8636969 的混合对于 mac 来说非常简单:

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

    在您的build.gradle 中。这只会在每次构建完成时播放 Submarine 声音。

    【讨论】:

      【解决方案5】:

      在mac上根据这个gist和这个answer来查找文件夹做:

      ~/.gradle/init.d文件夹中创建文件speak.gradle,内容如下(如果你找不到init.d文件夹,你可以创建它)

      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)
          }
          
      })
      

      您可以将更多的声音简化为donefail

      【讨论】:

      • 对于简化的变体:gradle.buildFinished { buildResult -&gt; if (buildResult.failure) { "say Build failed.".execute() } else { "say Ok.".execute() }}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多