java - 精确的安卓节拍器

标签 java android

这个 question 的答案声明我们可以使用 AudioTrack 在 Android 上制作可靠且精确的节拍器。我们也可以使用 MediaPlayerSoundPoolThreadTimer,但它们总是会造成延迟。除了使用 AudioTrack 生成合成声音,我们如何使用自定义音频文件实现相同的效果?

最佳答案

您可以尝试使用 System.nanoTime() 创建您自己的时间计数器,当您需要精确度时,您总是可以使用它。

public static long nanoTime()

Returns the current value of the most precise available system timer, in nanoseconds. This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

For example, to measure how long some code takes to execute:

long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
Returns: The current value of the system timer, in nanoseconds. Since: 1.5

来源:

Oracle 文档 https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#nanoTime()

关于java - 精确的安卓节拍器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33663429/

相关文章:

java - 如何在 SOAPEnvelope 中添加命名空间声明

java - 在 Android "click and hold"操作中注册一个选项

Android MediaController 拦截所有其他触摸事件

android - Intent extra 未在特定设备上交付

java - 安卓和推特

android sdk 23 instrumentation权限问题(java.lang.SecurityException)

android - String转Android JSONObject丢失utf-8

java - Java Spring 和 Tomcat 并发文件下载速度慢

java - 在特定位置/位置打印数组 java netbeans

java - 在没有 Proxy 类的情况下使用方面注入(inject) InvocationHandlers