我反复调用ToneGenerator.startTone()
发出短促的声音。但是在第一次通话时,它会阻塞很长一段时间。因此,第一次爆发太长了。这是一个例子:
成员变量:
private ToneGenerator mDTMFPlayer
在构造函数中:
mDTMFPlayer = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, TONE_RELATIVE_VOLUME);
在以
Thread
开头的OnClickListener.onClick()
中:long startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG,"After 1st: " + (System.currentTimeMillis() - startTime));
try { Thread.sleep(160); } catch (InterruptedException e) { }
mDTMFPlayer.stopTone();
startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG,"After 2nd: " + (System.currentTimeMillis() - startTime));
try { Thread.sleep(160); } catch (InterruptedException e) { }
mDTMFPlayer.stopTone();
startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG,"After 3rd: " + (System.currentTimeMillis() - startTime));
try { Thread.sleep(160); } catch (InterruptedException e) { }
mDTMFPlayer.stopTone();
这是输出,
startTone()
的执行时间以毫秒为单位:11-16 18:07:35.885 16927-17977/com.my.project D/Ring: After 1st: 454
11-16 18:07:36.502 16927-17977/com.my.project D/Ring: After 2nd: 0
11-16 18:07:36.672 16927-17977/com.my.project D/Ring: After 3rd: 1
第一次通话阻塞了将近半秒钟,对于我所需的时间来说太长了。此后的任何呼叫都会使阻塞消失一段时间。奇怪的是,如果我稍等一下再试一次,它又变慢了。似乎有一段时间之后,封锁再次出现。
请指教。
最佳答案
我认为AudioManager.STREAM_VOICE_CALL
导致了这种情况。我设计的行为与您相似。我运行应用程序后,它对于第一个startTone()
调用的启动时间很长。如果我退出并进入应用程序,则可以快速处理所有3个电话。但是,如果在应用启动之前播放了一些系统声音,它将显示相同的“慢,快,快”结果。
所以我想这与流切换/阻塞有关,因为使用AudioManager.STREAM_NOTIFICATION
,它在我的设备上仅需要4-10毫秒。也可以在这里阅读更多信息:What is the difference between AudioManager's stream types at low level?
考虑以下代码:
for (int i = -1; i < 10; i++) {
System.out.println("AudioSystem stream " + i);
mDTMFPlayer = new ToneGenerator(i, TONE_RELATIVE_VOLUME);
long startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG, "After 1st: " + (System.currentTimeMillis() - startTime));
try {Thread.sleep(160);} catch (InterruptedException e) {}
mDTMFPlayer.stopTone();
startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG, "After 2nd: " + (System.currentTimeMillis() - startTime));
try {Thread.sleep(160);} catch (InterruptedException e) {}
mDTMFPlayer.stopTone();
startTime = System.currentTimeMillis();
mDTMFPlayer.startTone(ToneGenerator.TONE_DTMF_0);
Log.d(TAG, "After 3rd: " + (System.currentTimeMillis() - startTime));
try {Thread.sleep(160);} catch (InterruptedException e) {}
mDTMFPlayer.stopTone();
mDTMFPlayer.release();
}
输出:
I/System.out: AudioSystem stream -1 STREAM_DEFAULT
D/com.example.MainActivity: After 1st: 8
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 0 STREAM_VOICE_CALL
D/com.example.MainActivity: After 1st: 325
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 1 STREAM_SYSTEM
D/com.example.MainActivity: After 1st: 17
D/com.example.MainActivity: After 2nd: 2
D/com.example.MainActivity: After 3rd: 3
I/System.out: AudioSystem stream 2 STREAM_RING
D/com.example.MainActivity: After 1st: 28
D/com.example.MainActivity: After 2nd: 2
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 3 STREAM_MUSIC
D/com.example.MainActivity: After 1st: 19
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 4 STREAM_ALARM
D/com.example.MainActivity: After 1st: 28
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 5 STREAM_NOTIFICATION
D/com.example.MainActivity: After 1st: 16
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 6 STREAM_BLUETOOTH_SCO
D/com.example.MainActivity: After 1st: 332
D/com.example.MainActivity: After 2nd: 2
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 7 STREAM_SYSTEM_ENFORCED
D/com.example.MainActivity: After 1st: 324
D/com.example.MainActivity: After 2nd: 1
D/com.example.MainActivity: After 3rd: 1
I/System.out: AudioSystem stream 8 STREAM_DTMF
D/com.example.MainActivity: After 1st: 26
D/com.example.MainActivity: After 2nd: 2
D/com.example.MainActivity: After 3rd: 4
I/System.out: AudioSystem stream 9 STREAM_TTS
D/com.example.MainActivity: After 1st: 12
D/com.example.MainActivity: After 2nd: 4
D/com.example.MainActivity: After 3rd: 2
顺便说一句,如果您想研究相关的C源代码,可以查看android_media_ToneGenerator.cpp,ToneGenerator.h,ToneGenerator.cpp AudioService.java
关于java - Android ToneGenerator startTone()首次通话非常慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47340816/