简体   繁体   English

如何在Android Studio中对录制音频进行线程处理?

[英]How to thread process recording audio in Android Studio?

我正在使用AudioRecord对象,并且在AudioRecord.OnRecordPositionUpdateListener我正在实现AudioRecord.read以将数据写入字节缓冲区,此后,我调用另一个对象的方法来处理字节缓冲区,然后将其返回以写入文件,就是问题是对象的处理时间很长,似乎正在停止主线程,这很奇怪,我认为AudioRecord.OnRecordPositionUpdateListener是Threaded

Unless it explicitly says so, assume nothing is threaded in android. 除非明确说明,否则假设android中没有线程。 Its all callbacks on the UI thread. UI线程上的所有回调。

The normal way to do this would be to start up a thread in onCreate or onStart of your app, and have it wait for a message from the main thread. 通常的方法是在应用程序的onCreate或onStart中启动一个线程,然后等待主线程发出消息。 Your implementation of onMarkerRead (or whatever other function you need) would then send a message to that thread. 然后,您对onMarkerRead的实现(或您需要的任何其他函数)将向该线程发送一条消息。 You could create a thread whenever the notification function is called, but keeping the thread around avoids thread creation overhead at the OS level every time you get a notification. 每当调用通知功能时,您都可以创建一个线程,但是保持该线程的位置避免了每次收到通知时在OS级别创建线程的开销。

Avoid using AsyncTasks here. 避免在此处使用AsyncTasks。 They sound like the right solution, but since all app tasks are single threaded they could be held up indefinitely. 它们听起来像是正确的解决方案,但是由于所有应用程序任务都是单线程的,因此可以无限期地保留它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM