我反复呼叫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
第一个电话是阻塞了将近半秒,这对于我需要的时间太长了。之后的任何调用都会使阻塞消失一段时间。奇怪的是,如果我等一下再试一次,它又会变慢。似乎有一段时间阻塞回来了。
请告知。
答案 0 :(得分: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