开发者

AudioFlinger(59): RecordThread: buffer overflow when Activity is paused?

I have an application which consists of a service and an activity that may be started by the service for certain events. The service may create & use the AudioRecord & AudioTrack classes - at which time the application's Activity is displayed. The problem is if the Activity is paused (i.e., onPause() is called) I start getting the RecordThread: buffer overflow errors?

My guess is the Au开发者_开发知识库dioRecorder is running in the main thread. And, even though it was created by the service, when the Activity pauses reading stops thus the buffer overflows? Must the AudioRecorder reading be done in a separate thread even though it is running in the service?

Any help would be greatly appreciated, thanks.


The RecordThread buffer overflows happen when you aren't pulling the data from the AudioRecord object fast enough.

You should definitely have the loop that pulls data from the AudioRecord object in a sperate thread, and you should stop that thread if your activity gets paused (unless you want to record in the background.)

Here are a couple examples of working implementations:

  • And-dev example
  • StackOverflow example


Although an old question, I wish I knew this answer when starting to work with Services, so I will log it for future reference by others:

An often overlooked but VERY important point regarding Services is the fact that they do not automatically spawn their own threads, but runs on the main GUI thread. This is very counter-intuitive when considering the word 'service', but nonetheless true. (See the first 'Caution' section at http://developer.android.com/guide/topics/fundamentals/services.html).

You may also want to consider extending the IntentService class instead of the Service class which will "...create[s] a default worker thread that executes all intents ... separate from your application's main thread." (http://developer.android.com/guide/topics/fundamentals/services.html)

Hope someone finds this useful!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜