在我的应用程序在Android Studio / IntelliJ中的智能手机上编译和部署后,是否可以播放声音
我的解决方法是在我的StartActivity的onStart()
方法中播放声音,但我必须为每个新项目实现(复制/粘贴)此模式。不是很好的解决方案。
答案 0 :(得分:9)
在Android Studio中,进入偏好设置>外观&行为>通知,请转到Gradle Build(Logging)并选中Read aloud框。
这将说明Gradle构建在x分钟内完成,而构建完成时则为x秒。
答案 1 :(得分:3)
不确定它是否可以发出声音,但您可以使用带有gradle的announce plugin来获取通知。检查this article是否开始。通知行为肯定取决于您使用的操作系统..
答案 2 :(得分:1)
在speak.gradle
文件夹中创建包含以下内容的文件~/.gradle/init.d
(如果找不到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)
}
})
您可以将声音简化为 done
和 fail
答案 3 :(得分:1)
https://stackoverflow.com/a/66226491/8636969 和 https://stackoverflow.com/a/63632498/8636969 的混合,对于 mac 来说非常简单:
gradle.buildFinished { BuildResult buildResult ->
try {
"afplay /System/Library/Sounds/Submarine.aiff".execute()
} catch (Throwable ignored) {}
}
在您的 build.gradle
中。这只会在每次构建完成时播放潜艇声音。
答案 4 :(得分:0)
在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) {}
}