我正在多次调用ToneGenerator.startTone()来发出短促的声音.但在第一次通话时,它会长时间阻挡.所以第一次爆发太长了.这是一个例子:
成员变量:
private ToneGenerator mDTmfplayer
在构造函数中:
mDTmfplayer = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, TONE_relative_VolUME);
在由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: 45411-16 18:07:36.502 16927-17977/com.my.project D/Ring: After 2nd: 011-16 18:07:36.672 16927-17977/com.my.project D/Ring: After 3rd: 1
第一个电话是阻塞了将近半秒,这对于我需要的时间太长了.之后的任何调用都会使阻塞消失一段时间.有点奇怪的是,如果我再等一下再试一次,那就再慢了.似乎有一段时间后阻塞回来了.
请指教.
解决方法:
我认为AudioManager.STREAM_VOICE_CALL导致了这一点.我的设计上的行为与你的相似.在我运行app之后,它有第一次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_DEFAulTD/com.example.MainActivity: After 1st: 8D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 0 STREAM_VOICE_CALLD/com.example.MainActivity: After 1st: 325D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 1 STREAM_SYstemD/com.example.MainActivity: After 1st: 17D/com.example.MainActivity: After 2nd: 2D/com.example.MainActivity: After 3rd: 3I/System.out: AudioSystem stream 2 STREAM_RINGD/com.example.MainActivity: After 1st: 28D/com.example.MainActivity: After 2nd: 2D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 3 STREAM_MUSICD/com.example.MainActivity: After 1st: 19D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 4 STREAM_ALARMD/com.example.MainActivity: After 1st: 28D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 5 STREAM_NOTIFICATIOND/com.example.MainActivity: After 1st: 16D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 6 STREAM_BLUetoOTH_SCOD/com.example.MainActivity: After 1st: 332D/com.example.MainActivity: After 2nd: 2D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 7 STREAM_SYstem_ENFORCEDD/com.example.MainActivity: After 1st: 324D/com.example.MainActivity: After 2nd: 1D/com.example.MainActivity: After 3rd: 1I/System.out: AudioSystem stream 8 STREAM_DTMFD/com.example.MainActivity: After 1st: 26D/com.example.MainActivity: After 2nd: 2D/com.example.MainActivity: After 3rd: 4I/System.out: AudioSystem stream 9 STREAM_TTSD/com.example.MainActivity: After 1st: 12D/com.example.MainActivity: After 2nd: 4D/com.example.MainActivity: After 3rd: 2
顺便说一句,如果你想研究相关的C代码,你可以看看android_media_ToneGenerator.cpp,ToneGenerator.h,ToneGenerator.cpp AudioService.java
总结以上是内存溢出为你收集整理的java – Android ToneGenerator startTone()在第一次调用时非常慢全部内容,希望文章能够帮你解决java – Android ToneGenerator startTone()在第一次调用时非常慢所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)