我的应用程序在Android Studio/InIntelliJ 中编译并部署在智能手机上后,是否可以播放声音
我的解决方法是在StartActivity的onStart()
方法中播放声音,但我必须为每个新项目实现(复制/粘贴)此模式。不是很好的解决方案。
在Android Studio中,进入首选项>外观&行为>通知,转到渐变构建(日志记录)并选中朗读框。
当你的构建完成时,这将表示Gradle构建在x分x秒内完成。
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
中。这只是在每次建造完成时播放潜艇的声音。
为此,我们需要调用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
,你可以试试这个。
在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) {}
}
在mac上基于这个要点和这个答案找到文件夹do:
在~/.gradle/init.d
文件夹中创建包含以下内容的文件speak.gradle
(如果找不到init.d
文件夹,可以创建它)
speake.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