AudioTrack MODE_STATIC issue

All your problems with Audio, Video and Images.

AudioTrack MODE_STATIC issue

Postby pking » Fri Aug 13, 2010 7:39 am

I'm developing a frequency generator app using AudioTrack in MODE_STATIC.

Basically, I write out a sample buffer to AudioTrack and use setLoopPoints() to loop over the buffer until It's flagged to stop.

This works really well - in MODE_STATIC data is written just once to the hardware and doesn't require a loop, saving the processing time I'd otherwise be using to push out sample buffers with MODE_STREAM. The result is nice, clean sine waves without any of the pops and clicks that seem to creep in when I use MODE_STREAM.

I've hit a major snag, however, while debugging my app. If I have sound rolling around in the hardware buffer and my app is killed unexpectedly, my app will never 'stop' that buffer; it will continue playing audio until I reboot or kill mediaserver.

I can't find any documentation about this bit of behavior so I'm at a loss on how to handle it cleanly. When my service is killed I have no discernible way to re-attach to that stray audio stream, and I can't kill it on the way down as onDestroy() will never be called on my service.

Has anyone come up against this or anything similar while accessing other hardware? Any suggestions on how I might handle killing this leaked audio?
pking
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 13, 2010 6:55 am

Top

Re: AudioTrack MODE_STATIC issue

Postby enalovej » Fri Aug 13, 2010 9:20 am

The result is nice, clean sine waves without any of the pops and clicks that seem to creep in when I use MODE_STREAM.
Challenge, the feeling of accomplishment, and knowing that you have made a contribution to tiffany jewelry.
enalovej
Freshman
Freshman
 
Posts: 2
Joined: Fri Aug 13, 2010 9:06 am

Re: AudioTrack MODE_STATIC issue

Postby pking » Fri Aug 13, 2010 4:25 pm

enalovej wrote:The result is nice, clean sine waves without any of the pops and clicks that seem to creep in when I use MODE_STREAM.


Did your reply get lost in the quote? If you were trying to hint that my 'pops and clicks' were coming from malformed samples, I can assure you I can get a very good sounding stream with MODE_STREAM most of the time, but any delays to AudioTrack.write() cause pops, which isn't entirely within my control. Increasing my buffer sizes is an option, but this will result in latency between user actions and the audio stream, which I'd like to minimize.

MODE_STATIC is definitely the way to go here if I can get around this kill issue.
pking
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 13, 2010 6:55 am

Re: AudioTrack MODE_STATIC issue

Postby pking » Tue Aug 17, 2010 4:01 am

I was able to get around this, though I believe there's a potential bug in android's AudioTrack STATIC implementation - being that once the caller application dies, so should the sound buffer in the hardware layer.

I was able to hack around this by setting the sample buffer loop positions in a timed thread, setting the loop count to 1.

This way, when my app stops telling the buffer to loop again, the sound stops playing.

It's still far more efficient than writing out the same sample to the hardware over and over, but not quite as nice as being able to set the buffer once and forget it.
pking
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 13, 2010 6:55 am

Re: AudioTrack MODE_STATIC issue

Postby blundell » Tue Aug 17, 2010 8:49 am

Can you not just cleanup before your app is killed?
Activity Lifecycle

Service Lifecycle
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: AudioTrack MODE_STATIC issue

Postby pking » Tue Aug 17, 2010 9:48 pm

If I could predict the future, then yes, I could, but I cannot tell when my app may be unexpectedly killed via a taskmanager or from the console.

The problem is, when an app is killed hard, code execution *stops* completely, there's no call to onPause, onStop, onDestroy, etc, so there's no way to clean up in this state. Under a normal close or 'stopped by the system to free resources' kill, when these functions do get called, I can clean up the audio buffers and release their resources with no issue.
pking
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 13, 2010 6:55 am

Top

Re: AudioTrack MODE_STATIC issue

Postby blundell » Tue Aug 17, 2010 10:02 pm

I would of thought a kill by a taskmanager would fulfill the lifecycle of onDestroy();
Have you tested this?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: AudioTrack MODE_STATIC issue

Postby pking » Wed Aug 18, 2010 3:49 pm

I have indeed - on a kill nothing gets called, processing stops completely.

A kill by the system to free resources does follow the normal cycle, basically asking your app nicely, 'please exit'.

A user explicitly killing the app on the other hand is like a swift boot out the door, and don't come back asking for your stuff!
pking
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 13, 2010 6:55 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests