int n1 = 65535;
int n2 = 1659;
int fs1 = 144000;
int fs2 = 7056000;
int dfrq = 44100;
int delay = (int) ((double) n1 / 2 / ((double) fs1 / dfrq) + (double) n2 / 2 / ((double) fs2 / dfrq));
以上计算结果为我提供了随机值,有时是正确的delay = 10040,但是有时delay = 2147483647(二进制值为0b1111111111111111111111111111111),是溢出的。
问题是为什么在android上,此计算会随机产生两个不同的结果。
我的临时解决方案是:
double s1 = 1f * n1 / 2;
double s2 = 1f * fs1 / dfrq;
double s3 = 1f * n2 / 2;
double s4 = 1f * fs2 / dfrq;
L.d(TAG, "s1:" + s1 + " s2:" + s2 + " s3:" + s3 + " s4:" + s4);
int delay = (int) (s1 / s2 + s3 / s4);